In this problem I have a number of queries for which I have to output the count of integers
in the array
which is divisible by k(one of the queries).The array
contains duplicate elements. I am trying to optimise the problem and my approach is given below :
Code:
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int[] ar={2,4,6,9,11,34,654,23,32,54,76,21432,32543,435,43543,643,2646,4567,457654,75,754,7,567865,8765877,53,2};
int query=sc.nextInt();
int length=ar.length;
int count=0;
for (int i=0;i<query ;i++ ) {
int x=sc.nextInt();
for (int j=0;j<length ;j++ ) {
if(ar[j]>x){
if(ar[j]%x==0){
count++;
}
}
}
System.out.println("Count:"+count);
}
}
The above code gives the correct output, but the complexity is O(query*length)
and what if the array size is much bigger,the program will timeout
.
Can anyone help me optimize the problem?