title: Longest Subarray Count 1S One Count 0S
signature: longest_subarray_count_1s_one_count_0s <- 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(6L, 10L, 31L, 35L), 2L, 
          1L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(4L, 88L, -72L, 28L, -54L, 32L, -34L), 6L, 
          5L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), 4L, 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(39L, 22L, 15L, 10L, 34L, 87L, 46L, 83L, 74L, 77L, 61L, 90L, 43L, 67L, 64L, 72L, 92L, 52L, 68L, 53L, 67L, 32L, 82L, 76L, 76L, 47L, 74L, 47L, 8L, 80L, 85L, 58L, 75L), 26L, 
          9L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-82L, -58L, -50L, -30L, -14L, -4L, -2L, -2L, 0L, 22L, 36L, 58L, 70L, 80L, 80L, 82L, 84L, 90L), 14L, 
          11L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L), 7L, 
          7L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(4L, 11L, 17L, 21L, 21L, 22L, 30L, 31L, 36L, 37L, 39L, 40L, 45L, 46L, 47L, 48L, 52L, 53L, 53L, 60L, 60L, 65L, 66L, 66L, 67L, 67L, 87L, 88L, 91L, 97L), 29L, 
          9L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-4L, -60L, -78L, -50L, 64L, 18L, 0L, 76L, 12L, 86L, -22L), 7L, 
          6L
      )))
  
      cat(sprintf("TEST-8...%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), 17L, 
          3L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(4L, 39L, 50L, 37L, 71L, 66L, 55L, 34L, 1L, 41L, 34L, 99L, 69L, 31L), 11L, 
          9L
      )))
  
  }
  
  main()
source_py: def longest_subarray_count_1s_one_count_0s(arr, n):
      um = {i: 0 for i in range(10)}
      sum = 0
      maxLen = 0
      for i in range(n):
          if (arr[i] == 0):
              sum += (- 1)
          else:
              sum += 1
          if (sum == 1):
              maxLen = (i + 1)
          elif (sum not in um):
              um[sum] = i
          if ((sum - 1) in um):
              if (maxLen < (i - um[(sum - 1)])):
                  maxLen = (i - um[(sum - 1)])
      return maxLen
source_cpp: int longest_subarray_count_1s_one_count_0s(vector<int> arr, int n) {
    unordered_map < int, int > um;
    int sum = 0, maxLen = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      sum += arr [ i ] == 0 ? - 1 : 1;
      if ( sum == 1 ) maxLen = i + 1;
      else if ( um . find ( sum ) == um . end ( ) ) um [ sum ] = i;
      if ( um . find ( sum - 1 ) != um . end ( ) ) {
        if ( maxLen < ( i - um [ sum - 1 ] ) ) maxLen = i - um [ sum - 1 ];
      }
    }
    return maxLen;
  }