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.
// Get the last element of set 'a' and the first element of set 'b'
int startRange = a.get(a.size() - 1); Click Here
int endRange = b.get(0);
// You can now iterate over the range from startRange to endRange
for (int i = startRange; i <= endRange; i++) {
// Check if 'i' is divisible by all elements of 'a' and is a factor of all elements of 'b'
boolean divisibleByAllA = true;
boolean factorOfAllB = true;
for (int num : a) {
if (i % num != 0) {
divisibleByAllA = false;
break;
}
}
for (int num : b) {
if (num % i != 0) {
factorOfAllB = false;
break;
}
}
if (divisibleByAllA && factorOfAllB) {
// Your logic for valid integer within range
System.out.println(i);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Between Two Sets
You are viewing a single comment's thread. Return to all comments →
// Get the last element of set 'a' and the first element of set 'b' int startRange = a.get(a.size() - 1); Click Here int endRange = b.get(0);
// You can now iterate over the range from startRange to endRange for (int i = startRange; i <= endRange; i++) { // Check if 'i' is divisible by all elements of 'a' and is a factor of all elements of 'b' boolean divisibleByAllA = true; boolean factorOfAllB = true;
}