- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find maximum in generated array in Python

Suppose we have a number n. We have to generate an array A of length n + 1 in the following way −

A[0] = 0

A[1] = 1

A[2 * i] = A[i] if 2 <= 2 * i <= n

A[2 * i + 1] = A[i] + A[i + 1] if 2 <= 2 * i + 1 <= n

Finally we have to find the maximum number in the array nums.

So, if the input is like n = 5, then the output will be 3 because

A[0] = 0

A[1] = 1

A[2] = A[1] = 1

A[3] = A[1] + A[2] = 1 + 1 = 2

A[4] = A[2]= 1

A[5] = A[2] + A[3] = 1 + 2 = 3

A[6] = A[3] = 2

So the maximum is 3

To solve this, we will follow these steps −

A := a new list from 0 to n

for each element i in A, do

if i is same as 0 or i is same as 1, then

go to next iteration

otherwise when i is even, then

A[i] := A[integer of i/2]

otherwise,

A[i] := A[integer of i/2] + A[(integer of i/2) + 1]

return maximum element of A

## Example (Python)

Let us see the following implementation to get better understanding −

def solve(n): A = list(range(0,n+1)) for i in A: if i == 0 or i == 1: continue elif i%2 == 0: A[i] = A[i//2] else: A[i] = A[i//2] + A[(i//2) + 1] return max(A) n = 5 print(solve(n))

## Input

5

## Output

3

- Related Questions & Answers
- Program to find maximum weighted sum for rotated array in Python
- Program to find maximum XOR with an element from array in Python
- Program to find maximum width ramp in Python
- Program to find maximum erasure value in Python
- Program to find maximum building height in Python
- PHP program to find the maximum element in an array
- Program to find maximum average pass ratio in Python
- Program to find maximum ice cream bars in Python
- Program to find maximum subarray min-product in Python
- Program to find maximum product of two distinct elements from an array in Python
- Program to find maximum score in stone game in Python
- Program to find maximum value at a given index in a bounded array in Python
- C++ Program to find array after removal from maximum
- C# program to find maximum and minimum element in an array
- Program to find maximum population year using Python