Maximum sum of non-adjacent elements
You are given an array/list of ‘N’ integers. You are supposed to return the maximum sum of the subsequence with the constraint that no two elements are adjacent in the given array/list.
Note:
A subsequence of an array/list is obtained by deleting some number of elements (can be zero) from the array/list, leaving the remaining elements in their original order.
Input format:
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array.
The second line contains ‘N’ single space-separated integers denoting the elements of the array/list.
Output format:
For each test case, print a single integer that denotes the maximum sum of the non-adjacent elements.
Print the output of each test case in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 500
1 <= N <= 1000
0 <= ARR[i] <= 10^5
Where 'ARR[i]' denotes the 'i-th' element in the array/list.
Time Limit: 1 sec.
CodingNinjas
author
2y
Recursive Approach (top down)
We are going to explore all the possibilities of subsequences in which there will be no two elements that are adjacent to one another in the given array/list. So if we tak...read more
CodingNinjas
author
2y
Iterative + DP
In the first approach, there was higher time complexity, so how to reduce that?
The cause of increased time complexity was that we were solving redundant subproblems repeatedly, so in or...read more
CodingNinjas
author
2y
Iterative + 2 variables
Here we are applying a bottom approach (Dynamic Programming) as in the previous one, but now we are looking for reducing space complexity to O(1).
If we try to notice closely, w...read more
Add answer anonymously...
Top American Express Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in American Express Software Developer
>
American Express Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app