 # CODEFORCES even array problem doubt?

How to solve following question, i tried many times but failed.
https://codeforces.com/problemset/problem/1367/B

keep count of odd numbers in even places and even numbers in odd places…if they are equal then thats your answer…or else not possible

Keep count of odd numbers in even indices in a variable `o` and even numbers in odd indices in a variable `e`.
If `o` and `e` are not equal, answer is `-1`. Or else answer is `o` or `e` (since they are equal).
This is because you’ll need `o` to be the same as `e` so that it is possible to swap them.
My AC code:

``````#include <bits/stdc++.h>

using namespace std;

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector <int> a(n);
int o = 0, e = 0;
for (int i = 0; i < n; ++i)
{
cin >> a[i];
if (i % 2 == 0 && a[i] & 1) ++o;
if (i & 1 && a[i] % 2 == 0) ++e;
}
if (o == e) cout << (o+e)/2 << '\n';
else cout << "-1\n";
}

}
``````

My brain was not working properly. That’s why in the line `if (o == e) cout << (o+e)/2 << '\n';`, I wrote `o` or `e` as `(o+e)/2`.

thanks,
i am working on it

#include<bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define sync ios_base::sync_with_stdio(false); cin.tie(NULL)
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#define mod 1000000007
#define F first
#define S second

#define DEBUG(x) do { std::cerr << x; } while (0)
//std::vector v(1e9);
using namespace std;

int main()
{
sync;
cin.tie(0);
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int f;
f=f=0;
std::vector a(n,0);
for(int i=0;i<n;i++)
{
cin>>a[i];
if((i%2)!=(a[i]%2))
{
f[i%2]++;
}
}
if(f != f)
cout<<-1<<"\n";
else
cout<<f<<"\n";
}

}

1 Like