Dsa
Arrays
Hard
K-Unique Numbers
Given an array of integers, find the size of the longest subarray that contains at most K distinct integers. If there are fewer than K distinct integers in the array, return the size of the entire array.
Input:
The first line contains an integer N, the size of the array.
The second line contains N space-separated integers.
The third line contains the integer K.
Output:
Output a single integer representing the size of the longest subarray with at most K distinct integers.
Example:
Input:
7
1 2 1 2 3 1 2
2
Output:
5
Key concepts
arrayshashingcounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now