Dsa
Sliding Window
Warm-up
Consecutive Element Frequency
You are given an array of integers. Write a function to find the maximum frequency of any element within any window of size k.
For example, for the input array [1, 2, 1, 3, 2] and k = 3, the output would be 2 (as the maximum frequency of 1, 2 in the window [1, 2, 1] or [2, 1, 3]).
Input Format:
- The first line contains two integers, n (number of elements in array) and k (window size).
- The second line contains n space-separated integers.
Output Format:
- A single integer representing the maximum frequency of any element within any window of size k.
Example:
Input:
5 3
1 2 1 3 2
Output:
2
Key concepts
sliding_windowarrayfrequency
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