#include "bits/stdc++.h"
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
int ar[n];
int oddcnt = 0;
for (int i = 0; i < n; i++)
{
cin >> ar[i];
if (ar[i] % 2 != 0)
{
oddcnt++;
}
}
int lo = 0, hi = n - 1;
for (int i = 0; i < n; i++)
{
if (ar[hi] % 2 != 0)
{
swap(ar[lo], ar[hi]);
lo++;
}
else
{
hi--;
}
}
long long ans = 0;
for (int i = 0; i < n - 1; i++)
{
int temp = ar[i] * ar[i + 1];
ans += temp;
}
if (oddcnt % 2 == 0)
{
if (ans % 2 != 0)
{
for (int i = 0; i < n; i++)
{
cout << ar[i] << " ";
}
cout << endl;
}
else
{
cout << -1 << endl;
}
}
else
{
if (ans % 2 != 0)
{
for (int i = 0; i < n; i++)
{
cout << ar[i] << " ";
}
cout << endl;
}
else
{
cout << -1 << endl;
}
}
}
}[quote="cherry0697, post:1, topic:100065, full:true"]
# PROBLEM LINK:
[Contest Division 1](https://www.codechef.com/LTIME106A/problems/ADJLOVE)
[Contest Division 2](https://www.codechef.com/LTIME106B/problems/ADJLOVE)
[Contest Division 3](https://www.codechef.com/LTIME106C/problems/ADJLOVE)
[Contest Division 4](https://www.codechef.com/LTIME106D/problems/ADJLOVE)
**Setter:** [S.Manuj Nanthan](https://www.codechef.com/users/munch_01)
**Tester:** [Manan Grover](https://www.codechef.com/users/mexomerf)
**Editorialist:** [Aman Dwivedi](https://www.codechef.com/users/cherry0697)
# DIFFICULTY:
Easy
# PREREQUISITES:
Maths
# PROBLEM:
An array is called *lovely* if the sum of product of each adjacent pair of elements is odd.
More formally, the array $S$ of size $M$ is lovely if the value $\sum_{i=1}^{M-1}$ $(S_i . S_{i+1})$ is **odd**.
You are given an array $A$ consisting of $N$ positive integers. Find a [permutation](https://en.wikipedia.org/wiki/Permutation#Permutations_of_multisets) of array $A$ which is *lovely*.
If multiple such permutations are possible, output any. If there is no *lovely* permutation, output `-1`.
# EXPLANATION:
Let's try to see what happens under various cases, before that remember the three basic properties:
$$
Even+Even = Even \\
Odd+Odd = Even \\
Odd+Even = Odd
$$
$$
and
$$
$$
Even * Even = Even \\
Odd*Odd = Odd \\
Even*Odd = Even
$$
Now let's look the cases we were talking about:
- Odd numbers are less than or equal to $1$.
Let's see what happens when there is no odd number present. Hence in any permutation you arrange the array elements. The consecutive product is gonna be even for all the indexes look at the above property that even multiplied with even gives you an even number. And then the sum of all even numbers is going to be even.
Now when there is only one odd element in the given array. Wherever you place this odd number it's left side element and right side element is going to be even. And as we know even multiplied with odd gives you even number. Again the sum of all even numbers is going to be even.
Hence we can conclude that in such cases it is never possible to achieve the odd sum in whatever permutation we arrange the given array.
- All numbers of the given array are odd.
This is very interesting and simple case as all numbers are odd and hence when the odd number is multiplied with the odd number it produces an odd number. Hence every consecutive product is an odd number. Now the answer depends whether the sum of these consecutive product is even or odd.
- $N$ is Even
There will be $(N-1)$ consecutive products that exist for the the array of length $N$. Hence $(N-1)$ is an odd number. If odd number is added odd times it produces an odd number.
- $N$ is Odd
In this case $(N-1)$ is going to be even. Hence when odd number is added even times it produces an even number.
Hence we can conclude that in such cases whether the sum is going to be odd depends on whether $N$ is even.
- All other cases
Ok, so for all other cases it is always possible to produce an odd number. Let's say the number of odd numbers in the given array be $X$. Now let us try to prove how:
[details = "X is Odd"]
Now we know that these numbers are the only chances for us to get the odd sum in the array. Now if we place all these numbers consecutively then there will be $(X-1)$ consecutive odd products. Since $(X-1)$ is even hence we will end up getting the sum as even.
Let us then do one thing place the $(X-1)$ odd numbers consecutively and the remaining odd number anywhere. Now there will be $(X-2)$ odd consecutive products in the given array which leads us to get the odd sum.
Hence we can say that it is possible to get the odd sum when $X$ is going to be odd.
[/details]
[details="X is Even"]
Very simple if you read the Odd case above. Just place the all these $X$ numbers consecutive and then you know what happens and you will be able to get the odd sum.
[/details]
# TIME COMPLEXITY:
$O(N)$ per test case.
# SOLUTIONS:
[details = "Author"]
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
odd=[]
even=[]
for i in a:
if(i%2):
odd.append(i)
else:
even.append(i)
if(len(odd)<=1 or (len(odd)==n and n%2)):
print(-1)
else:
if(len(odd)%2):
print(odd.pop(),end=β β)
print(*even,end=β ')
print(*odd)
[/details]
[details = "Tester"]
#include <bits/stdc++.h>
using namespace std;
long long readInt(long long l, long long r, char endd) {
long long x = 0;
int cnt = 0;
int fi = -1;
bool is_neg = false;
while (true) {
char g = getchar();
if (g == β-β) {
assert(fi == -1);
is_neg = true;
continue;
}
if (β0β <= g && g <= β9β) {
x *= 10;
x += g - β0β;
if (cnt == 0) {
fi = g - β0β;
}
cnt++;
assert(fi != 0 || cnt == 1);
assert(fi != 0 || is_neg == false);
assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
}
else if (g == endd) {
assert(cnt > 0);
if (is_neg) {
x = -x;
}
assert(l <= x && x <= r);
return x;
}
else {
assert(false);
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen(βinput.txtβ, βrβ, stdin);
freopen(βoutput.txtβ, βwβ, stdout);
#endif
int t;
t = readInt(1, 1000, β\nβ);
while(tβ){
int n;
n = readInt(2, 500, β\nβ);
int temp;
vector e, o;
for(int i = 0; i < n; i++){
if(i != n - 1){
temp = readInt(1, 1000000, β ');
}else{
temp = readInt(1, 1000000, β\nβ);
}
if(temp % 2){
o.push_back(temp);
}else{
e.push_back(temp);
}
}
if(o.size() == 0){
cout<<-1<<"\n";
}else{
if(o.size() % 2 == 0){
for(int i = 0; i < o.size(); i++){
cout<<o[i]<<" β;
}
for(int i = 0; i < e.size(); i++){
cout<<e[i]<<β β;
}
cout<<β\n";
}else{
if(e.size() == 0 || o.size() == 1){
cout<<-1<<"\n";
}else{
cout<<o[0]<<" β;
for(int i = 0; i < e.size(); i++){
cout<<e[i]<<β β;
}
for(int i = 1; i < o.size(); i++){
cout<<o[i]<<β β;
}
cout<<β\n";
}
}
}
}
return 0;
}
[/details]
[details = "Editorialist"]
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin>>n;
vector<int> even,odd;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(x%2)
odd.push_back(x);
else
even.push_back(x);
}
if (odd.size()<=1 || (odd.size() == n && n%2))
{
cout<<-1<<endl;
return;
}
if(odd.size()%2)
{
cout<<odd[0]<<" ";
odd.erase(odd.begin());
}
for(auto itr: even)
cout<<(itr)<<" ";
for(auto itr: odd)
cout<<(itr)<<" ";
cout<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}
[/details]
[/quote]
[quote="cherry0697, post:1, topic:100065, full:true"]
# PROBLEM LINK:
[Contest Division 1](https://www.codechef.com/LTIME106A/problems/ADJLOVE)
[Contest Division 2](https://www.codechef.com/LTIME106B/problems/ADJLOVE)
[Contest Division 3](https://www.codechef.com/LTIME106C/problems/ADJLOVE)
[Contest Division 4](https://www.codechef.com/LTIME106D/problems/ADJLOVE)
**Setter:** [S.Manuj Nanthan](https://www.codechef.com/users/munch_01)
**Tester:** [Manan Grover](https://www.codechef.com/users/mexomerf)
**Editorialist:** [Aman Dwivedi](https://www.codechef.com/users/cherry0697)
# DIFFICULTY:
Easy
# PREREQUISITES:
Maths
# PROBLEM:
An array is called *lovely* if the sum of product of each adjacent pair of elements is odd.
More formally, the array $S$ of size $M$ is lovely if the value $\sum_{i=1}^{M-1}$ $(S_i . S_{i+1})$ is **odd**.
You are given an array $A$ consisting of $N$ positive integers. Find a [permutation](https://en.wikipedia.org/wiki/Permutation#Permutations_of_multisets) of array $A$ which is *lovely*.
If multiple such permutations are possible, output any. If there is no *lovely* permutation, output `-1`.
# EXPLANATION:
Let's try to see what happens under various cases, before that remember the three basic properties:
$$
Even+Even = Even \\
Odd+Odd = Even \\
Odd+Even = Odd
$$
$$
and
$$
$$
Even * Even = Even \\
Odd*Odd = Odd \\
Even*Odd = Even
$$
Now let's look the cases we were talking about:
- Odd numbers are less than or equal to $1$.
Let's see what happens when there is no odd number present. Hence in any permutation you arrange the array elements. The consecutive product is gonna be even for all the indexes look at the above property that even multiplied with even gives you an even number. And then the sum of all even numbers is going to be even.
Now when there is only one odd element in the given array. Wherever you place this odd number it's left side element and right side element is going to be even. And as we know even multiplied with odd gives you even number. Again the sum of all even numbers is going to be even.
Hence we can conclude that in such cases it is never possible to achieve the odd sum in whatever permutation we arrange the given array.
- All numbers of the given array are odd.
This is very interesting and simple case as all numbers are odd and hence when the odd number is multiplied with the odd number it produces an odd number. Hence every consecutive product is an odd number. Now the answer depends whether the sum of these consecutive product is even or odd.
- $N$ is Even
There will be $(N-1)$ consecutive products that exist for the the array of length $N$. Hence $(N-1)$ is an odd number. If odd number is added odd times it produces an odd number.
- $N$ is Odd
In this case $(N-1)$ is going to be even. Hence when odd number is added even times it produces an even number.
Hence we can conclude that in such cases whether the sum is going to be odd depends on whether $N$ is even.
- All other cases
Ok, so for all other cases it is always possible to produce an odd number. Let's say the number of odd numbers in the given array be $X$. Now let us try to prove how:
[details = "X is Odd"]
Now we know that these numbers are the only chances for us to get the odd sum in the array. Now if we place all these numbers consecutively then there will be $(X-1)$ consecutive odd products. Since $(X-1)$ is even hence we will end up getting the sum as even.
Let us then do one thing place the $(X-1)$ odd numbers consecutively and the remaining odd number anywhere. Now there will be $(X-2)$ odd consecutive products in the given array which leads us to get the odd sum.
Hence we can say that it is possible to get the odd sum when $X$ is going to be odd.
[/details]
[details="X is Even"]
Very simple if you read the Odd case above. Just place the all these $X$ numbers consecutive and then you know what happens and you will be able to get the odd sum.
[/details]
# TIME COMPLEXITY:
$O(N)$ per test case.
# SOLUTIONS:
[details = "Author"]
for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split()))
odd=[]
even=[]
for i in a:
if(i%2):
odd.append(i)
else:
even.append(i)
if(len(odd)<=1 or (len(odd)==n and n%2)):
print(-1)
else:
if(len(odd)%2):
print(odd.pop(),end=β β)
print(*even,end=β ')
print(*odd)
[/details]
[details = "Tester"]
#include <bits/stdc++.h>
using namespace std;
long long readInt(long long l, long long r, char endd) {
long long x = 0;
int cnt = 0;
int fi = -1;
bool is_neg = false;
while (true) {
char g = getchar();
if (g == β-β) {
assert(fi == -1);
is_neg = true;
continue;
}
if (β0β <= g && g <= β9β) {
x *= 10;
x += g - β0β;
if (cnt == 0) {
fi = g - β0β;
}
cnt++;
assert(fi != 0 || cnt == 1);
assert(fi != 0 || is_neg == false);
assert(!(cnt > 19 || (cnt == 19 && fi > 1)));
}
else if (g == endd) {
assert(cnt > 0);
if (is_neg) {
x = -x;
}
assert(l <= x && x <= r);
return x;
}
else {
assert(false);
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen(βinput.txtβ, βrβ, stdin);
freopen(βoutput.txtβ, βwβ, stdout);
#endif
int t;
t = readInt(1, 1000, β\nβ);
while(tβ){
int n;
n = readInt(2, 500, β\nβ);
int temp;
vector e, o;
for(int i = 0; i < n; i++){
if(i != n - 1){
temp = readInt(1, 1000000, β ');
}else{
temp = readInt(1, 1000000, β\nβ);
}
if(temp % 2){
o.push_back(temp);
}else{
e.push_back(temp);
}
}
if(o.size() == 0){
cout<<-1<<"\n";
}else{
if(o.size() % 2 == 0){
for(int i = 0; i < o.size(); i++){
cout<<o[i]<<" β;
}
for(int i = 0; i < e.size(); i++){
cout<<e[i]<<β β;
}
cout<<β\n";
}else{
if(e.size() == 0 || o.size() == 1){
cout<<-1<<"\n";
}else{
cout<<o[0]<<" β;
for(int i = 0; i < e.size(); i++){
cout<<e[i]<<β β;
}
for(int i = 1; i < o.size(); i++){
cout<<o[i]<<β β;
}
cout<<β\n";
}
}
}
}
return 0;
}
[/details]
[details = "Editorialist"]
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin>>n;
vector<int> even,odd;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(x%2)
odd.push_back(x);
else
even.push_back(x);
}
if (odd.size()<=1 || (odd.size() == n && n%2))
{
cout<<-1<<endl;
return;
}
if(odd.size()%2)
{
cout<<odd[0]<<" ";
odd.erase(odd.begin());
}
for(auto itr: even)
cout<<(itr)<<" ";
for(auto itr: odd)
cout<<(itr)<<" ";
cout<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}
[/details]
[/quote]
can you pls check what i did wrong in my approach..