CSES Solutions - Subarray Distinct Values
Last Updated :
28 Mar, 2024
Given an array of N integers arr[] and an integer K, your task is to calculate the number of subarrays that have at most K distinct values.
Examples:
Input: N = 5, K = 2, arr[] = {1, 2, 1, 2, 3}
Output: 10
Explanation: There are 12 subarrays with at most 2 distinct values: {1}, {2}, {1}, {2}, {3}, {1, 2}, {2, 1}, {1, 2}, {2, 3}, {1, 2, 1}, {2, 1, 2}, {1, 2, 1, 2}.
Input: N = 4, K = 1, arr[] = {2, 2, 2, 2}
Output: 4
Explanation: There are 4 subarrays with at most 1 distinct values: {2}, {2}, {2}, {2}.
Approach To solve the problem, follow the below idea:
We maintain a Sliding Window and a hashmap to track the count of elements in the window. As we move the window's right boundary, we update the hashmap. If the number of distinct elements in the window exceeds k, we shrink the window from the left until it contains at most k distinct elements. We count the subarrays for each valid window position and add them to the result.
Step-by-step algorithm:
- Initialize two pointers left = 0 and right = 0 to mark the starting and ending point of the sliding window and another variable result = 0 to store the number of arrays with at most K distinct values.
- Maintain a variable distinct_count to keep track of the number of distinct elements in the current window.
- Also maintain a map, say frequency to store the frequency of elements in the current window.
- Iterate over the array till the right pointer of the window does not exceed the end of the array.
- Move the right pointer till the number of distinct elements in the current window <= K.
- Shrink the window from the left side if the number of distinct elements exceeds K.
- Decrement the frequency of the leftmost element and move the left pointer to the right until the number of distinct elements becomes <= K.
- While doing this, we accumulate the count of valid subarrays by adding the length of each subarray to result.
- After reaching the end of the array, we print the final result.
Below is the implementation of the above algorithm:
C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll solve(ll* arr, ll N, ll K)
{
// left and right pointers to mark the start and end of
// the sliding window
ll left = 0, right = 0;
// Variable to count how many different numbers we have
// in the window
ll distinct_count = 0;
// Variable to store the final result
ll result = 0;
// Map to keep track of how many times each number
// appears in the window
unordered_map<ll, ll> frequency;
// Slide the window till the window till the right
// pointer does not reach the end of the array
while (right < N) {
// Check if the current number is new or if its
// count is zero
if (frequency.find(arr[right]) == frequency.end()
|| frequency[arr[right]] == 0)
distinct_count++;
// Update the count of the current number
frequency[arr[right]]++;
// If there are more than K distinct numbers, shrink
// the window from the left
while (distinct_count > K) {
// Decrease the count of the number going out of
// the window
frequency[arr[left]]--;
// If its count becomes zero, then there will be
// one less distinct number in the window
if (frequency[arr[left]] == 0)
distinct_count--;
// Move the left pointer to the right to shrink
// the window
left++;
}
// Calculate the number of subarrays that end at the
// current position
result += right - left + 1;
// Move the right edge of the window to the right to
// expand it
right++;
}
// Return the result
return result;
}
int main()
{
// Sample Input
ll N = 5, K = 2;
ll arr[N] = { 1, 2, 1, 2, 3 };
cout << solve(arr, N, K) << "\n";
// We're done!
return 0;
}
Java
import java.util.HashMap;
public class Main {
static long solve(long[] arr, long N, long K) {
// left and right pointers to mark the start and end of
// the sliding window
long left = 0, right = 0;
// Variable to count how many different numbers we have
// in the window
long distinct_count = 0;
// Variable to store the final result
long result = 0;
// Map to keep track of how many times each number
// appears in the window
HashMap<Long, Long> frequency = new HashMap<>();
// Slide the window till the window till the right
// pointer does not reach the end of the array
while (right < N) {
// Check if the current number is new or if its
// count is zero
if (!frequency.containsKey(arr[(int)right]) || frequency.get(arr[(int)right]) == 0)
distinct_count++;
// Update the count of the current number
frequency.put(arr[(int)right], frequency.getOrDefault(arr[(int)right], 0L) + 1);
// If there are more than K distinct numbers, shrink
// the window from the left
while (distinct_count > K) {
// Decrease the count of the number going out of
// the window
frequency.put(arr[(int)left], frequency.get(arr[(int)left]) - 1);
// If its count becomes zero, then there will be
// one less distinct number in the window
if (frequency.get(arr[(int)left]) == 0)
distinct_count--;
// Move the left pointer to the right to shrink
// the window
left++;
}
// Calculate the number of subarrays that end at the
// current position
result += right - left + 1;
// Move the right edge of the window to the right to
// expand it
right++;
}
// Return the result
return result;
}
public static void main(String[] args) {
// Sample Input
long N = 5, K = 2;
long[] arr = { 1, 2, 1, 2, 3 };
System.out.println(solve(arr, N, K));
// We're done!
}
}
JavaScript
function solve(arr, N, K) {
// left and right pointers to mark the start and end of
// the sliding window
let left = 0, right = 0;
// Variable to count how many different numbers we have
// in the window
let distinctCount = 0;
// Variable to store the final result
let result = 0;
// Map to keep track of how many times each number
// appears in the window
let frequency = {};
// Slide the window till the right pointer does not reach the end of the array
while (right < N) {
// Check if the current number is new or if its count is zero
if (!frequency[arr[right]] || frequency[arr[right]] === 0)
distinctCount++;
// Update the count of the current number
frequency[arr[right]] = (frequency[arr[right]] || 0) + 1;
// If there are more than K distinct numbers, shrink the window from the left
while (distinctCount > K) {
// Decrease the count of the number going out of the window
frequency[arr[left]]--;
// If its count becomes zero, then there will be one less distinct number in the window
if (frequency[arr[left]] === 0)
distinctCount--;
// Move the left pointer to the right to shrink the window
left++;
}
// Calculate the number of subarrays that end at the current position
result += right - left + 1;
// Move the right edge of the window to the right to expand it
right++;
}
return result;
}
let N = 5, K = 2;
let arr = [1, 2, 1, 2, 3];
console.log(solve(arr, N, K));
// This code is contributed by Ayush Mishra
Python3
from collections import defaultdict
def solve(arr, N, K):
# left and right pointers to mark the start and end of
# the sliding window
left = 0
right = 0
# Variable to count how many different numbers we have
# in the window
distinct_count = 0
# Variable to store the final result
result = 0
# Dictionary to keep track of how many times each number
# appears in the window
frequency = defaultdict(int)
# Slide the window till the window till the right
# pointer does not reach the end of the array
while right < N:
# Check if the current number is new or if its
# count is zero
if frequency[arr[right]] == 0:
distinct_count += 1
# Update the count of the current number
frequency[arr[right]] += 1
# If there are more than K distinct numbers, shrink
# the window from the left
while distinct_count > K:
# Decrease the count of the number going out of
# the window
frequency[arr[left]] -= 1
# If its count becomes zero, then there will be
# one less distinct number in the window
if frequency[arr[left]] == 0:
distinct_count -= 1
# Move the left pointer to the right to shrink
# the window
left += 1
# Calculate the number of subarrays that end at the
# current position
result += right - left + 1
# Move the right edge of the window to the right to
# expand it
right += 1
# Return the result
return result
N = 5
K = 2
arr = [1, 2, 1, 2, 3]
print(solve(arr, N, K))
Time Complexity: O(N), where N is the size of array arr[].
Auxiliary Space: O(N)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
13 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. How do
14 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high. We sort the array using multiple passes. After the fi
8 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Conditions to apply Binary Search Algorithm in a Data S
15+ min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Selection Sort
Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted. First we find the smallest element a
8 min read