We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

There, all the multiples common to first array(a) elements is found upto 100 and stored in 'd'. and then the elements that are in second array(b) that becomes the multiples of any of the elements is found and count is incremented accordingly

public static int getTotalX(List<int> a, List<int> b)
{
int total = 0;
int number = a.Max();
Enumerable.Range(number, b.Min())
.ToList()
.ForEach(n =>
{
if (a.All(e => number % e == 0 || e % number == 0)
&& b.All(e => number % e == 0 || e %number == 0))
total++;
number++;
});
return total;
}

I noticed that Your answer goes through way too many iterations and doesn't take large enough steps, there can be no correct answer between a.Max() and 2a.Max(), therefore skipping those is a good idea.

Looping through by generating an amount of integers also isnt really necessary, just start from lowest possible correct answer ( a.Max() ) and make sure youre not over the largest possible ( b.Min() ) correct answer.

Works like a charm :).

Here is the resulting code with comments.

public static int getTotalX(List<int> a, List<int> b)
{
/*Starting from 0 found, storing largest value in A and smallest value in b. Setting starting point to largest value in A.*/
int foundCount = 0, maxA = a.Max(), minB = b.Min(), current = maxA;
while (current <= minB)
{
/*If the current value is divisible by all members of both arrays, then it's the one we want.*/
if (a.All(e => current % e == 0 || e % current == 0) && b.All(e => current % e == 0 || e % current == 0))
foundCount++;
/*Iterate the value by largest member of divisors, no reason to take smaller steps.*/
current+= maxA;
};
return foundCount;
}

" C# " for everyone CTRL+F-ing.

//Stored a.Max() and b.Min() as dsgarg71 suggested below :).

How in the world did you know to use this to find the answer?

I would really like to know.

What did you study to know this? Nothing I have ever covered in C# would tell me to use something like this to come up with this answer, hell I couldn't even understand the frickin problem. I just don't get how peeps look at something and boom know what to apply and where...really discourages me.

Dont get discouraged :).
I think studying something specifically, to solve these types of problems, would be a waste of time. These types of problems are really about building something from LEGO-s. The LEGO-s being things you know about any programming language, or problem solving overall. You just put pieces together and see what fits. If you wanted to improve yourself, just get more LEGO-s, learn more about the language, its possibilities etc...

With these types of problems, theres always a distict correct answer, which gives you a solid metric, as to how right you are. If youre wrong, you get the wrong result, so its really easy to get feedback and improve.

If understanding the problem overall was the difficult part, then maybe try some simpler problems first, this specific problem can be solved in about infinite ways, some faster/optimal, some not.

Real programmers can program fortran in any language.
Go back to CS101 and learn to use variable names that have meaning.
a,b,x,r,j,d,c provide no context.
This code would be unacceptable in a commericial application.

## Between Two Sets

You are viewing a single comment's thread. Return to all comments →

small

Could you explain what exactly is going on with this?

There, all the multiples common to first array(a) elements is found upto 100 and stored in 'd'. and then the elements that are in second array(b) that becomes the multiples of any of the elements is found and count is incremented accordingly

Small

i am not getting the logic can you please explain?

It's pretty easy

range(1,int(nmin/nmax)+1) -

Loops through the possibilites between nmax and nmin

sum((inmax)%n for n in a) == 0

Finds whether chosen n is multiple of nmax by using modulus function

sum(n%(inmax) for n in b) == 0

Finds whether chosen n is a factor of bmin

I hope you got it..

We need to find numbers b/w two arrays, we can take max from a and min from b, then we can do calculation to find numbers.

This is what i'm doing in m beloved JS.

function getTotalX(a, b) { let total = 0; let maxA = Math.max(...a); let minB = Math.min(...b); let number = maxA;

}

That makes it very easy for me.

@gkjha009 -- This is

reallygood.i think u shouldn't compute

numberIsMultipleOfAllifallElementsAreMultipleisfalseThis is a great solution. I implemented it in Swift

Nice logic!

The same logic in C#:

Hello!

I noticed that Your answer goes through way too many iterations and doesn't take large enough steps, there can be no correct answer between a.Max() and 2a.Max(), therefore skipping those is a good idea.

Looping through by generating an amount of integers also isnt really necessary, just start from lowest possible correct answer ( a.Max() ) and make sure youre not over the largest possible ( b.Min() ) correct answer.

Works like a charm :).

Here is the resulting code with comments.

" C# " for everyone CTRL+F-ing.

//Stored a.Max() and b.Min() as dsgarg71 suggested below :).

Very nice optimization! I really was not realized it. Thanks. :)

Thanks :). dsarg71 found another neat optimization. I've added that as well.

Wow It did the magic. Thanks

Thats n^3, Why not to store a.Max/b.Min and other such stuff once got so that their compute does not repeat.

You're right, the computation does indeed get repeated, I've changed the code accordingly. Good eye!

How in the world did you know to use this to find the answer?

I would really like to know.

What did you study to know this? Nothing I have ever covered in C# would tell me to use something like this to come up with this answer, hell I couldn't even understand the frickin problem. I just don't get how peeps look at something and boom know what to apply and where...really discourages me.

its all about mathematics people will learn eventually

Hey

Dont get discouraged :). I think studying something specifically, to solve these types of problems, would be a waste of time. These types of problems are really about building something from LEGO-s. The LEGO-s being things you know about any programming language, or problem solving overall. You just put pieces together and see what fits. If you wanted to improve yourself, just get more LEGO-s, learn more about the language, its possibilities etc...

With these types of problems, theres always a distict correct answer, which gives you a solid metric, as to how right you are. If youre wrong, you get the wrong result, so its really easy to get feedback and improve.

If understanding the problem overall was the difficult part, then maybe try some simpler problems first, this specific problem can be solved in about infinite ways, some faster/optimal, some not.

Keep trying ;), you'll get it eventually.

This is a fucking work of art.

nice job

why "i x nmax"?

can u explain the for loop range??

Real programmers can program fortran in any language. Go back to CS101 and learn to use variable names that have meaning. a,b,x,r,j,d,c provide no context. This code would be unacceptable in a commericial application.

whenever I see variables like a,b,x,r,i,k etc, I get a near heart attack.

RIP

static int gcd(int l,int e) { if(e==0) { return l; } else { return gcd(e,l%e); } } static int lcm(int[] e) {

int ans=1; for(int i:e) { ans=(ans*i)/gcd(ans,i); } return ans; } static int getTotalX(int[] a, int[] b) { /* * Write your code here. */

what is 101 here