C++ solution having n^2 time complexity

flipkart
Tags: #<Tag:0x00007f18265d0008>

#1

int Solution::perfectPeak(vector &arr) {

int n = arr.size();
bool flag ;

for(int i=1; i<n-1; i++) {
    
    flag=true ;
    int k=i;
    int j=i;
    
    while(true) {
        
        if(j>0)
            j-- ;
        
        if(k<n-1)
            k++ ;
        
        if(arr[i]<=arr[j] || arr[i]>=arr[k]) {
            
            flag=false;
            break;
        }
        
        if(k==n-1 && j==0)
            break ;
    }
    
    if(flag==true)
        return 1 ;
}

return 0 ;
}