|
| 1 | +--- |
| 2 | +id: get-maximum-in-generated-array |
| 3 | +title: Get Maximum in Generated Array |
| 4 | +sidebar_label: 1646-Get Maximum in Generated Array |
| 5 | +tags: |
| 6 | + - Brute Force |
| 7 | + - Dynamic Programming |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | +description: "This is a solution to the Get Maximum in Generated Array problem on LeetCode." |
| 13 | +sidebar_position: 2 |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +You are given an integer `n`. A 0-indexed integer array `nums` of length `n + 1` is generated in the following way: |
| 19 | + |
| 20 | +- `nums[0] = 0` |
| 21 | +- `nums[1] = 1` |
| 22 | +- `nums[2 * i] = nums[i]` when `2 <= 2 * i <= n` |
| 23 | +- `nums[2 * i + 1] = nums[i] + nums[i + 1]` when `2 <= 2 * i + 1 <= n` |
| 24 | + |
| 25 | +Return the maximum integer in the array `nums`. |
| 26 | + |
| 27 | +### Examples |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | + |
| 31 | +``` |
| 32 | +Input: n = 7 |
| 33 | +Output: 3 |
| 34 | +Explanation: According to the given rules: |
| 35 | + nums[0] = 0 |
| 36 | + nums[1] = 1 |
| 37 | + nums[(1 * 2) = 2] = nums[1] = 1 |
| 38 | + nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2 |
| 39 | + nums[(2 * 2) = 4] = nums[2] = 1 |
| 40 | + nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3 |
| 41 | + nums[(3 * 2) = 6] = nums[3] = 2 |
| 42 | + nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3 |
| 43 | +Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is max(0,1,1,2,1,3,2,3) = 3. |
| 44 | +``` |
| 45 | + |
| 46 | +**Example 2:** |
| 47 | + |
| 48 | +``` |
| 49 | +Input: n = 2 |
| 50 | +Output: 1 |
| 51 | +Explanation: According to the given rules, nums = [0,1,1]. The maximum is max(0,1,1) = 1. |
| 52 | +``` |
| 53 | + |
| 54 | +**Example 3:** |
| 55 | + |
| 56 | +``` |
| 57 | +Input: n = 3 |
| 58 | +Output: 2 |
| 59 | +Explanation: According to the given rules, nums = [0,1,1,2]. The maximum is max(0,1,1,2) = 2. |
| 60 | +``` |
| 61 | + |
| 62 | +### Constraints |
| 63 | + |
| 64 | +- `0 <= n <= 100` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Solution for Get Maximum in Generated Array Problem |
| 69 | + |
| 70 | +### Approach: Dynamic Programming |
| 71 | + |
| 72 | +The problem can be solved efficiently using a dynamic programming approach. We generate the array according to the given rules and keep track of the maximum value. |
| 73 | + |
| 74 | +#### Code in Different Languages |
| 75 | + |
| 76 | +<Tabs> |
| 77 | +<TabItem value="C++" label="C++" default> |
| 78 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 79 | + |
| 80 | +```cpp |
| 81 | +class Solution { |
| 82 | +public: |
| 83 | + int getMaximumGenerated(int n) { |
| 84 | + if (n == 0) return 0; |
| 85 | + if (n == 1) return 1; |
| 86 | + |
| 87 | + vector<int> nums(n + 1); |
| 88 | + nums[0] = 0; |
| 89 | + nums[1] = 1; |
| 90 | + int maxVal = 1; |
| 91 | + |
| 92 | + for (int i = 2; i <= n; ++i) { |
| 93 | + if (i % 2 == 0) { |
| 94 | + nums[i] = nums[i / 2]; |
| 95 | + } else { |
| 96 | + nums[i] = nums[i / 2] + nums[i / 2 + 1]; |
| 97 | + } |
| 98 | + maxVal = max(maxVal, nums[i]); |
| 99 | + } |
| 100 | + |
| 101 | + return maxVal; |
| 102 | + } |
| 103 | +}; |
| 104 | +``` |
| 105 | +
|
| 106 | +</TabItem> |
| 107 | +<TabItem value="Java" label="Java"> |
| 108 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 109 | +
|
| 110 | +```java |
| 111 | +class Solution { |
| 112 | + public int getMaximumGenerated(int n) { |
| 113 | + if (n == 0) return 0; |
| 114 | + if (n == 1) return 1; |
| 115 | +
|
| 116 | + int[] nums = new int[n + 1]; |
| 117 | + nums[0] = 0; |
| 118 | + nums[1] = 1; |
| 119 | + int maxVal = 1; |
| 120 | +
|
| 121 | + for (int i = 2; i <= n; ++i) { |
| 122 | + if (i % 2 == 0) { |
| 123 | + nums[i] = nums[i / 2]; |
| 124 | + } else { |
| 125 | + nums[i] = nums[i / 2] + nums[i / 2 + 1]; |
| 126 | + } |
| 127 | + maxVal = Math.max(maxVal, nums[i]); |
| 128 | + } |
| 129 | +
|
| 130 | + return maxVal; |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +</TabItem> |
| 136 | +<TabItem value="Python" label="Python"> |
| 137 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 138 | + |
| 139 | +```python |
| 140 | +class Solution: |
| 141 | + def getMaximumGenerated(self, n: int) -> int: |
| 142 | + if n == 0: |
| 143 | + return 0 |
| 144 | + if n == 1: |
| 145 | + return 1 |
| 146 | + |
| 147 | + nums = [0] * (n + 1) |
| 148 | + nums[0] = 0 |
| 149 | + nums[1] = 1 |
| 150 | + max_val = 1 |
| 151 | + |
| 152 | + for i in range(2, n + 1): |
| 153 | + if i % 2 == 0: |
| 154 | + nums[i] = nums[i // 2] |
| 155 | + else: |
| 156 | + nums[i] = nums[i // 2] + nums[i // 2 + 1] |
| 157 | + max_val = max(max_val, nums[i]) |
| 158 | + |
| 159 | + return max_val |
| 160 | +``` |
| 161 | + |
| 162 | +</TabItem> |
| 163 | +</Tabs> |
| 164 | + |
| 165 | +#### Complexity Analysis |
| 166 | + |
| 167 | +- **Time Complexity**: $O(n)$ |
| 168 | + - We iterate through the array once to fill in the values. |
| 169 | +- **Space Complexity**: $O(n)$ |
| 170 | + - We use additional space to store the array `nums` of length `n + 1`. |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +<h2>Authors:</h2> |
| 175 | + |
| 176 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 177 | +{['ImmidiSivani'].map(username => ( |
| 178 | + <Author key={username} username={username} /> |
| 179 | +))} |
| 180 | +</div> |
0 commit comments