Skip to content

Commit 251c01e

Browse files
Added min-operations-to-make-array-palindrome
1 parent def8905 commit 251c01e

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int mod=1e9+7;
5+
#define F(a,b,var) for(int var=a;var<b;var++)
6+
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
7+
8+
int minOpsToMakePalindrome(vector<int> test_palin){
9+
int i = 0, j = test_palin.size() - 1;
10+
int counts = 0;
11+
while(i <= j){
12+
if(test_palin[i] == test_palin[j]){
13+
i += 1;
14+
j -= 1;
15+
}
16+
17+
else if(test_palin[i] > test_palin[j]){
18+
j -= 1;
19+
test_palin[j] += test_palin[j + 1];
20+
counts += 1;
21+
}
22+
else{
23+
i += 1;
24+
test_palin[i] += test_palin[i - 1];
25+
counts += 1;
26+
}
27+
}
28+
//cout<<counts<<"\n";
29+
return counts;
30+
}
31+
32+
int main() {
33+
34+
//code
35+
36+
FAST_INP;
37+
int T;
38+
cin>>T;
39+
while(T--)
40+
{
41+
int n, i, curr;
42+
cin>>n;
43+
vector<int> test_palin;
44+
45+
for(int i=0; i<n; i++){
46+
cin>>curr;
47+
test_palin.push_back(curr);
48+
}
49+
cout<<minOpsToMakePalindrome(test_palin)<<endl;
50+
}
51+
return 0;
52+
}

0 commit comments

Comments
 (0)