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.

This is almost exactly what I did! A small optimization thing I did was to do "i += min" instead of "i++". Since you know that the next number has to be at least a multiple of that number. I don't know if I'm being clear or not haha

Thanks for this solution! I realized that
int i=min;i<=max;i++ iterates all the way to the highest value in b, which is unnecessary, as the largest possible factor of values in array b is the smallest value in it. Change int max = b[b.length-1]; to int max = b[0]; and you save a lot of unnecessary work.

The solution is very much helpful!!But "x" has to lie between the max value in array a and min value in arra y b.hence the values of min and max could be int min=a[a.length-1] and int max = b[0];This will reduce the time for processing the code.

You're right! I've optimized my solution and removed the sorting, nice! Now min always holds the the highest value in a, and max holds the lowest in b.

This is true and demonstrates good understanding of the problem constraints. In terms of memory usage however, I believe 2^32 bits are allocated for every integer, regardless of size. The unsused bits are just set to 0's.

## Between Two Sets

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

Thanks for the answer...... helped me to find out logic :)

Similar to my solution. Can't it be solved with a shorter, more elegant logic though?

What do you think about recursive approach? I think its better code. For such small array sizes its not that much slower.

This is almost exactly what I did! A small optimization thing I did was to do "i += min" instead of "i++". Since you know that the next number has to be at least a multiple of that number. I don't know if I'm being clear or not haha

Thanks for this solution! I realized that

`int i=min;i<=max;i++`

iterates all the way to the highest value in b, which is unnecessary, as the largest possible factor of values in array b is the smallest value in it. Change`int max = b[b.length-1];`

to`int max = b[0];`

and you save a lot of unnecessary work.The solution is very much helpful!!But "x" has to lie between the max value in array a and min value in arra y b.hence the values of min and max could be int min=a[a.length-1] and int max = b[0];This will reduce the time for processing the code.

This is true just tested. So ideal range is

`int min = a[a.length-1]; int max = b[0];`

:) :)

is arrays.sort() needed? you can find min and max while reading inputs.

You're right! I've optimized my solution and removed the sorting, nice! Now min always holds the the highest value in a, and max holds the lowest in b.

no reason to make max Integer.MAX_VALUE when problem constraints put the max at 100, simply put it at 101

This is true and demonstrates good understanding of the problem constraints. In terms of memory usage however, I believe 2^32 bits are allocated for every integer, regardless of size. The unsused bits are just set to 0's.

Could you explain why you figure this out ? The code is easy to understrand but I can't come up with this idea

i tried the same way in c++ but i was getting time out erreoe

Man , that is awesome