title: Subarrays Distinct Elements
signature: subarrays_distinct_elements <- 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(3L, 4L, 5L, 6L, 12L, 15L, 16L, 17L, 20L, 20L, 22L, 24L, 24L, 27L, 28L, 34L, 37L, 39L, 39L, 41L, 43L, 49L, 49L, 51L, 55L, 62L, 63L, 67L, 71L, 74L, 74L, 74L, 77L, 84L, 84L, 89L, 89L, 97L, 99L), 24L, 
          255L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-8L, 54L, -22L, 18L, 20L, 44L, 0L, 54L, 90L, -4L, 4L, 40L, -74L, -16L), 13L, 
          335L
      )))
  
      cat(sprintf("TEST-2...%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, 1L, 1L, 1L, 1L, 1L), 28L, 
          30L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(36L, 71L, 36L, 58L, 38L, 90L, 17L), 4L, 
          13L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-90L, -32L, -16L, 18L, 38L, 82L), 5L, 
          35L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 1L), 2L, 
          4L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(3L, 11L, 21L, 25L, 28L, 28L, 38L, 42L, 48L, 53L, 55L, 55L, 55L, 58L, 71L, 75L, 79L, 80L, 80L, 94L, 96L, 99L), 20L, 
          152L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-16L, -52L, -4L, -46L, 54L, 0L, 8L, -64L, -82L, -10L, -62L, -10L, 58L, 44L, -28L, 86L, -24L, 16L, 44L, 22L, -28L, -42L, -52L, 8L, 76L, -44L, -34L, 2L, 88L, -88L, -14L, -84L, -36L, -68L, 76L, 20L, 20L, -50L), 35L, 
          1791L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 27L, 
          29L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(19L, 13L, 61L, 32L, 92L, 90L, 12L, 81L, 52L), 5L, 
          35L
      )))
  
  }
  
  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;
  }