dimanche 10 novembre 2019

Longest Palindrome in integer array

I want to find the largest palindrome in an integer array. I tried making my own algorithm and not looking at the online ones. But this is not working. i tried doing debugging but couldn't get it to work. One of the example is "1367611342142412431113424823782".

void palindrome()
{
    int max = 0;
    int len;
    int start;
    int end;
    int st=0,en=0;
    bool palin = false;
    for(int i=0;i<size;i++)
    {
        for(int j=size-1; j>=0;j--)
        {
            if(array[i] == array[j])
            {
                start = i;
                end = j;
                while(j==i+1 || j+1 == i || j == i )
                {
                    if(array[i] == array[j])
                    {
                        i++; 
                        j--;
                        palin = true;
                    }
                    else
                    {
                        palin = false;
                        break;  
                    }
                }
                i= start;
                j= end;
            }
            if(palin == true)
                {
                    len = end - start;
                    if(len>max)
                    {
                        cout<<" "<<st<<" "<<en<<endl;
                        st=i;
                        en =j;
                        max = len;
                    }
                }
        }
    }
    cout<<endl;
    cout<<st<<" "<<en<<endl;
    ofstream file("output.txt");
    for(int i=st;i<=en;i++)
    {
        file<<array[i];
    }
}

Aucun commentaire:

Enregistrer un commentaire