title: Find The Element Before Which All The Elements Are Smaller Than It And After Which All Are Greater Than It
signature: def findTheElementBeforeWhichAllTheElementsAreSmallerThanItAndAfterWhichAllAreGreaterThanIt(arr: List[Int], n: Int) = {
test_code: import scala.collection.mutable.HashMap
  import scala.collection.immutable.HashSet
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  
  // TESTING CODE 
  // ============================================
  def validateSolution(actual: Int , expected: Int): Boolean = {
      actual.toString == expected.toString
  }
  
  def driver(arr: List[Int], n: Int, expected: Int): String = {
      try {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n),expected)) {
              "PASSED"
          } else{
              "FAILED"
          }
      }catch{
          case e: Exception => {e.getClass.getName}
      }
  
  }
  object QuestionEvaluator extends App {
  
      println(s"TEST-0...${driver(
        List(4, 24, 30, 33, 56, 67, 87, 90), 4, 
        3    
      )}")   
  
  
      println(s"TEST-1...${driver(
        List(72, -48, 12, 4, 46, 36, 2, 58, 82, -88, -14, 56, 90, 76, 18, -6, -28, 18, 88, 90, 40, -68, -10, -82, -28, 16, 32, -90, 12, -86, -16, 78, -98, -52, -26, 80, 6, 50, 40, -12, 52, 38, -92, 94, -32, 14, -80, -88, 48), 28, 
        -1    
      )}")   
  
  
      println(s"TEST-2...${driver(
        List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 30, 
        -1    
      )}")   
  
  
      println(s"TEST-3...${driver(
        List(51, 87, 46, 76, 2, 89, 56, 34, 49, 61, 44, 73, 14, 60, 89), 11, 
        -1    
      )}")   
  
  
      println(s"TEST-4...${driver(
        List(-90, -90, -80, -72, -68, -64, -62, -62, -60, -46, -44, -44, -44, -42, -42, -32, -22, -22, -18, -2, 4, 6, 10, 12, 14, 30, 34, 34, 40, 56, 56, 56, 58, 68, 74, 78, 78, 82, 84, 86, 88, 90, 92), 29, 
        28    
      )}")   
  
  
      println(s"TEST-5...${driver(
        List(0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0), 26, 
        -1    
      )}")   
  
  
      println(s"TEST-6...${driver(
        List(7, 8, 11, 12, 15, 17, 28, 34, 57, 61, 66, 76, 92), 9, 
        8    
      )}")   
  
  
      println(s"TEST-7...${driver(
        List(-22, -96, -78, -60, 34, -18, 86, -42, -78, 76, 8, 28, -80, 80, 6, -72, 34, 66, 84, 50, -4, 18, 72, -66, -68, -24, 56, -12, -70, 24, -82), 19, 
        -1    
      )}")   
  
  
      println(s"TEST-8...${driver(
        List(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1), 9, 
        -1    
      )}")   
  
  
      println(s"TEST-9...${driver(
        List(79, 81, 55, 6, 78, 93, 81, 33, 29, 1, 27, 49, 21, 58, 22, 65, 44, 95, 6, 51, 75, 42, 14, 55, 79, 82, 90, 8, 15, 15, 53, 98, 76, 43, 33), 30, 
        -1    
      )}")   
  
  }
  
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;
  }