title: Subarrays Distinct Elements
signature: int subarraysDistinctElements(vector<int> arr, int n) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(vector<int> arr, int n, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver({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); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver({-8, 54, -22, 18, 20, 44, 0, 54, 90, -4, 4, 40, -74, -16}, 13, 335); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({36, 71, 36, 58, 38, 90, 17}, 4, 13); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({-90, -32, -16, 18, 38, 82}, 5, 35); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({1, 0, 1}, 2, 4); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver({3, 11, 21, 25, 28, 28, 38, 42, 48, 53, 55, 55, 55, 58, 71, 75, 79, 80, 80, 94, 96, 99}, 20, 152); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver({-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); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver({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); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver({19, 13, 61, 32, 92, 90, 12, 81, 52}, 5, 35); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }