Shortest Distance to Target Color - Problem

You're working with a colorful array where each position contains one of three colors: 1 (red), 2 (green), or 3 (blue). Your task is to efficiently answer multiple queries about finding the shortest distance from any given position to the nearest occurrence of a specific target color.

For each query (i, c), you need to find the minimum number of steps required to reach color c from index i. If the target color doesn't exist in the array, return -1.

Example: Given colors = [1,1,2,1,3,2,2,3,3], if you're at index 3 (color 1) and looking for color 2, you can go left to index 2 (distance = 1) or right to index 5 (distance = 2), so the answer is 1.

Input & Output

example_1.py — Basic Case
$ Input: colors = [1,1,2,1,3,2,2,3,3], queries = [[1,3],[2,2],[6,1]]
Output: [3, 0, 3]
💡 Note: Query [1,3]: At index 1 (color 1), nearest color 3 is at index 4, distance = |4-1| = 3. Query [2,2]: At index 2 (color 2), we're already at color 2, distance = 0. Query [6,1]: At index 6 (color 2), nearest color 1 is at index 3, distance = |6-3| = 3.
example_2.py — Color Not Found
$ Input: colors = [1,2], queries = [[0,3]]
Output: [-1]
💡 Note: Query [0,3]: At index 0, we're looking for color 3, but color 3 doesn't exist in the array, so we return -1.
example_3.py — Multiple Queries Same Position
$ Input: colors = [1,2,3,1,2], queries = [[2,1],[2,2],[2,3]]
Output: [1, 1, 0]
💡 Note: All queries start at index 2 (color 3). For color 1: nearest is at index 3, distance = 1. For color 2: nearest is at index 1, distance = 1. For color 3: we're already at color 3, distance = 0.

Constraints

  • 1 ≤ colors.length ≤ 5 × 104
  • 1 ≤ colors[i] ≤ 3
  • 1 ≤ queries.length ≤ 5 × 104
  • 0 ≤ queries[i][0] < colors.length
  • 1 ≤ queries[i][1] ≤ 3

Visualization

Tap to expand
Shortest Distance to Target Color INPUT colors array (9 elements) 0 1 2 3 4 5 6 7 8 1 1 2 1 3 2 2 3 3 1 = Red 2 = Green 3 = Blue Queries: [1, 3]: From idx 1, find 3 [2, 2]: From idx 2, find 2 [6, 1]: From idx 6, find 1 colors = [1,1,2,1,3,2,2,3,3] queries = [[1,3],[2,2],[6,1]] Query format: [index, color] ALGORITHM STEPS 1 Precompute Indices Store positions for each color color1: [0, 1, 3] color2: [2, 5, 6] color3: [4, 7, 8] 2 Binary Search Find closest position in list 3 Check Neighbors Compare left and right positions 4 Return Min Distance min(|idx - left|, |idx - right|) Example: Query [1, 3] color3 positions: [4, 7, 8] From idx 1, nearest is idx 4 Distance: |4 - 1| = 3 Time: O(log n) per query FINAL RESULT Query Results Query [1, 3]: From index 1, find color 3 (blue) Nearest blue at index 4 3 Query [2, 2]: From index 2, find color 2 (green) Already at green! Distance = 0 0 Query [6, 1]: From index 6, find color 1 (red) Nearest red at index 3 3 OUTPUT [3, 0, 3] OK - All queries answered Key Insight: Precompute the positions of each color in sorted arrays. For each query, use binary search to find the insertion point in the corresponding color's position list. Check both the left and right neighbors at that insertion point to find the minimum distance. This reduces query time from O(n) to O(log n). TutorialsPoint - Shortest Distance to Target Color | Optimal Solution
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 21
38.2K Views
Medium Frequency
~18 min Avg. Time
1.1K 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