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.
Example 1:
Input: nums = [0,1,2] Output: 0 Explanation: i=0: 0 mod 10 = 0 == nums[0]. i=1: 1 mod 10 = 1 == nums[1]. i=2: 2 mod 10 = 2 == nums[2]. All indices have i mod 10 == nums[i], so we return the smallest index 0.
Example 2:
Input: nums = [4,3,2,1] Output: 2 Explanation: i=0: 0 mod 10 = 0 != nums[0]. i=1: 1 mod 10 = 1 != nums[1]. i=2: 2 mod 10 = 2 == nums[2]. i=3: 3 mod 10 = 3 != nums[3]. 2 is the only index which has i mod 10 == nums[i].
Example 3:
Input: nums = [1,2,3,4,5,6,7,8,9,0] Output: -1 Explanation: No index satisfies i mod 10 == nums[i].
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 9When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We are given a collection of numbers. The brute force strategy involves examining each position in the collection one by one. For each position, we check if the value at that position matches a specific condition and return the first one that fits.
Here's how the algorithm would work step-by-step:
def smallest_equal_index(numbers):
for index_value in range(len(numbers)):
# Check if the current index equals the value at the index
if index_value % 10 == numbers[index_value]:
# Found an index matching the condition.
return index_value
# No index satisfies the condition
return -1The fastest way to solve this is to directly check the condition for each position one at a time. Since we're looking for the *smallest* position that satisfies the requirement, we can stop as soon as we find it.
Here's how the algorithm would work step-by-step:
def smallest_equal(numbers: list[int]) -> int:
# Iterate through the list to find the smallest index
for index_position in range(len(numbers)):
# Check if the condition is met
if index_position % 10 == numbers[index_position]:
# Return the index if the condition is met
return index_position
# If no such index exists, return -1
return -1| Case | How to Handle |
|---|---|
| Null or empty input array | Return -1 immediately as no index can satisfy the condition. |
| Array with a single element | Check if the element at index 0 satisfies i mod 10 == nums[i] and return 0 if true, otherwise return -1. |
| Large array (close to maximum allowed size) | Ensure the solution has O(n) time complexity to avoid timeouts and constant memory usage (O(1)). |
| Array with all elements equal | The solution should iterate through the array and return the first index i where i mod 10 equals the common element. |
| Array with numbers exceeding the integer limit. | Since we're looking at 'i mod 10', this has no effect, the program should still work as expected. |
| Array contains negative numbers. | The problem statement states that the array contains non-negative numbers, so this case should be skipped. |
| No index 'i' satisfies the condition 'i mod 10 == nums[i]' | Return -1 after iterating through the entire array without finding a valid index. |
| Integer overflow when calculating 'i mod 10' if i is extremely large | Modulo operation handles integer overflows correctly by wrapping around, so no extra handling is needed. |