Code issue , , , , , ,, , , , , , ,, ,, , ,

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner obj = new Scanner(System.in);
int t=obj.nextInt();
while(t>0)
{
int n=obj.nextInt();
int a=check(n);
System.out.println(a);
t–;
}
}
public static int check(int n)
{
ArrayList list=new ArrayList<>();
list.add(0);
list.add(1);
int j=2;
for(int i=2; i<=n; i++)
{
if(i%2==0||i%3==0||i%5==0)
{
list.add(i);
j++;
}
else
{
n++;
}
}
return list.get(j-1);
}
}


hii sir can you check this code because it’s not passing test cases

@Nitin-Mishra-2380486738834604
Try for this
1
176
correct output : 10125
your code gives : 238 // not ugly

logic :
Ugly number(i) = minimum of (2 * ugly number(f2), 3* ugly number(f3), 5*ugly number(f5))

Where, f2, f3, f5 are counters for 2, 3, 5 respectively which stores the index for f2th ugly number, f3th ugly number, f5th ugly number.

Then we will store our answer.

Algorithm:

Initializing f2, f3, f5 to maintain f2th ugly number, f3th ugly number and f5th ugly number to 1.
Creating a DP matrix in which ith cell represent ith ugly number.
Start filling the dp matrix from i = 2.
Ugly number(i) = minimum of (2 * ugly number(f2), 3* ugly number(f3), 5*ugly number(f5)).
Finding the answer using above relation and storing it at ith index.
Now, the time complexity O(N).