# Why is this easy alternative approach throwing an WA? (Fixed)

For this problem:
DEPCHEF

The solution code given is to check the values which satisfy D[i] - (A[i+1] + A[i-1]) > 0 and then store the max(ans,D[i]) as the solution.
For example: This code is acceptable and gives AC on submission -:

``````#include <bits/stdc++.h>

using namespace std;

int main(){
long long t;
cin >> t;
while (t--){
int n;
cin >> n;
vector<int> v1;
vector<int> v2;
v2.push_back(-1);
v1.push_back(-1);
for (int i = 0;i<n;i++){
int temp;
cin>> temp;
v1.push_back(temp);
}

int t1 = v1[1];
int t2 = v1[n];
v1.push_back(t1);
v1[0] = t2;
for (int i = 0;i<n;i++){
int temp;
cin >> temp;
v2.push_back(temp);
}
v2.push_back(t2);

int msum = -1;
for (int i = 1;i<=n;i++){
int csum = v2[i] - (v1[i+1]+v1[i-1]);
if (csum > 0){
msum = max(msum,v2[i]);
}
}
cout << msum << endl;
}
}
``````

Here v1,v2 are arrays A and D respectively according to the question.
The approach is clear, to store the max value of the expression csum (and print -1 if there is none).
However when I applied a somewhat similar approach, I got an WA -:

``````#include <bits/stdc++.h>

using namespace std;

int main(){
long long t;
cin >> t;
while (t--){
int n;
cin >> n;
vector<int> v1;
vector<int> v2;
v2.push_back(-1);
v1.push_back(-1);
for (int i = 0;i<n;i++){
int temp;
cin>> temp;
v1.push_back(temp);
}

int t1 = v1[1];
int t2 = v1[n];
v1.push_back(t1);
v1[0] = t2;
for (int i = 0;i<n;i++){
int temp;
cin >> temp;
v2.push_back(temp);
}
v2.push_back(t2);

pair<int,int> msum = {0,-1};
for (int i = 1;i<=n;i++){
int csum = v2[i] - (v1[i+1]+v1[i-1]);
if (csum > msum.first){
msum.first = csum;
msum.second = v2[i];
}
}
cout << msum.second << endl;
}
}
``````

In this approach, I have created a pair<int,int> msum in which the first index stores 0 (initially) and then D[i] in the second, I think this should eliminate the need of additionally checking if csum is greater than 0 by directly comparing it with the max value of the expression (msum.first). I donâ€™t know which type of testcase might be failing the solution, if anyone has an idea I would be really grateful

Thanks!

EDIT: I got the answer by thinking bit more about it, the comparison is necessary with 0. This alternative approach is not correct because we are comparing the difference which of the expression (instead we have to compare the D[i]).