Dsa
Arrays
Medium
Find Longest Subarray with K Distinct Integers
Given an array of integers and an integer K, return the length of the longest contiguous subarray that contains at most K distinct integers.
Input Format:
- The first line contains two integers, N (the number of elements in the array) and K (the maximum number of distinct integers).
- The second line contains N space-separated integers representing the elements of the array.
Output Format:
- Print a single integer representing the length of the longest contiguous subarray with at most K distinct integers.
Example:
Input:
7 2
1 2 1 2 3 2 1
Output:
5
Key concepts
arrayssliding windowhashmap
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