title: How To Check If A Given Array Represents A Binary Heap
signature: how_to_check_if_a_given_array_represents_a_binary_heap <- function(arr, i, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, i, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, i, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(1L, 2L, 3L, 5L, 24L, 24L, 25L, 25L, 36L, 37L, 37L, 39L, 42L, 44L, 46L, 50L, 51L, 54L, 56L, 60L, 62L, 70L, 71L, 73L, 75L, 80L, 80L, 85L, 86L, 89L, 91L, 95L, 99L), 0L, 18L, 
          FALSE
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-44L, -58L, 88L, -42L, 42L, -14L, -44L, 42L, 64L, 94L, -46L, -70L, 34L, -10L, -46L, -52L, -6L, -78L, 64L, 56L, 74L, 98L, -34L, -4L, -92L, 6L, -52L, -20L, 78L, -72L, -40L), 0L, 27L, 
          FALSE
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(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, 1L), 0L, 20L, 
          FALSE
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(10L, 2L, 1L, 34L, 21L, 37L, 49L, 31L, 70L, 97L, 87L, 50L, 76L, 55L), 0L, 8L, 
          FALSE
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-94L, -84L, -82L, -66L, -64L, -62L, -56L, -38L, -24L, -24L, -4L, 2L, 4L, 4L, 8L, 14L, 16L, 20L, 30L, 34L, 34L, 48L, 58L, 58L, 70L, 76L, 78L, 86L, 88L, 96L, 98L), 0L, 26L, 
          FALSE
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L), 12L, 11L, 
          TRUE
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(5L, 20L, 30L, 5L, 10L, 21L, 5L), 0L, 7L, 
          FALSE
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(50L, 20L, 30L, 5L, 10L, 21L, 5L), 0L, 7L, 
          TRUE
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(50L, 20L, 30L, 5L, 10L, 21L, 5L), 2L, 7L, 
          TRUE
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(50L, 20L, 30L, 5L, 10L, 21L, 5L), 4L, 7L, 
          TRUE
      )))
  
  }
  
  main()
source_py: def how_to_check_if_a_given_array_represents_a_binary_heap(arr, i, n):
      if (i > int(((n - 2) / 2))):
          return True
      if ((arr[i] >= arr[((2 * i) + 1)]) and (arr[i] >= arr[((2 * i) + 2)]) and how_to_check_if_a_given_array_represents_a_binary_heap(arr, ((2 * i) + 1), n) and how_to_check_if_a_given_array_represents_a_binary_heap(arr, ((2 * i) + 2), n)):
          return True
      return False
source_cpp: bool how_to_check_if_a_given_array_represents_a_binary_heap(vector<int> arr, int i, int n) {
    if ( i > ( n - 2 ) / 2 ) return true;
    if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && how_to_check_if_a_given_array_represents_a_binary_heap ( arr, 2 * i + 1, n ) && how_to_check_if_a_given_array_represents_a_binary_heap ( arr, 2 * i + 2, n ) ) return true;
    return false;
  }