Uber-India | OA | Peak Array Variation
Anonymous User
732

Recently i appeared for OA for Uber india.There was one question which was variation of peak array problem. My code was only passing the sample test cases and was failing on all hidden test cases. Can you guys please help me with brute and optimal solutions and approaches.image
image
image

Heres my Code-:

    public int cost(int[] arr){	
   	int[] b=arr.clone();
   	Arrays.sort(b);
   	if(Arrays.equals(arr,b)) return 0;
   	int n=arr.length;
   	int cnt=0;
   	int i=1;
   	while(i<n/2){
   	   if(arr[i]<arr[i-1]){
   	       swap(arr,i,i-1);
   	       cnt++;
   	       i=0;
   	   } 
   	   i++;
   	}
   	int j=n/2+1;
   	while(j<n){
   	    if(arr[j]>arr[j-1]){
   	        swap(arr,j,j-1);
   	        cnt++;
   	        j=n/2+1;
   	    }
   	    j++;
   	}
   	return cnt;
   	}
Comments (2)