Can anyone tell what’s wrong in my approach?
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int t = Integer.parseInt(s.next());
while(t-->0) {
int n = s.nextInt();
long k = s.nextLong();
long[] visit = new long[n];
long wait = Integer.MAX_VALUE;
for(int i = 0 ; i < n ; i++){
visit[i] = s.nextLong();
if(Math.max(k, visit[i]) % Math.min(k, visit[i]) == 0) {
long temp = Math.max(k, visit[i]);
if(temp < wait)
wait = temp;
}
if(wait > visit[i] * k)
wait = visit[i] * k;
}
if(wait < Long.MAX_VALUE)
System.out.println(wait);
else
System.out.println("-1");
}
}
The key was to find the minimum LCM of K with an element in A . Also, you had to look for overflows to solve the last sub task. Please refer to this post for that part.
And for DWW19B , there is a simple way to check for overflow in LCM. Though the logarithm approach shared by @sdssudhu is beautiful in itself, but there is another simple integer arithmetic technique also.
Let’s say INF = 10^{18} + 1, now we need to check if lcm(K, A_{i}) \ge INF. Yes lcm(K, A_{i}) can be very large (out of bounds for a 64 bit integer as well, for the last sub task).
So, you can check it like this:
We know that lcm(K, A_{i}) = \frac{K \times A_{i}}{gcd(K, A_{i})}
Just check i…
Bro Please add problems to practice.
I have mailed the admin twice now, still no reply till now.
@admin @vijju123