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] = 0nums[1] = 1nums[2 * i] = nums[i]when2 <= 2 * i <= nnums[2 * i + 1] = nums[i] + nums[i + 1]when2 <= 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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code