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:

  1. Iterate through the array using index i.
  2. For each index i, calculate the remainder of i when divided by 10 using the modulo operator (%).
  3. Check if this remainder is equal to the value at the corresponding index in the array (nums[i]).
  4. If it is, return the index i.
  5. If no such index is found, return -1.
1
2
3
4
5
6
7
8
9
class Solution:
    def smallestEqual(self, nums: List[int]) -> int:
        # Iterate through the array using index i
        for i in range(len(nums)):
            # Check if i mod 10 is equal to nums[i]
            if i % 10 == nums[i]:
                return i
        # If no such index is found, return -1
        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.