Why should this fail and for what test cases specifically


#1

vector left(A.size()),right(A.size());
left[0] = A[0]; right[A.size()-1] = A[A.size()-1];
for(int i = 1; i <A.size();i++)
{
left[i] = max(left[i-1],A[i]);
}
for(int i = A.size()-2; i>=0; i–)
{
right[i] = min(right[i+1],A[i]);
}
for(int i = 1; i<A.size()-1; i++)
{
if(A[i]==left[i] and A[i]==right[i]) (shouldn’t this be true and if not where does this fail)
{
return 1;
}
}
return 0;


#2

//this one is the correct code
public int perfectPeak(int[] A) {
int n = A.length;
if(n<3)
return 0;
int left [] = new int[n];
int right[] = new int[n];
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int i=0;i<n;i++){
left[i] = max;
max= Math.max(A[i],max);
}

    for(int i = n-1;i>=0;i--){
       right[i]= min;
       min= Math.min(A[i],min);
    }
    
    for(int i=1;i<=n-2;i++){
        if(A[i]>left[i] && A[i]<right[i]){
            return 1;
        }
    }
    return 0;
}

#3

int Solution::perfectPeak(vector &A) {
int n=A.size();
int ma[n],mi[n];
ma[0]=A[0],mi[n-1]=A[n-1];
for(int i=1;i<n-1;i++)ma[i]=max(ma[i-1],A[i]);
for(int i=n-2;i>0;i–)mi[i]=min(mi[i+1],A[i]);
for(int i=1;i<n-1;i++)if(ma[i-1]<A[i]&&A[i]<mi[i+1])return 1;
return 0;
}