Not passing for only 1 large testcase - Java


#1

Can someone please help me identifying what’s wrong in code? Thanks much!
public class Solution {
public int solve(ArrayList A) {
int t = 1;
ArrayList original = new ArrayList<>(A);
long arr[] = new long[original.size()];
Arrays.fill(arr,-1);
do
{
for(int i = 0; i<A.size() ;i++)
{
if(arr[i]== -1)
{
String str = A.get(i);
str = circularRotate(str,t);
A.set(i,str);
if(str.equals(original.get(i)))
{
if(arr[i] == -1)
{
arr[i] = t%1000000007;
}
}
}
}
t = (t+1)%1000000007;
}while(!original.equals(A));
long ans = lcm(arr);
return (int)(ans%1000000007);
}
public long lcm(long arr[])
{
for(int i = 0 ; i<arr.length-1;i++)
{
long gcd = gcd(arr[i],arr[i+1]);
long num = arr[i]*arr[i+1];
arr[i+1] = (num/gcd);
}
return arr[arr.length - 1];
}
public long gcd (long a, long b)
{
if(b == 0)
return a;
return gcd(b,a%b);
}
public String circularRotate(String str, int t)
{
int len = str.length();
int rot = t%len;
if(rot == 0)
return str;
String prefix = str.substring(0,rot);
String postfix = str.substring(rot);
return postfix+prefix;
}
}