Element with left side smaller and right side greater | Practice | GeeksforGeeks

Given an unsorted array of size N. Find the first element in array such that all of its left elements are smaller and all right elements to it are greater than it.
Note: Left and right side elements can be equal to required element. And extreme elements cannot be required element.
 
Example 1:
Input:
N = 4
A[] = {4, 2, 5, 7}
Output:
5
Explanation:
Elements on left of 5 are smaller than 5
and on right of it are greater than 5.
 
Example 2:
Input:
N = 3
A[] = {11, 9, 12}
Output:
-1
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function findElement() which takes the array A[] and its size N as inputs and returns the required element. If no such element present in array then return -1.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
3 <= N <= 106
1 <= A[i] <= 106
0
hiltonsamuel19982 days ago
Python Solution: 
def findElement( arr, n):    flag=0    m=arr[0]    if arr==sorted(arr):        return arr[1] 
#This loop gets max element in array excluding a[n-1]   for i in range(n-1):        if m<=arr[i]:            m=arr[i]            flag=i #getting index of max element
#This loop checks if anything is less thaan right side of max element   for i in range(flag,n):        if m>arr[i]:            return -1   if flag!=0:        return m    return -1
0
aviralkumar19045 days ago
int findElement(int a[], int n) {      int maxl[n];   maxl[0]=a[0];   for(int i=1;i<n;i++)   {       maxl[i]=max(maxl[i-1],a[i]);   }   int minr[n];   minr[n-1]=a[n-1];   for(int i=n-2;i>=0;i--)   {       minr[i]=min(minr[i+1],a[i]);   }      for(int i=0;i<n;i++)   {           if(i==0 || i==n-1)           continue;       if(maxl[i]==a[i] && minr[i]==a[i])       {                      return a[i];       }   }      return -1;}
 
0
krishnendughosh1 week ago
Java Solution : 0.63/1.78
tc : O(n)
sc : O(n)
 
class Compute {
    public int findElement(int arr[], int n){
        int left[] = new int[n];
        int right[] = new int[n];
        left[0] = -1;
        right[n-1] = -1;
        int max = arr[0], min = arr[n-1], res = -1;
        for(int i = 1; i<n; i++){
            if(max>arr[i]) left[i] = -1;
            else{
                max = arr[i];
                left[i] = arr[i];
            }
        }
        
        for(int i = n-2; i>=0; i--){
            if(arr[i]<=min){
                min = arr[i];
                right[i] = arr[i];
            }else right[i] = -1;
        }
        
        for(int i = 0; i<n; i++){
            if(left[i]!=-1 && right[i]!=-1) {
                res = arr[i];
                break;
            }
        }
        
        return res;
    }
}
 
0
sulabhsoni2 weeks ago
/* 2 for loop easy java solution , al test case passed, Time taken : 0.63/1.78 */
public int findElement(int arr[], int n){       if(n < 3){          return -1;        }       int[] temp = new int[n];              int min= arr[n-1];       temp[n-1] = min;       for(int i = n-2; i >= 0 ; i--)       {           if(arr[i] < min) {               min = arr[i];           }           temp[i] = min;       }              int max = arr[0];       for(int i = 1 ; i < n-1 ; i++) {           if(arr[i] >= max && (temp[i]) >= arr[i])           {               return arr[i];           }           else if(arr[i] > max){               max = arr[i];           }       }       return -1;   }
0
traila2581 month ago
Very simple python solution
 

def findElement( arr, n):
    l,h = arr[0]
,arr[n-1]
    ans = -1
    st=0
    i=1
    while(i<n-1):
        if  ans == -1:
            if arr[i]>= l and arr[i]<=arr[i+1]:
                ans = arr[i]
                st = i
        l = max(arr[i],l)
    
        if ans != -1:
            if arr[i] < ans or arr[i+1] < ans:
                i = st+1
                ans = -1
   

        i = i+1
    return ans
0
looneydreamers22511 month ago
Time Taken : 0.58/1.78 s
All test cases passed....
 
 public int findElement(int arr[], int n)   {       int[] max = new int[n];          int[] min = new int[n];       max[0]=arr[0];       for(int i=1;i<n;i++)       {           if(arr[i]>max[i-1])               max[i]=arr[i];           else               max[i]=max[i-1];       }       min[n-1]=arr[n-1];       for(int i=n-2;i>=0;i--)       {           if(arr[i]<min[i+1])               min[i]=arr[i];           else               min[i]=min[i+1];       }       for(int i=1;i<n-1;i++)             if(arr[i]==max[i] && arr[i]==min[i])                  return arr[i];            return -1;       }
0
prachiacharya20012 months ago
int findElement(int arr[], int n) {   int maxarr[n];   int minarr[n];   maxarr[0]=arr[0];   minarr[n-1]=arr[n-1];   for(int i=1;i<n;i++){       maxarr[i]=max(maxarr[i-1],arr[i]);       minarr[n-1-i]=min(minarr[n-i],arr[n-i-1]);   }   for(int i=0;i<n-1;i++){       if(arr[i]>=maxarr[i-1]&&arr[i]<=minarr[i+1]){           return arr[i];       }   }   return -1;}
0
navamshuram20022 months ago
def findElement(arr,n):   minele = [None] * n   maxele = [None] * n      ele = -1   big = arr[0]   small = arr[n-1]      for i in range (1,n-1):       if (arr[i]>big):           big = arr[i]       maxele[i] = big   for i in range (n-1,-1,-1):       if (arr[i]<small):           small = arr[i]       minele[i] = small          for i in range (1,n-1):       if (minele[i]==maxele[i]):           ele = minele[i]           break   return ele
0
harshjha51220012 months ago
int findElement(int a[], int n) {   int maxe[n];   int mini[n];   maxe[0] = a[0];   mini[n-1] = a[n-1];   for(int i=1;i<n;i++)   {       maxe[i] = max(maxe[i-1],a[i]);       mini[n-1-i] = min(mini[n-i] , a[n-1-i]);          }   for(int i=0;i<n;i++)   {       maxe[i] = maxe[i] - mini[i];   }   int i = 1;     while(i<n)   {       if(maxe[i] == 0 && i<(n-1))       {                      return a[i];                             }       i++;   }   return -1;
0
akshaypatade2 months ago
Is anybody getting a time out error for test case no 1093.
 
Please find my code below
 
class Compute {   public int findElement(int arr[], int n){              if(arr.length <= 2)           return -1;
              HashMap<Integer,ArrayList<Integer>> map = new HashMap<>();              int max = arr[0];       for(int i = 1; i < arr.length - 1; i++) {                      ArrayList<Integer> l = new ArrayList<>();           l.add(max);           map.put(i,l);           max = Math.max(arr[i], max);       }              int min = arr[arr.length - 1];       for(int i = arr.length - 2; i > 0; i--) {                      //System.out.println(i);           ArrayList<Integer> l = map.get(i);           l.add(min);           map.put(i, l);           min = Math.min(arr[i], min);       }              for(int i = 1; i < arr.length - 1; i++) {                      if(arr[i] >= map.get(i).get(0) && arr[i] <= map.get(i).get(1))               return arr[i];       }              return -1;   }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.