Ctrl K

Near a Key Value

Easy
You are given an integer array values, a number key that occurs in the array, and a radius. Position i is near the key if there is some position j with values[j] == key and |i - j| ≤ radius. Return every position near the key (0-based) in increasing order.
Example 1
values=[6, 2, 8, 2, 5, 1, 7], key=2, radius=1 → [0, 1, 2, 3, 4]

The value 2 sits at positions 1 and 3. Within 1 of them are positions 0, 1, 2, 3, 4. Positions 5 and 6 are too far.

Example 2
values=[4, 4, 9, 4], key=9, radius=0 → [2]

With radius 0 only the position holding 9 itself qualifies.

Example 3
values=[1, 3, 3], key=1, radius=3 → [0, 1, 2]
Constraints
  • 1 ≤ values.length ≤ 1000
  • 1 ≤ values[i] ≤ 1000
  • key occurs in values
  • 0 ≤ radius ≤ values.length
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasyMerge Into the Spare Tail EasyZeros to the Back EasyValues in Both Lists Easy
loading Python…
Input
values=[6, 2, 8, 2, 5, 1, 7], key=2, radius=1
Expected
[0, 1, 2, 3, 4]

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.