Hi guys!

I've been trying to make a program that reads n elements of an array and then it displays the longest increasing subsequence of prime numbers whose reverse numbers are prime as well.

For example if n=12 and the elements of tha array(positive integers)are:

51 48 11 971 44 19 181 751 347 33 929 359

it should display the subsequence 181 751 347.

181 is a prime number and its reverse is 181 that is prime as well.

It's the same for 751==>157 and 347==>743

Code:

#include <iostream>
using namespace std;
int main()
{
int i,lmax = 0,l,poz,p,n,a[50],aux,d,d1,ok1 = 0,ok2 = 0,j,rev = 0,nr,q;
cin >> n;
for(i = 1;i <= n;i++)cin >> a[i];
i = 1;
while (i <= n){ poz = i;
ok1 = 0;
aux = a[i];
for(d = 2;d <= aux/2;d++)
if(aux % d == 0)ok1 = 1;
rev = 0;
nr = aux;
while (nr>0){rev = rev*10+nr%10;
nr/=10;
}
ok2 = 0;
for(d1 = 2;d1 <= rev/2;d1++)
if(rev%d1 == 0)ok2 = 1;
if((ok1 == 0)&&(ok2 == 0))i++;
q = i;
l = q-poz+1;
if(l > lmax)lmax = l;
i++;
}
i = 1;
while (i <= n){
poz = 1;
aux = a[i];
for(d = 2;d <= aux/2;d++)
if(aux % d == 0)ok1 = 1;
rev = 0;
nr = aux;
while (nr>0){rev = rev*10+nr%10;
nr/=10;
}
ok2 = 0;
for(d1 = 2;d1 <= rev/2;d1++)
if(rev % d1 == 0)ok2 = 1;
if((ok1 == 0)&&(ok2 == 0))i++;
if (j <= n){
q = i;
l = q-poz+1;
if(l == lmax)
for(j = poz ;j <= q;j++)cout << a[j] << " ";
}
i++;
}
return 0;
}

I know that it's wrong but I don't know how to fix it.What should I do ?

Thanks in advance!