title: How To Check If A Given Array Represents A Binary Heap 1
signature: function how_to_check_if_a_given_array_represents_a_binary_heap_1(arr, n)
source_py: def how_to_check_if_a_given_array_represents_a_binary_heap_1(arr, n):
      for i in range((int(((n - 2) / 2)) + 1)):
          if (arr[((2 * i) + 1)] > arr[i]):
              return False
          if ((((2 * i) + 2) < n) and (arr[((2 * i) + 2)] > arr[i])):
              return False
      return True
source_cpp: bool how_to_check_if_a_given_array_represents_a_binary_heap_1(vector<int> arr, int n) {
    for ( int i = 0;
    i <= ( n - 2 ) / 2;
    i ++ ) {
      if ( arr [ 2 * i + 1 ] > arr [ i ] ) return false;
      if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) return false;
    }
    return true;
  }
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({2, 2, 2, 7, 10, 14, 24, 38, 42, 50, 59, 60, 72, 73, 79, 83, 89}, 9, false)
      print(string.format("TEST-0...%s",result))
  
      result = driver({-48, 98, 96, -56, -2, 58, 52, -50, 58, 50, 62, 86, -26, -98, 34, 20, -28, 56, -36}, 9, false)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 20, false)
      print(string.format("TEST-2...%s",result))
  
      result = driver({91, 50, 37}, 2, true)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-80, -26, 0, 0, 6, 14, 22, 76, 82, 88, 98}, 10, false)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 0}, 1, true)
      print(string.format("TEST-5...%s",result))
  
      result = driver({9, 24, 24, 33, 48, 50, 55, 61, 69, 79, 83}, 8, false)
      print(string.format("TEST-6...%s",result))
  
      result = driver({30, 32, -82, -48, 88, -24, 74, 2, 90, 98, 68, 82, 32, -60, 2, -94, 18, 14, 46, 50, -60, -74, -76, 66, -76, -34, -20, 82, -44, -62, 34, 48, -56, 2, 64, -78, -64, 98, -10, -28, 78, -42}, 27, false)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 6, false)
      print(string.format("TEST-8...%s",result))
  
      result = driver({38, 74, 99, 75, 44, 75, 24, 70, 78, 74, 60, 59, 34, 27, 7, 23, 19, 95, 4, 35, 38, 22, 46, 1, 44, 20, 52, 1, 96, 57, 5, 76, 49, 1, 37, 35}, 31, false)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": false, "inputs": {"arr": [2, 2, 2, 7, 10, 14, 24, 38, 42, 50, 59, 60, 72, 73, 79, 83, 89], "n": 9}}, {"idx": 1, "outputs": false, "inputs": {"arr": [-48, 98, 96, -56, -2, 58, 52, -50, 58, 50, 62, 86, -26, -98, 34, 20, -28, 56, -36], "n": 9}}, {"idx": 2, "outputs": false, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 20}}, {"idx": 3, "outputs": true, "inputs": {"arr": [91, 50, 37], "n": 2}}, {"idx": 4, "outputs": false, "inputs": {"arr": [-80, -26, 0, 0, 6, 14, 22, 76, 82, 88, 98], "n": 10}}, {"idx": 5, "outputs": true, "inputs": {"arr": [1, 0], "n": 1}}, {"idx": 6, "outputs": false, "inputs": {"arr": [9, 24, 24, 33, 48, 50, 55, 61, 69, 79, 83], "n": 8}}, {"idx": 7, "outputs": false, "inputs": {"arr": [30, 32, -82, -48, 88, -24, 74, 2, 90, 98, 68, 82, 32, -60, 2, -94, 18, 14, 46, 50, -60, -74, -76, 66, -76, -34, -20, 82, -44, -62, 34, 48, -56, 2, 64, -78, -64, 98, -10, -28, 78, -42], "n": 27}}, {"idx": 8, "outputs": false, "inputs": {"arr": [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 6}}, {"idx": 9, "outputs": false, "inputs": {"arr": [38, 74, 99, 75, 44, 75, 24, 70, 78, 74, 60, 59, 34, 27, 7, 23, 19, 95, 4, 35, 38, 22, 46, 1, 44, 20, 52, 1, 96, 57, 5, 76, 49, 1, 37, 35], "n": 31}}]
question_info.extension: lua
question_info.entry_fn_name: how_to_check_if_a_given_array_represents_a_binary_heap_1
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__