You are given an integer n
. A 0-indexed integer array nums
of length n + 1
is generated in the following way:
nums[0] = 0
nums[1] = 1
nums[2 * i] = nums[i]
when 2 <= 2 * i <= n
nums[2 * i + 1] = nums[i] + nums[i + 1]
when 2 <= 2 * i + 1 <= n
Return the maximum integer in the array nums
βββ.
Example 1:
Input: n = 7 Output: 3 Explanation: According to the given rules: nums[0] = 0 nums[1] = 1 nums[(1 * 2) = 2] = nums[1] = 1 nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2 nums[(2 * 2) = 4] = nums[2] = 1 nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3 nums[(3 * 2) = 6] = nums[3] = 2 nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3 Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is max(0,1,1,2,1,3,2,3) = 3.
Example 2:
Input: n = 2 Output: 1 Explanation: According to the given rules, nums = [0,1,1]. The maximum is max(0,1,1) = 1.
Example 3:
Input: n = 3 Output: 2 Explanation: According to the given rules, nums = [0,1,1,2]. The maximum is max(0,1,1,2) = 2.
Constraints:
0 <= n <= 100
program main
implicit none
integer :: n, nums(n+1), max_val
! example 1
n = 7
call generate_array(n, nums)
write(*,*) 'Example 1:'
write(*,*) 'n = ', n
write(*,*) 'nums = ', nums
max_val = get_max(nums)
write(*,*) 'Maximum value = ', max_val
! example 2
n = 2
call generate_array(n, nums)
write(*,*) 'Example 2:'
write(*,*) 'n = ', n
write(*,*) 'nums = ', nums
max_val = get_max(nums)
write(*,*) 'Maximum value = ', max_val
! example 3
n = 3
call generate_array(n, nums)
write(*,*) 'Example 3:'
write(*,*) 'n = ', n
write(*,*) 'nums = ', nums
max_val = get_max(nums)
write(*,*) 'Maximum value = ', max_val
contains
subroutine generate_array(n, nums)
implicit none
integer, intent(in) :: n
integer, intent(out) :: nums(n+1)
nums(0) = 0
nums(1) = 1
do i = 2, n
if (mod(i, 2) == 0) then
nums(i) = nums(i/2)
else
nums(i) = nums(i/2) + nums((i+1)/2)
end if
end do
end subroutine generate_array
function get_max(nums) result(max_val)
implicit none
integer, intent(in) :: nums(:)
integer :: max_val
max_val = maxval(nums)
end function get_max
end program main
temp.f95:4:28: 4 | integer :: n, nums(n+1), max_val | 1 Error: Explicit shaped array with nonconstant bounds at (1) temp.f95:12:11: 12 | max_val = get_max(nums) | 1 Error: Symbol βmax_valβ at (1) has no IMPLICIT type temp.f95:7:5: 7 | n = 7 | 1 Error: Symbol βnβ at (1) has no IMPLICIT type temp.f95:8:31: 8 | call generate_array(n, nums) | 1 Error: Symbol βnumsβ at (1) has no IMPLICIT type temp.f95:42:12: 42 | do i = 2, n | 1 Error: Symbol βiβ at (1) has no IMPLICIT type temp.f95:40:13: 40 | nums(0) = 0 | 1 Warning: Array reference at (1) is out of bounds (0 < 1) in dimension 1
program max_array
implicit none
integer, parameter :: n = 7
integer :: nums(n + 1)
integer :: i
! Initialize the array
nums(0) = 0
nums(1) = 1
do i = 2, n, 2
nums(i) = nums(i / 2)
end do
do i = 3, n, 2
nums(i) = nums(i / 2) + nums(i / 2 + 1)
end do
! Print the maximum
print *, maxval(nums)
end program max_array
3
def findKthPositive(arr, k):
i, num = 0, 1
while k > 0:
if i < len(arr) and arr[i] == num:
i += 1
else:
k -= 1
num += 1
return num - 1
i
and num
. i
is used to iterate through the input array arr
, and num
is used to check if the integer is in the array.k
becomes 0. Inside the loop, we do the following:num
is in the array (i.e., if arr[i] == num
). If it is, increment i
. This means the current value of num
is not a missing number.num
is not in the array, decrement k
. This means we found one missing number.num
.k
becomes 0, meaning we found the kth missing number. The value of num - 1
is the kth missing positive integer. Return this value.int findKthPositive(vector<int>& arr, int k) {
int i = 0, num = 1;
while (k > 0) {
if (i < arr.size() && arr[i] == num) {
i++;
} else {
k--;
}
num++;
}
return num - 1;
}
i
and num
. i
is used to iterate through the input array arr
, and num
is used to check if the integer is in the array.k
becomes 0. Inside the loop, we do the following:num
is in the array (i.e., if arr[i] == num
). If it is, increment i
. This means the current value of num
is not a missing number.num
is not in the array, decrement k
. This means we found one missing number.num
.k
becomes 0, meaning we found the kth missing number. The value of num - 1
is the kth missing positive integer. Return this value.