title: Longest Subarray Count 1S One Count 0S
signature: def longestSubarrayCount1SOneCount0S(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(6, 10, 31, 35), 2, 
        1    
      )}")   
  
  
      println(s"TEST-1...${driver(
        List(4, 88, -72, 28, -54, 32, -34), 6, 
        5    
      )}")   
  
  
      println(s"TEST-2...${driver(
        List(0, 0, 0, 1, 1, 1, 1, 1, 1), 4, 
        1    
      )}")   
  
  
      println(s"TEST-3...${driver(
        List(39, 22, 15, 10, 34, 87, 46, 83, 74, 77, 61, 90, 43, 67, 64, 72, 92, 52, 68, 53, 67, 32, 82, 76, 76, 47, 74, 47, 8, 80, 85, 58, 75), 26, 
        9    
      )}")   
  
  
      println(s"TEST-4...${driver(
        List(-82, -58, -50, -30, -14, -4, -2, -2, 0, 22, 36, 58, 70, 80, 80, 82, 84, 90), 14, 
        11    
      )}")   
  
  
      println(s"TEST-5...${driver(
        List(1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1), 7, 
        7    
      )}")   
  
  
      println(s"TEST-6...${driver(
        List(4, 11, 17, 21, 21, 22, 30, 31, 36, 37, 39, 40, 45, 46, 47, 48, 52, 53, 53, 60, 60, 65, 66, 66, 67, 67, 87, 88, 91, 97), 29, 
        9    
      )}")   
  
  
      println(s"TEST-7...${driver(
        List(-4, -60, -78, -50, 64, 18, 0, 76, 12, 86, -22), 7, 
        6    
      )}")   
  
  
      println(s"TEST-8...${driver(
        List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1), 17, 
        3    
      )}")   
  
  
      println(s"TEST-9...${driver(
        List(4, 39, 50, 37, 71, 66, 55, 34, 1, 41, 34, 99, 69, 31), 11, 
        9    
      )}")   
  
  }
  
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;
  }