You are given an array A of length N, a starting index cur, and a distance D.
Your task is to iteratively update the array A starting from index cur.
At each step, replace A[cur] with A[cur] + 1, then find the nearest index (left or right, within distance D) where this new value exists in the array.
If multiple indices exist at the same distance, choose the leftmost index.
If no such index exists within distance D, keep the value in the same index and stop updating.
Complete the function nearestValueReplacement in the editor.
nearestValueReplacement has the following parameters:
- 1.
int[] A: an array of integers - 2.
int cur: the starting index - 3.
int D: the distance within which to search
Returns
int[]: the updated array
Examples
01 · Example 1
A = [1, 3, 2, 3, 4, 5, 2] cur = 2 D = 2 return = [1, 3, 3, 3, 4, 5, 2]
;o
More Google problems
- Consolidate On-Call RotationsOA · Seen Jun 2026
- Detonate Bombs with Chain ReactionsONSITE INTERVIEW · Seen May 2026
- Evaluate a Nested Math ExpressionONSITE INTERVIEW · Seen May 2026
- Tic-Tac-Toe Game StatusPHONE SCREEN · Seen May 2026
- Longest Dictionary TokenizationPHONE SCREEN · Seen May 2026
- Minimum Cars for Rental RequestsONSITE INTERVIEW · Seen Apr 2026
- Shortest Path with Mandatory WaypointONSITE INTERVIEW · Seen Apr 2026
- Count Divisible Coin SelectionsOA · Seen Dec 2025
public int[] nearestValueReplacement(int[] A, int cur, int D) {
// write your code here
}
A[1, 3, 2, 3, 4, 5, 2]
cur2
D2
expected[1, 3, 3, 3, 4, 5, 2]
sign in to submit