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
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
temp.f95:9:4: 9 | nums = [0, 1, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (100 and 3) temp.f95:13:4: 13 | nums = [4, 3, 2, 1] | 1 Error: Different shape for array assignment at (1) on dimension 1 (100 and 4) temp.f95:17:4: 17 | nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] | 1 Error: Different shape for array assignment at (1) on dimension 1 (100 and 10)
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
temp.f95:4:33: 4 | public :: find_smallest_index | 1 Error: PUBLIC attribute applied to MODULE find_smallest_index at (1) temp.f95:8:32: 8 | function find_smallest_index(nums) result(smallest_index) | 1 Error: MODULE attribute of โfind_smallest_indexโ conflicts with PROCEDURE attribute at (1) temp.f95:9:38: 9 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:33: 10 | integer :: smallest_index | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:20: 11 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:27: 13 | smallest_index = -1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:32: 14 | do i = 0, size(nums) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:43: 15 | if (mod(i, 10) == nums(i)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:16:34: 16 | smallest_index = i | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:20: 17 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:18:15: 18 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:19:11: 19 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:20:7: 20 | end function find_smallest_index | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:9: 24 | use find_smallest_index | 1 Fatal Error: Cannot open module file โfind_smallest_index.modโ for reading at (1): No such file or directory compilation terminated.
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.
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.