Get Maximum in Generated Array

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def getMaximumGenerated(self, n: int) -> int:
        if n == 0:
            return 0
        elif n == 1:
            return 1

        # Initialize the nums array with given constraints
        nums = [0] * (n + 1)
        nums[1] = 1

        # Populate the array according to the given rules
        for i in range(2, n + 1):
            if i % 2 == 0:
                nums[i] = nums[i // 2]
            else:
                nums[i] = nums[i // 2] + nums[i // 2 + 1]

        return max(nums)

This method first handles the edge cases where n is 0 or 1. Then it initializes the nums array and iterates through it, applying the rules given in the problem to fill in the values. Finally, it returns the maximum value from the array.