title: Find The Element Before Which All The Elements Are Smaller Than It And After Which All Are Greater Than It
signature: function find_the_element_before_which_all_the_elements_are_smaller_than_it_and_after_which_all_are_greater_than_it(arr, n)
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;
  }
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(arr, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({4, 24, 30, 33, 56, 67, 87, 90}, 4, 3)
      print(string.format("TEST-0...%s",result))
  
      result = driver({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)
      print(string.format("TEST-1...%s",result))
  
      result = driver({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)
      print(string.format("TEST-2...%s",result))
  
      result = driver({51, 87, 46, 76, 2, 89, 56, 34, 49, 61, 44, 73, 14, 60, 89}, 11, -1)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-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)
      print(string.format("TEST-4...%s",result))
  
      result = driver({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)
      print(string.format("TEST-5...%s",result))
  
      result = driver({7, 8, 11, 12, 15, 17, 28, 34, 57, 61, 66, 76, 92}, 9, 8)
      print(string.format("TEST-6...%s",result))
  
      result = driver({-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)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, 9, -1)
      print(string.format("TEST-8...%s",result))
  
      result = driver({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)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 3, "inputs": {"arr": [4, 24, 30, 33, 56, 67, 87, 90], "n": 4}}, {"idx": 1, "outputs": -1, "inputs": {"arr": [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], "n": 28}}, {"idx": 2, "outputs": -1, "inputs": {"arr": [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], "n": 30}}, {"idx": 3, "outputs": -1, "inputs": {"arr": [51, 87, 46, 76, 2, 89, 56, 34, 49, 61, 44, 73, 14, 60, 89], "n": 11}}, {"idx": 4, "outputs": 28, "inputs": {"arr": [-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], "n": 29}}, {"idx": 5, "outputs": -1, "inputs": {"arr": [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], "n": 26}}, {"idx": 6, "outputs": 8, "inputs": {"arr": [7, 8, 11, 12, 15, 17, 28, 34, 57, 61, 66, 76, 92], "n": 9}}, {"idx": 7, "outputs": -1, "inputs": {"arr": [-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], "n": 19}}, {"idx": 8, "outputs": -1, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], "n": 9}}, {"idx": 9, "outputs": -1, "inputs": {"arr": [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], "n": 30}}]
question_info.extension: lua
question_info.entry_fn_name: find_the_element_before_which_all_the_elements_are_smaller_than_it_and_after_which_all_are_greater_than_it
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__