-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB. Balanced Array.cpp
More file actions
48 lines (37 loc) · 954 Bytes
/
B. Balanced Array.cpp
File metadata and controls
48 lines (37 loc) · 954 Bytes
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
48
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
// If n is not divisible by 4, it's impossible to form the required array
if (n % 4 != 0) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
int arr[n];
int even = 2;
int odd = 1;
int evenSum=0;
for (int i = 0; i < n / 2; i++) {
arr[i] = even;
evenSum+=even;
even += 2;
}
int oddSum=0;
for (int i = n / 2; i < n - 1; i++) {
arr[i] = odd;
oddSum+=odd;
odd += 2;
}
arr[n - 1] = evenSum-oddSum;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
return 0;
}