-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd_Divide_and_Floor.cpp
More file actions
47 lines (40 loc) · 1.03 KB
/
Add_Divide_and_Floor.cpp
File metadata and controls
47 lines (40 loc) · 1.03 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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll testcases;
cin >> testcases;
while (testcases--) {
ll n;
cin >> n;
vector<ll> a(n);
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
if (n == 1) {
cout << 0 << endl;
continue;
}
vector<ll> seq; // To store operations
while (a[0] != a[n - 1]){
if ((a[0] & 1) && (a[n - 1] % 2 == 0)){
seq.push_back(1); // When first element is odd and last is even
a[0] = (a[0] + 1) / 2;
a[n - 1] = (a[n - 1] + 1) / 2;
}
else{
seq.push_back(0);
a[0] = (a[0]) / 2;
a[n - 1] = (a[n - 1]) / 2;
}
}
cout << seq.size() << endl;
if (seq.size() <= n) {
for (ll i = 0; i < seq.size(); i++) {
cout << seq[i] << " ";
}
cout << endl;
}
}
return 0;
}