My issue
What I am doing wrong here for first 2 test lines it works correct then for others it failed , could anyone please let me know where I got wrong ?
My code
#include <bits/stdc++.h>
using namespace std;
bool allZero(vector<int>& arr) {
for(int x : arr) if(x != 0) return false;
return true;
}
int helper(int N, int second, vector<int>&arr) {
while(!allZero(arr)){
for(int i=0; i<N-1; i++) {
if(arr[i] > arr[i+1]) {
arr[i]--;
arr[i+1]++;
second++;
}
else if(arr[i] < arr[i+1]) {
arr[i]++;
arr[i+1]--;
second++;
}
}
}
return second;
}
int main() {
int t;
cin >> t;
while(t--) {
int N;
cin >> N;
int second = 0;
vector<int> arr(N);
for(int i=0; i<N; i++) {
cin >> arr[i];
}
cout << helper(N, second, arr) << endl;
}
return 0;
}
Problem Link: Temperature Balance Practice Coding Problem