Whats wrong with my code?problem:LTIME56

problem:LTIME56
#include

using namespace std;

int main()

{

int t;
cin >> t;
while (t--)
{
    int n;
    cin >> n;
    int a[n],odd = 0,even=0;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        if (a[i] % 2 != 0)
        {
            odd++;
        }
        else
        {
            even++;
        }
    }
    if (odd % 2 == 0 || even == 0)
    {
        cout << 1 << endl;
    }
    else
    {
        cout << 2 << endl;
    }
}
return 0;

}

Try taking two loops for initializing the array and checking for if it is odd or even

1 Like

nothing changes in output.
i think mistake in problem.
if we consider case all numbers are odd in odd times
consider 1,1,3
sum of 1+1 is even and add into 3
so it will be 5 answer will be 1.
so that’s why i inserted condition even==0 .

but i analyzed some codes which has correct answer
it shows me answer 2. try yourself.

if i delete even ==0 condition and put condition n==1
i also got correct answer.

Array need to be declare with fix size, if need the size to be changing, can consider to use dynamic size array, ie vector.

As for this case, you can have a fix value 12 or const int n = 12.

with this declaration, you will have a dedicate memory allocated without any rubbish data in it. Can try it.

1 Like

There is no mistake in question. because you got this question wrong. They want to say that they chose an arbitrary pair of elements from the sequence such that their sum is even, deleted these two elements from the sequence, and inserted their sum into the sequence. means that if you have an array like [1,1,3], the sum of 1 + 1 is an even number, and you have to add that to the sequence (like [2,3], not as 2 + 3, and the final array is [5]). As a result, the minimum length is 2.

1 Like

ok i now i got it
thanks @vraj_07

1 Like

i will note this
thanks @adamjamal