We are given an array of integers nums
, and we need to find the smallest index i
such that i % 10 == nums[i]
. If there is no such index, we should return -1.
0 <= nums[i] <= 10^3
.1 <= nums.length <= 10^5
.Given these typical constraints, we can proceed with our solution.
nums
:
i
, check if i % 10
is equal to nums[i]
.-1
.def smallest_equal(nums):
for i in range(len(nums)):
if i % 10 == nums[i]:
return i
return -1
The time complexity of this solution is O(n), where n
is the length of the array nums
. This is because we might have to look at each element once in the worst case.
The space complexity is O(1) as we are not using any extra space that’s dependent on the size of the input array, merely a few variables for iteration.
Let’s now run through an example to confirm understanding:
Example:
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
smallest_equal(nums) # This should return 0 because 0 % 10 == 0
In this case, the function will return 0
as expected.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?