Get Maximum in Generated Array - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: n = 7
Output: 3
💡 Note: nums = [0,1,1,2,1,3,2,3]. Generated by rules: nums[2]=nums[1]=1, nums[3]=nums[1]+nums[2]=2, nums[4]=nums[2]=1, nums[5]=nums[2]+nums[3]=3, nums[6]=nums[3]=2, nums[7]=nums[3]+nums[4]=3. Maximum is 3.
Example 2 — Small Input
$ Input: n = 2
Output: 1
💡 Note: nums = [0,1,1]. nums[2] = nums[1] = 1. Maximum is 1.
Example 3 — Edge Case
$ Input: n = 0
Output: 0
💡 Note: nums = [0]. Only element is 0, so maximum is 0.

Constraints

  • 0 ≤ n ≤ 100

Visualization

Tap to expand
Get Maximum in Generated ArrayInput: n = 7n = 7Generated Array01121323i=0i=1i=2i=3i=4i=5i=6i=7Generation Rules:nums[2*i] = nums[i] (even indices)nums[2*i+1] = nums[i] + nums[i+1] (odd indices)Output: Maximum = 3Maximum: 3
Understanding the Visualization
1
Input
Given integer n = 7
2
Generate
Build array using generation rules
3
Output
Return maximum value found: 3
Key Takeaway
🎯 Key Insight: Simulate the array generation process while tracking the maximum value encountered
Asked in
Amazon 25 Microsoft 20
28.0K Views
Medium Frequency
~10 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen