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 <= 100
  • 0 <= nums[i] <= 9

Note: This problem is from LeetCode.
โŒ Compiled
โŒ Executed
โŒ Correct
program main
    implicit none

    integer :: nums(100)
    integer :: i, j
    integer :: result

    ! Example 1
    nums = [0, 1, 2]
    write (*, '(A, I0)') 'Example 1: ', findSmallestIndex(nums)

    ! Example 2
    nums = [4, 3, 2, 1]
    write (*, '(A, I0)') 'Example 2: ', findSmallestIndex(nums)

    ! Example 3
    nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    write (*, '(A, I0)') 'Example 3: ', findSmallestIndex(nums)

contains

    function findSmallestIndex(nums) result(result)
        implicit none
        integer, intent(in) :: nums(:)
        integer :: result
        integer :: i

        do i = 0, size(nums) - 1
            if (mod(i, 10) == nums(i)) then
                result = i
                exit
            end if
        end do
    end function findSmallestIndex

end program main
โŒ Compiled
โŒ Executed
โŒ Correct
module find_smallest_index
    implicit none
    private
    public :: find_smallest_index

contains

    function find_smallest_index(nums) result(smallest_index)
        integer, intent(in) :: nums(:)
        integer :: smallest_index
        integer :: i

        smallest_index = -1
        do i = 0, size(nums) - 1
            if (mod(i, 10) == nums(i)) then
                smallest_index = i
                exit
            end if
        end do
    end function find_smallest_index
end module find_smallest_index

program test_find_smallest_index
    use find_smallest_index
    implicit none
    integer, parameter :: nums1(3) = [0, 1, 2]
    integer, parameter :: nums2(4) = [4, 3, 2, 1]
    integer, parameter :: nums3(10) = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
    integer :: smallest_index

    smallest_index = find_smallest_index(nums1)
    write (*, '(A, I0)') 'Example 1: ', smallest_index

    smallest_index = find_smallest_index(nums2)
    write (*, '(A, I0)') 'Example 2: ', smallest_index

    smallest_index = find_smallest_index(nums3)
    write (*, '(A, I0)') 'Example 3: ', smallest_index
end program test_find_smallest_index
๐ŸŒ Data from online sources
def smallest_index(nums):
    for i, num in enumerate(nums):
        if i % 10 == num:
            return i
    return -1

The algorithm iterates through the elements of the input array (nums) and checks whether the current index (i) of the element satisfies the condition i mod 10 == nums[i]. If the condition is satisfied, it returns the index (i). If the loop finishes without finding an index that satisfies the condition, it returns -1.

The algorithm has a time complexity of O(n), where n is the length of the input array. This is because the algorithm iterates through the array once.

๐ŸŒ Data from online sources
int smallestIndex(std::vector<int>& nums) {
    for (size_t i = 0; i < nums.size(); ++i) {
        if (i % 10 == nums[i]) {
            return i;
        }
    }
    return -1;
}

The algorithm iterates through the elements of the input array (nums) and checks whether the current index (i) of the element satisfies the condition i mod 10 == nums[i]. If the condition is satisfied, it returns the index (i). If the loop finishes without finding an index that satisfies the condition, it returns -1.

The algorithm has a time complexity of O(n), where n is the length of the input array. This is because the algorithm iterates through the array once.