Smallest Index With Equal Value - Problem

Given a 0-indexed integer array nums, return the smallest index i of nums such that i mod 10 == nums[i], or -1 if such index does not exist.

x mod y denotes the remainder when x is divided by y.

Input & Output

Example 1 — Found Match
$ Input: nums = [0,1,2,3,4,5,6,7,8,9]
Output: 0
💡 Note: At index 0: 0 % 10 = 0 and nums[0] = 0, so return 0
Example 2 — Later Match
$ Input: nums = [4,3,2,1]
Output: -1
💡 Note: No index i satisfies i % 10 == nums[i]: 0≠4, 1≠3, 2≠2 is false, 3≠1
Example 3 — Double Digit Index
$ Input: nums = [1,2,3,4,5,6,7,8,9,0,1,2,3]
Output: 10
💡 Note: At index 10: 10 % 10 = 0 but nums[10] = 1, continue. At index 12: 12 % 10 = 2 but nums[12] = 3. Actually at index 10: 10 % 10 = 0 and nums[10] = 1, so no match until later.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 0 ≤ nums[i] ≤ 9

Visualization

Tap to expand
Find smallest index i where i % 10 == nums[i]01530Array: [0, 1, 5, 3, 0]i=0i=1i=2i=3i=40%10=0==0?1%10=1==1?2%10=2==5?3%10=3==3✓skipFirst match at index 3: 3 % 10 = 3 and nums[3] = 3Result: 3
Asked in
Google 15 Amazon 12
12.0K Views
Low Frequency
~5 min Avg. Time
234 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