You are viewing a single comment's thread. Return to all comments →
This Java code will help you to being a good waiter.
public static boolean[] seive(int N){ boolean[] isPrime=new boolean[N+1]; for(int i=0;i<=N;i++){ isPrime[i]=true; } isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<=N;i++){ if(isPrime[i]==true){ for(int j=i*i;j<=N;j=j+i){ isPrime[j]=false; } } } return isPrime; }
public static List<Integer> waiter(List<Integer> number, int q) { // Write your code here List<Integer> prime=new ArrayList<>(); boolean[] eratos=seive(9735); for(int i=0;i<eratos.length;i++){ if(eratos[i]){ prime.add(i); } } List<Integer> A=new ArrayList<>(number); List<Integer> answer=new ArrayList<>(); for(int i=0;i<q;i++){ List<Integer> temp=new ArrayList<>(); List<Integer> B=new ArrayList<>(); for(int h=A.size()-1;h>=0;h--){ if(A.get(h)%prime.get(i)==0){ B.add(A.get(h)); } else temp.add(A.get(h)); } for(int j=B.size()-1;j>=0;j--){ answer.add(B.get(j)); } A=new ArrayList<>(temp); } if(A.size()!=0){ for(int k=A.size()-1;k>=0;k--){ answer.add(A.get(k)); } } return answer; }
Seems like cookies are disabled on this browser, please enable them to open this website
Waiter
You are viewing a single comment's thread. Return to all comments →
This Java code will help you to being a good waiter.
public static boolean[] seive(int N){ boolean[] isPrime=new boolean[N+1]; for(int i=0;i<=N;i++){ isPrime[i]=true; } isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<=N;i++){ if(isPrime[i]==true){ for(int j=i*i;j<=N;j=j+i){ isPrime[j]=false; } } } return isPrime; }