title: Subarrays Distinct Elements
signature: fun subarraysDistinctElements(arr: ArrayList<Int>, n: Int): Int {
test_code: package main
  
  import kotlin.text.*
  import kotlin.math.*
  
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  fun validateSolution(actual:Int, expected:Int):Boolean{
    return actual.equals(expected)
  }
  fun driver(arr: ArrayList<Int>, n: Int, expected:Int):String {
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME(arr, n), expected)) {
                return "PASSED"
        }
        return "FAILED"
      } catch (e: Exception){
        return e::class.simpleName as String
      }
  
  }
  
  fun main() {
      var result: String = ""
      result = driver(arrayListOf(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("TEST-0...${result}")
      
      result = driver(arrayListOf(-8, 54, -22, 18, 20, 44, 0, 54, 90, -4, 4, 40, -74, -16), 13, 335)
      println("TEST-1...${result}")
      
      result = driver(arrayListOf(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("TEST-2...${result}")
      
      result = driver(arrayListOf(36, 71, 36, 58, 38, 90, 17), 4, 13)
      println("TEST-3...${result}")
      
      result = driver(arrayListOf(-90, -32, -16, 18, 38, 82), 5, 35)
      println("TEST-4...${result}")
      
      result = driver(arrayListOf(1, 0, 1), 2, 4)
      println("TEST-5...${result}")
      
      result = driver(arrayListOf(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("TEST-6...${result}")
      
      result = driver(arrayListOf(-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("TEST-7...${result}")
      
      result = driver(arrayListOf(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("TEST-8...${result}")
      
      result = driver(arrayListOf(19, 13, 61, 32, 92, 90, 12, 81, 52), 5, 35)
      println("TEST-9...${result}")
      
          
  }
  
  main()
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;
  }