def subarrays_with_k(nums, k):
count = {}
res = 0
def add(x):
count[nums[x]] = 1 + count.get(nums[x], 0)
def good(x):
return nums[x] in count and count[nums[x]] >= k
def remove(x):
if count[nums[x]] == 0:
del count[nums[x]]
else:
count[nums[x]] -= 1
l = 0
for r in range(len(nums)):
add(r)
if good(r):
while good(r):
remove(l)
l += 1
res += len(nums) - r
return res
if __name__ == "__main__":
_, k = list(map(int, input().strip().split()))
nums = list(map(int, input().strip().split()))
print(subarrays_with_k(nums, k))
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vector<int> b = a;
sort(b.begin(), b.end());
b.resize(unique(b.begin(), b.end()) - b.begin());
for (auto& x: a) x = int(lower_bound(b.begin(), b.end(), x) - b.begin());
vector<int> cnt(b.size());
long long ans = 0;
bool ok = false;
for (int i = 0, j = 0; i < n; ++i) {
cnt[a[i]] += 1;
while (j < i && (cnt[a[i]] > k || (cnt[a[i]] == k && a[j] != a[i]))) {
cnt[a[j]] -= 1;
j += 1;
}
if (cnt[a[i]] >= k) ok = true;
if (ok) ans += j + 1;
}
cout << ans << "\n";
return 0;
}
130. Surrounded Regions | 129. Sum Root to Leaf Numbers |
120. Triangle | 102. Binary Tree Level Order Traversal |
96. Unique Binary Search Trees | 75. Sort Colors |
74. Search a 2D Matrix | 71. Simplify Path |
62. Unique Paths | 50. Pow(x, n) |
43. Multiply Strings | 34. Find First and Last Position of Element in Sorted Array |
33. Search in Rotated Sorted Array | 17. Letter Combinations of a Phone Number |
5. Longest Palindromic Substring | 3. Longest Substring Without Repeating Characters |
1312. Minimum Insertion Steps to Make a String Palindrome | 1092. Shortest Common Supersequence |
1044. Longest Duplicate Substring | 1032. Stream of Characters |
987. Vertical Order Traversal of a Binary Tree | 952. Largest Component Size by Common Factor |
212. Word Search II | 174. Dungeon Game |
127. Word Ladder | 123. Best Time to Buy and Sell Stock III |
85. Maximal Rectangle | 84. Largest Rectangle in Histogram |
60. Permutation Sequence | 42. Trapping Rain Water |