title: Subarrays Distinct Elements
signature: def subarraysDistinctElements(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(3, 4, 5, 6, 12, 15, 16, 17, 20, 20, 22, 24, 24, 27, 28, 34, 37, 39, 39, 41, 43, 49, 49, 51, 55, 62, 63, 67, 71, 74, 74, 74, 77, 84, 84, 89, 89, 97, 99), 24, 
        255    
      )}")   
  
  
      println(s"TEST-1...${driver(
        List(-8, 54, -22, 18, 20, 44, 0, 54, 90, -4, 4, 40, -74, -16), 13, 
        335    
      )}")   
  
  
      println(s"TEST-2...${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, 1, 1, 1, 1, 1), 28, 
        30    
      )}")   
  
  
      println(s"TEST-3...${driver(
        List(36, 71, 36, 58, 38, 90, 17), 4, 
        13    
      )}")   
  
  
      println(s"TEST-4...${driver(
        List(-90, -32, -16, 18, 38, 82), 5, 
        35    
      )}")   
  
  
      println(s"TEST-5...${driver(
        List(1, 0, 1), 2, 
        4    
      )}")   
  
  
      println(s"TEST-6...${driver(
        List(3, 11, 21, 25, 28, 28, 38, 42, 48, 53, 55, 55, 55, 58, 71, 75, 79, 80, 80, 94, 96, 99), 20, 
        152    
      )}")   
  
  
      println(s"TEST-7...${driver(
        List(-16, -52, -4, -46, 54, 0, 8, -64, -82, -10, -62, -10, 58, 44, -28, 86, -24, 16, 44, 22, -28, -42, -52, 8, 76, -44, -34, 2, 88, -88, -14, -84, -36, -68, 76, 20, 20, -50), 35, 
        1791    
      )}")   
  
  
      println(s"TEST-8...${driver(
        List(0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 27, 
        29    
      )}")   
  
  
      println(s"TEST-9...${driver(
        List(19, 13, 61, 32, 92, 90, 12, 81, 52), 5, 
        35    
      )}")   
  
  }
  
source_py: def subarrays_distinct_elements(arr, n):
      s = []
      j = 0
      ans = 0
      for i in range(n):
          while ((j < n) and (arr[j] not in s)):
              s.append(arr[j])
              j += 1
          ans += (((j - i) * ((j - i) + 1)) // 2)
          s.remove(arr[i])
      return ans
source_cpp: int subarrays_distinct_elements(vector<int> arr, int n) {
    unordered_set < int > s;
    int j = 0, ans = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      while ( j < n && s . find ( arr [ j ] ) == s . end ( ) ) {
        s . insert ( arr [ j ] );
        j ++;
      }
      ans += ( ( j - i ) * ( j - i + 1 ) ) / 2;
      s . erase ( arr [ i ] );
    }
    return ans;
  }