-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanced_Array.cpp
More file actions
47 lines (40 loc) · 1.09 KB
/
Balanced_Array.cpp
File metadata and controls
47 lines (40 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// we have (even number) n
// create an array of length n where first n/2 elements are even
// next n/2 elements are odd
// all are distinct and positive
// sum of 1st half = sum of 2nd half
typedef long long ll;
#include<bits/stdc++.h>
using namespace std;
int main(){
ll testcases = 0;
cin >> testcases;
for(ll testcase = 0; testcase < testcases; testcase++){
ll n;
cin >> n;
if(n % 4 != 0){
cout << "NO" << endl;
}
else{
cout << "YES" << endl;
ll odds = 1, sum_odd = 0;
ll evens = 2, sum_even = 0;
vector<ll> a(n);
for(ll i = 0; i < n / 2; i++){
a[i] = evens;
sum_even += a[i];
evens += 2;
}
for(ll i = n / 2 + 1; i < n; i++){
a[i] = odds;
sum_odd += a[i];
odds += 2;
}
a[n / 2] += sum_even - sum_odd;
for(ll num : a){
cout << num << " ";
}
cout << endl;
}
}
}