Count the Number of K-Big Indices - Problem
You are given a 0-indexed integer array nums and a positive integer k.
We call an index i k-big if the following conditions are satisfied:
- There exist at least
kdifferent indicesidx1such thatidx1 < iandnums[idx1] < nums[i]. - There exist at least
kdifferent indicesidx2such thatidx2 > iandnums[idx2] < nums[i].
Return the number of k-big indices.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,3,5,1,4], k = 2
›
Output:
1
💡 Note:
Only index 2 (value 5) is k-big: it has 2 smaller elements on left [2,3] and 2 smaller elements on right [1,4]
Example 2 — No K-Big Indices
$
Input:
nums = [1,1,1], k = 3
›
Output:
0
💡 Note:
No index can have 3 smaller elements on both sides since all elements are equal and array has only 3 elements
Example 3 — Multiple K-Big Indices
$
Input:
nums = [1,2,3,4,5,6], k = 1
›
Output:
4
💡 Note:
Indices 1,2,3,4 are k-big: each has at least 1 smaller element on left and 1 smaller element on right
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i] ≤ 106
- 1 ≤ k ≤ nums.length
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [2,3,5,1,4] with k=2
2
Check Each Index
Count smaller elements on left and right
3
Output
Count of indices with both counts >= k
Key Takeaway
🎯 Key Insight: Count smaller elements on both sides of each index to determine if it's k-big
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code