VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-subarrays-with-k-equal-value-pairs/

⇱ Count Subarrays with K Equal Value Pairs - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count Subarrays with K Equal Value Pairs

Last Updated : 14 Mar, 2026

Given an arrays arr[] integer and a positive integer k , find the number of subarray which contains atleast k pairs with equal value. The pair is defined as two indices (i, j) such that i < j and arr[i] = arr[j] within the same subarray.

Example:

Input: arr[] = [2, 1, 4 , 5, 2, 1] , k = 1
Output: 3
Explanation: The valid subarrays that contain at least one pair of equal elements are: [2, 1, 4, 5, 2], [2, 1, 4, 5, 2, 1], and [1, 4, 5, 2, 1].

Input: arr[] = [1, 2, 3. 4. 5] , k = 2
Output: 0
Explanation: All elements are distinct, so no valid subarray exists.

Input: arr[] = [ 2, 1, 4, 2, 5, 2 , 6, 2, 6] , k = 3
Output: 10

[Naive Approach] - Try all Possible Subarrays - O(n^3) Time and O(1) Space

The idea is to check every possible subarray of the given array. For each subarray, we count how many pairs of equal elements it contains. While expanding a subarray by adding a new element, we compare this new element with all the elements that already exist in the current subarray. If the new element matches any previous element, it forms a pair, so we increase the pair count. If the total number of equal pairs in that subarray becomes greater than or equal to K, we count that subarray as a valid one.


Output
3

[Better Approach] - Using Hashmap - O(n^2) Time and O(n) Space

Instead of comparing the newly added element with every previous element inside the subarray (which causes the extra inner loop), keep a frequency map of how many times each value already appears in the current subarray.When you append a new element x, it will form exactly freq[x] new equal pairs (one with each previous occurrence of x). So you can update the pair count in O(1) (amortized) per extension:


Output
3

[Expected Approach] - Using Sliding Window and Hashmap - O(n) Time and O(n) Space

We keep a sliding window (a continuous subarray) and a frequency map of values inside it. When we add a number, it creates as many new equal pairs as its current count in the map, so we update the pair total immediately. As soon as the window has ≥ k pairs, every longer subarray ending at that right index is also valid - count them all at once, then move the left side forward and update counts to look for more. Each element enters and leaves the window only once, so the algorithm runs in linear time

Steps

  • Use freq to track counts inside the current window and cur for the number of equal pairs.
  • Move the right pointer i from 0 to n-1.
  • On adding arr[i], do cur += freq[arr[i]] then freq[arr[i]]++.
  • While cur >= K, add (n - i) to the answer.
  • Then remove the leftmost element by doing freq[arr[j]]-- and cur -= freq[arr[j]], and increment j.
  • Continue until i reaches the end.

Output
3
Comment
Article Tags:
Article Tags: