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 k different indices idx1 such that idx1 < i and nums[idx1] < nums[i].
  • There exist at least k different indices idx2 such that idx2 > i and nums[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
K-Big Indices Problem: Find indices with k=2 smaller elements on both sides2351401234Left: 2,3Right: 1,4Index 2: Left count = 2 ≥ k, Right count = 2 ≥ kResult: 1 k-big index found
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
Asked in
Google 25 Amazon 18 Microsoft 15
8.5K Views
Medium Frequency
~25 min Avg. Time
342 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