title: Find The Element Before Which All The Elements Are Smaller Than It And After Which All Are Greater Than It
signature: find_the_element_before_which_all_the_elements_are_smaller_than_it_and_after_which_all_are_greater_than_it <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(4L, 24L, 30L, 33L, 56L, 67L, 87L, 90L), 4L, 
          3L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(72L, -48L, 12L, 4L, 46L, 36L, 2L, 58L, 82L, -88L, -14L, 56L, 90L, 76L, 18L, -6L, -28L, 18L, 88L, 90L, 40L, -68L, -10L, -82L, -28L, 16L, 32L, -90L, 12L, -86L, -16L, 78L, -98L, -52L, -26L, 80L, 6L, 50L, 40L, -12L, 52L, 38L, -92L, 94L, -32L, 14L, -80L, -88L, 48L), 28L, 
          -1L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 30L, 
          -1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(51L, 87L, 46L, 76L, 2L, 89L, 56L, 34L, 49L, 61L, 44L, 73L, 14L, 60L, 89L), 11L, 
          -1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-90L, -90L, -80L, -72L, -68L, -64L, -62L, -62L, -60L, -46L, -44L, -44L, -44L, -42L, -42L, -32L, -22L, -22L, -18L, -2L, 4L, 6L, 10L, 12L, 14L, 30L, 34L, 34L, 40L, 56L, 56L, 56L, 58L, 68L, 74L, 78L, 78L, 82L, 84L, 86L, 88L, 90L, 92L), 29L, 
          28L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L), 26L, 
          -1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(7L, 8L, 11L, 12L, 15L, 17L, 28L, 34L, 57L, 61L, 66L, 76L, 92L), 9L, 
          8L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-22L, -96L, -78L, -60L, 34L, -18L, 86L, -42L, -78L, 76L, 8L, 28L, -80L, 80L, 6L, -72L, 34L, 66L, 84L, 50L, -4L, 18L, 72L, -66L, -68L, -24L, 56L, -12L, -70L, 24L, -82L), 19L, 
          -1L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), 9L, 
          -1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(79L, 81L, 55L, 6L, 78L, 93L, 81L, 33L, 29L, 1L, 27L, 49L, 21L, 58L, 22L, 65L, 44L, 95L, 6L, 51L, 75L, 42L, 14L, 55L, 79L, 82L, 90L, 8L, 15L, 15L, 53L, 98L, 76L, 43L, 33L), 30L, 
          -1L
      )))
  
  }
  
  main()
source_py: def find_the_element_before_which_all_the_elements_are_smaller_than_it_and_after_which_all_are_greater_than_it(arr, n):
      leftMax = ([None] * n)
      leftMax[0] = float('-inf')
      for i in range(1, n):
          leftMax[i] = max(leftMax[(i - 1)], arr[(i - 1)])
      rightMin = float('inf')
      for i in range((n - 1), (- 1), (- 1)):
          if ((leftMax[i] < arr[i]) and (rightMin > arr[i])):
              return i
          rightMin = min(rightMin, arr[i])
      return (- 1)
source_cpp: int find_the_element_before_which_all_the_elements_are_smaller_than_it_and_after_which_all_are_greater_than_it(vector<int> arr, int n) {
    int leftMax [ n ];
    leftMax [ 0 ] = INT_MIN;
    for ( int i = 1;
    i < n;
    i ++ ) leftMax [ i ] = max ( leftMax [ i - 1 ], arr [ i - 1 ] );
    int rightMin = INT_MAX;
    for ( int i = n - 1;
    i >= 0;
    i -- ) {
      if ( leftMax [ i ] < arr [ i ] && rightMin > arr [ i ] ) return i;
      rightMin = min ( rightMin, arr [ i ] );
    }
    return - 1;
  }