Count the Number of K-Big Indices

Imagine you're analyzing stock prices or performance ratings where you need to identify "standout" positions. You're given a 0-indexed integer array nums and a positive integer k.

An index i is considered k-big if it satisfies both conditions:
โ€ข There are at least k different indices to the left of i where nums[idx] < nums[i]
โ€ข There are at least k different indices to the right of i where nums[idx] < nums[i]

Your goal is to return the total count of k-big indices in the array.

Example: For nums = [2,3,6,5,2,3] and k = 2, index 2 (value 6) has 2 smaller elements on its left [2,3] and 2 smaller elements on its right [5,2], making it k-big.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2,3,6,5,2,3], k = 2
โ€บ Output: 1
๐Ÿ’ก Note: Only index 2 (value 6) is k-big. It has indices [0,1] with smaller values on the left and indices [3,4] with smaller values on the right, both counts are โ‰ฅ 2.
example_2.py โ€” No Valid Indices
$ Input: nums = [1,1,1], k = 3
โ€บ Output: 0
๐Ÿ’ก Note: No index can be k-big because we need at least 3 smaller elements on each side, but we only have 3 elements total.
example_3.py โ€” Multiple Valid Indices
$ Input: nums = [1,2,5,4,3,6,7,8,1,2], k = 2
โ€บ Output: 3
๐Ÿ’ก Note: Indices 6, 7, and possibly others satisfy the k-big condition with at least 2 smaller elements on both sides.

Visualization

Tap to expand
Mountain Peak Visibility: [2,3,6,5,2,3], k=2236โ˜…523Left: 2 peaks visibleRight: 2 peaks visiblePeak Analysis:โ€ข Peak at index 2 (height 6) can see 2 lower peaks left: [2,3] โœ“โ€ข Peak at index 2 (height 6) can see 2 lower peaks right: [5,2] โœ“Result: Index 2 is k-significant with k=2!
Understanding the Visualization
1
Survey the Range
We examine each mountain peak (array element) one by one
2
Count Left Visibility
For each peak, count how many lower peaks are visible to the left
3
Count Right Visibility
Similarly, count lower peaks visible to the right
4
Identify Significant Peaks
Peaks with โ‰ฅk visibility in both directions are k-significant
Key Takeaway
๐ŸŽฏ Key Insight: We need efficient data structures like Fenwick Trees to avoid repeatedly counting smaller elements, reducing time complexity from O(nยฒ) to O(n log n)

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n elements, we potentially scan up to n elements on each side

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using a few variables for counting, no additional data structures

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค k โ‰ค nums.length
  • Performance requirement: Solution should handle maximum constraints efficiently
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
28.4K Views
Medium Frequency
~35 min Avg. Time
865 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen