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
+1
ajaypal1920023 weeks ago
// C++ solution
int findElement(int a[], int n)
{
    int l[n];
    int r[n];

    int maxi = a[0];
    for (int i = 0; i < n; i++)
    {
        maxi = max(maxi, a[i]);
        l[i] = maxi;
    }

    int mini = a[n - 1];
    for (int i = n - 1; i >= 0; i--)
    {

        mini = min(mini, a[i]);
        r[i] = mini;
    }

    for (int i = 1; i < n - 1; i++)
    {
        if (a[i] >= l[i - 1] and a[i] <= r[i + 1])
        {
            return a[i];
        }
    }

    return -1;
}
0
jayesh293 weeks ago
JAVA 0.64 seconds -
    public int findElement(int arr[], int n){
        int big = arr[0], small = arr[n-1],ele=-1;
        int[] max = new int[n];
        int[] min = new int[n];
        max[0]=big;min[n-1]=small;
        int lo = 1,hi=n-2;
        while(lo<n){
            if(arr[lo]>big){
                big = arr[lo];
            }
            max[lo++]=big;
            if(arr[hi]<small){
                small=arr[hi];
            }
            min[hi--]=small;
        }
    for(int i=1;i<n-1;i++){
        if(max[i]==min[i]){
            ele = arr[i];
            break;
        }
    }
    return ele;        
    }
0
swapniltayal4221 month ago
int findElement(int arr[], int n) {      int maxi = INT_MIN;   int mini = INT_MAX;      int lrg[n];   int sml[n];      for (int i=0; i<n; i++){       maxi = max(arr[i], maxi);       lrg[i] = maxi;   }   for (int i=n-1; i>=0; i--){       mini = min(arr[i], mini);       sml[i] = mini;   }   for (int i=1; i<n-1; i++){       if (arr[i] >= lrg[i-1] && arr[i] <= sml[i+1]){           return arr[i];       }   }return -1;   }
0
pratikbhankhodiya1 month ago
int findElement(int arr[], int n) {
    vector<int> left(n),right(n);
    for(int i=0,maxi=INT_MIN,j=n-1,mini=INT_MAX;i<n;i++,j--){
        maxi=max(maxi,arr[i]);
        left[i]=maxi;
        mini=min(mini,arr[j]);
        right[j]=mini;
    }
    for(int i=1;i<n-1;i++){
        if(left[i]<=arr[i] and right[i]>=arr[i]) return arr[i];
    }
    return -1;
}
-1
hargunsinghsahni2 months ago
int findElement(int arr[], int n) {
    int l[n],r[n];
    int mx=-1;
    for(int i=0;i<n;i++)
    {
     if(arr[i]>mx)
      mx=arr[i];
     l[i]=mx; 
    }
    mx=INT_MAX;
    for(int i=n-1;i>=0;i--)
    {
     if(arr[i]<mx)
      mx=arr[i];
     r[i]=mx; 
    }
    int ans=-1;
    for(int i=0;i<n;i++)
     if(arr[i]>=l[i] && arr[i]<=r[i] && i!=0 && i!=n-1 && ans==-1)
      ans=arr[i];
    return ans;  
}
0
pawan kumar 332 months ago
// Working Code:-
 
int findElement(int arr[], int n) {      int left[n],right[n];   left[0] = -1;   right[n-1] = -1;   int max = arr[0];   int min = arr[n-1];   for(int i=1;i<n;i++)   {       left[i] = max;       if(arr[i] > max)           max = arr[i];   }   for(int i=n-2;i>=0;i--)   {       right[i] = min;       if(arr[i] < min)           min = arr[i];   }   for(int i=1;i<n-1;i++)   {       if(left[i]<=arr[i] && right[i]>=arr[i])           return arr[i];   }   return -1;      }
+3
vaibhav motwani2 months ago
// Shortest solution with least conditionsint findElement(int arr[], int n) {   int curr_ans=arr[0], return_value=-1;   for(int i=0;i<n;i++){       if(arr[i]>=curr_ans && return_value == -1 && i!=0 && i!= n-1){           curr_ans = arr[i];           return_value = curr_ans;       } else if(arr[i]<curr_ans){           return_value = -1;       }   }   return return_value;}
0
gupta2411sumit2 months ago
// Easy and fast c++ solution
 
int findElement(int arr[], int n) {
    int left[n] ;   int right[n] ;      int mx = arr[0] ;   int mn = arr[n-1] ;   left[0] = 0 ;   left[n-1] = 0 ;   right[0] = 0 ;   right[n-1] = 0 ;         for( int i = 1 ; i<n-1 ; i++)   {       if(arr[i]<mx)       {           left[i] = 0 ;                  }       else{           left[i] = 1 ;           mx = arr[i]  ;       }   }      for( int i = n-2 ; i>0 ; i--)   {       if(arr[i]>mn)       {           right[i] = 0 ;                           }       else{           right[i] = 1 ;            mn = arr[i]  ;                  }   }         for( int i = 0 ; i<n  ; i++)   {       if( left[i]==1 && right[i]==1)       {           return arr[i] ;       }      }      return -1 ;}
0
aarthinarayanan2 months ago
int findElement(int arr[], int n) {      int max_left[n],min_right[n];   max_left[0]=INT_MIN;   min_right[n-1]=INT_MAX;   for(int i=1;i<n;i++)   {       if(arr[i-1]>max_left[i-1])       max_left[i]=arr[i-1];       else       max_left[i]=max_left[i-1];   }   for(int i=n-2;i>=0;i--)   {       if(arr[i+1]<min_right[i+1])       min_right[i]=arr[i+1];       else       min_right[i]=min_right[i+1];   }   for(int i=1;i<n-1;i++)   if(arr[i]>=max_left[i] && arr[i]<=min_right[i])   return arr[i];   return -1;}
0
sadhvik03033 months ago
python 3:
def findElement(a, n):   ele=-1   big=a[0]   small=a[len(a)-1]   _max=list(range(n+1))   _min=list(range(n+1))   for i in range(0,len(a)):       if(a[i]>big):           big=a[i]       _max[i]=big   for j in range(len(a)-1,-1,-1):       if(a[j]<small):           small=a[j]       _min[j]=small   for i in range(0,len(a)):       if (i!=0 and i!=len(a)-1):           if(_min[i]==_max[i]):               ele=_min[i]               break   return ele 
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.