forked from Mugdha-Hazra/PrepBytes-questions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrime Dish.cpp
51 lines (48 loc) · 845 Bytes
/
Prime Dish.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
bool isprime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n%2 == 0 || n%3 == 0) return false;
for (int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
}
int main()
{
int t;
cin>>t;
while(t>0)
{
int n,a[1000000],i,m=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
while(m<n)
{
for(i=0;i<n;i++)
{
if(a[i]!=0&&isprime(a[i]))
{
cout<<i+1<<" ";
a[i]=0;
m++;
}
else
{ if(a[i]!=0)
a[i]++;
}
}
}
cout<<"\n";
t--;
}
return 0;
}