title: Count Subarrays Total Distinct Elements Original Array
signature: count_subarrays_total_distinct_elements_original_array <- 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(13L, 39L, 49L, 52L, 53L, 69L, 72L, 79L, 83L, 96L), 5L, 
          1L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-98L, -98L, 22L, -10L, -28L, 0L, 56L, 72L, 36L, 88L, 96L, 22L, 90L, 74L, -60L, -64L, 0L, 2L, -42L, 0L, 94L, -82L, -74L), 20L, 
          4L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(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), 26L, 
          160L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(35L, 23L, 41L, 58L, 66L, 92L, 3L, 33L, 78L, 70L, 80L, 86L, 21L, 21L, 74L, 19L), 12L, 
          1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-98L, -80L, -52L, -10L, 4L, 76L), 3L, 
          1L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L), 36L, 
          609L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(2L, 7L, 10L, 17L, 26L, 36L, 37L, 85L, 87L, 88L), 8L, 
          1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(64L, -2L, -94L, -84L, -48L, 86L), 5L, 
          1L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(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), 20L, 
          91L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(91L, 49L, 94L, 81L, 64L, 5L, 13L, 70L, 82L, 9L, 80L, 82L), 9L, 
          1L
      )))
  
  }
  
  main()
source_py: def count_subarrays_total_distinct_elements_original_array(arr, n):
      vis = dict()
      for i in range(n):
          vis[arr[i]] = 1
      k = len(vis)
      vid = dict()
      ans = 0
      right = 0
      window = 0
      for left in range(n):
          while ((right < n) and (window < k)):
              if (arr[right] in vid.keys()):
                  vid[arr[right]] += 1
              else:
                  vid[arr[right]] = 1
              if (vid[arr[right]] == 1):
                  window += 1
              right += 1
          if (window == k):
              ans += ((n - right) + 1)
          vid[arr[left]] -= 1
          if (vid[arr[left]] == 0):
              window -= 1
      return ans
source_cpp: int count_subarrays_total_distinct_elements_original_array(vector<int> arr, int n) {
    unordered_map < int, int > vis;
    for ( int i = 0;
    i < n;
    ++ i ) vis [ arr [ i ] ] = 1;
    int k = vis . size ( );
    vis . clear ( );
    int ans = 0, right = 0, window = 0;
    for ( int left = 0;
    left < n;
    ++ left ) {
      while ( right < n && window < k ) {
        ++ vis [ arr [ right ] ];
        if ( vis [ arr [ right ] ] == 1 ) ++ window;
        ++ right;
      }
      if ( window == k ) ans += ( n - right + 1 );
      -- vis [ arr [ left ] ];
      if ( vis [ arr [ left ] ] == 0 ) -- window;
    }
    return ans;
  }