title: Count Subarrays Total Distinct Elements Original Array
signature: class Solution {
  
      public int CountSubarraysTotalDistinctElementsOriginalArray(List<int> arr, int n) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(List<int> arr, int n, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(arr, n); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  new List<int>{13, 39, 49, 52, 53, 69, 72, 79, 83, 96}, 5, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-98, -98, 22, -10, -28, 0, 56, 72, 36, 88, 96, 22, 90, 74, -60, -64, 0, 2, -42, 0, 94, -82, -74}, 20, 
                  4
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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}, 26, 
                  160
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{35, 23, 41, 58, 66, 92, 3, 33, 78, 70, 80, 86, 21, 21, 74, 19}, 12, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-98, -80, -52, -10, 4, 76}, 3, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1}, 36, 
                  609
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{2, 7, 10, 17, 26, 36, 37, 85, 87, 88}, 8, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{64, -2, -94, -84, -48, 86}, 5, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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}, 20, 
                  91
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{91, 49, 94, 81, 64, 5, 13, 70, 82, 9, 80, 82}, 9, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
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;
  }