Smallest Index With Equal Value - Problem

You are given a 0-indexed integer array nums. Your task is to find the smallest index i where a special condition is met: the index's last digit equals the value at that position.

More formally, find the smallest index i such that i mod 10 == nums[i]. If no such index exists, return -1.

What does x mod y mean? It's the remainder when x is divided by y. For example, 13 mod 10 = 3 because 13 ÷ 10 = 1 remainder 3.

Example: In array [0,1,2], index 0 has value 0, and 0 mod 10 = 0 ✓. Index 1 has value 1, and 1 mod 10 = 1 ✓. Since we want the smallest index, the answer is 0.

Input & Output

example_1.py — Basic Case
$ Input: [0,1,2]
Output: 0
💡 Note: Index 0: 0 mod 10 = 0, nums[0] = 0 → Match! Return 0.
example_2.py — Later Match
$ Input: [4,3,2,1]
Output: 2
💡 Note: Index 0: 0 mod 10 = 0, nums[0] = 4 → No match. Index 1: 1 mod 10 = 1, nums[1] = 3 → No match. Index 2: 2 mod 10 = 2, nums[2] = 2 → Match! Return 2.
example_3.py — No Match
$ Input: [1,2,3,4,5,6,7,8,9]
Output: -1
💡 Note: No index i satisfies the condition i mod 10 == nums[i], so return -1.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 9
  • Array is 0-indexed
  • Return smallest valid index or -1 if none exists

Visualization

Tap to expand
Finding Smallest Index Where i mod 10 == nums[i]Index0Value0Index1Value1Index2Value30 % 10 = 0 ✓1 % 10 = 1 ✓2 % 10 ≠ 3 ✗MATCH!Since index 0 satisfies the condition,we return 0 immediately(smallest index wins!)
Understanding the Visualization
1
Initialize
Start scanning from index 0
2
Check Condition
For each index i, check if i % 10 equals nums[i]
3
Early Return
Return immediately when first match is found
4
No Match
If entire array is scanned without match, return -1
Key Takeaway
🎯 Key Insight: Since we need the smallest index, we must scan from left to right and return the first match immediately. No preprocessing can help us avoid this sequential check.
Asked in
Amazon 8 Google 5 Microsoft 3
12.8K Views
Medium Frequency
~8 min Avg. Time
425 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