Smallest Index With Equal Value
We need to find the smallest index i
in the array nums
such that the remainder of the division of i
by 10 is equal to the value at that index in the array. If no such index exists, we’ll return -1.
Here’s a clear step-by-step approach to solve this problem:
- Iterate through the array using index
i
. - For each index
i
, calculate the remainder ofi
when divided by 10 using the modulo operator (%
). - Check if this remainder is equal to the value at the corresponding index in the array (
nums[i]
). - If it is, return the index
i
. - If no such index is found, return -1.
|
|
Explanation:
- By iterating through the array and calculating the remainder of each index when divided by 10, we’re essentially checking if the current index follows the condition
i mod 10 == nums[i]
. - If we find an index that meets this condition, we return it immediately, thus ensuring that we return the smallest such index.
- If we iterate through the entire array without finding such an index, we return -1, indicating that no such index exists.