ADJLOVE - Editorial

hey @rahul_siddhu
Thanks for asking your doubt.

Here is a test case in which your logic fails.

1
4
1 1 1 2

Correct output

1 2 1 1

#include

#include

#include

#include<unordered_map>

#include

using namespace std;

int main()

{

ios_base::sync_with_stdio(NULL);

cin.tie(NULL);cout.tie(NULL);

int t;

cin>>t;

while(t--)

{

    int n;

    cin>>n;

    int a[n];

    int o=0,e=0;

    for(int i=0;i<n;i++)

    {

        cin>>a[i];

        if(a[i]%2)

        {

            o++;

        }

        else

        {

            e++;

        }

    }

    if(o==0)

    {

        cout<<"-1";

        cout<<"\n";

        continue;

    }

    if(e==0)

    {

        if(n%2)

        {

            cout<<"-1";

            cout<<"\n";

        }

        else

        {

            for(int i=0;i<n;i++)

            {

                cout<<a[i]<<" ";

            }

            cout<<"\n";

        }

        continue;

    }

    int ans[n];

    int k=0;

    for(int i=0;i<n;i++)

    {

        if(a[i]%2)

        {

            ans[k]=a[i];

            k++;

        }

    }

    int temp;

    if(o%2)

    {

        temp=k;

    }

    for(int i=0;i<n;i++)

    {

        if(!(a[i]%2))

        {

            ans[k]=a[i];

            k++;

        }

    }

    if(o%2)

    {

        swap(ans[temp],ans[temp-1]);

    }

    for(int i=0;i<n;i++)

    {

        cout<<ans[i]<<" ";

    }

    cout<<"\n";

}

return 0;

}
what is wrong here pls Help!!!