Get Maximum in Generated Array

🏠 ⬅️ ➑️

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

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
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
βœ… Compiled
βœ… Executed
⚠️ Correct
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
🌐 Data from online sources
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
  1. Initialize two pointers 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.
  2. Use a while loop that runs until k becomes 0. Inside the loop, we do the following:
  3. Check if the current value of 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.
  4. If the current value of num is not in the array, decrement k. This means we found one missing number.
  5. Increment num.
  6. When the loop ends, k becomes 0, meaning we found the kth missing number. The value of num - 1 is the kth missing positive integer. Return this value.
🌐 Data from online sources
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;
}
  1. Initialize two pointers 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.
  2. Use a while loop that runs until k becomes 0. Inside the loop, we do the following:
  3. Check if the current value of 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.
  4. If the current value of num is not in the array, decrement k. This means we found one missing number.
  5. Increment num.
  6. When the loop ends, k becomes 0, meaning we found the kth missing number. The value of num - 1 is the kth missing positive integer. Return this value.