How can I decrease the running time and compile time of the following code?
Given three integers
A
,B
andK
, you need to report theK
th number betweenA
andB
(both inclusive), satisfying the property that: the number does not contain a digit 'zero' and product of digits of number is a perfect square. If there doesn't exist thatK
th number output "-1"(without quotes).Input:
First Line contains the number of test cases \$T\$. Next \$T\$ lines contain 3 space separated integers
A
,B
,K
as mentioned in the task.Output:
Output \$T\$ lines as answer for every test case.
Constraints:
\$1 ≤ T ≤ 100\$
\$1 ≤ A ≤ B ≤ 10^{18}\$
\$1 ≤ K ≤ 10^7\$Sample input:
3 1 10 4 8 14 2 1 1000 23
Output:
-1 11 119
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cases = in.nextInt();
long A,B,K;
int caseNum =0;
while(caseNum < cases)
{
A = in.nextLong();
B = in.nextLong();
K = in.nextLong();
ArrayList<Long> products = new ArrayList<>();
long product = 1;
for(long i=A,k=0;i<=B && k<(B-A+1);i++,k++)
{
long num = i;
double sqrt;
ArrayList<Long> temp = new ArrayList<>();
while(num>0)
{
temp.add(num%10);
num = num/10;
}
for(int j=0;j<temp.size();j++)
{
if(temp.get(j)==0){
product=-1;
break;
}
product = product*temp.get(j);
//System.out.println("Currently processing: "+ temp.get(j));
}
sqrt = Math.sqrt(product);
if( product%sqrt==0)
{ products.add(i);
}
product=1;
}
try{
System.out.println(products.get((int)K-1));
}catch (Exception e) {
// TODO: handle exception
System.out.println(-1);
}
caseNum++;
}
}
}