Maximum Sum of Non-Adjacent Elements

Given an array/list of ‘N’ integers, your task is to return the maximum sum of the subsequence where no two elements are adjacent in the given array/list.

Example:

Input:
T = 1
N = 5
array = [3, 2, 7, 10, 12]
Output:
22
Explanation:

The maximum sum can be obtained by choosing the subsequence [3, 10, 12], which results in a sum of 22. No two chosen elements are adjacent.

Input:

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:

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.

Constraints:

  • 1 <= T <= 500
  • 1 <= N <= 1000
  • 0 <= ARR[i] <= 105
  • Time Limit: 1 sec.
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. You do not need to print anything; it has already been taken care of. Just implement the given function.

AnswerBot
4mo

Find the maximum sum of non-adjacent elements in an array.

  • Use dynamic programming to keep track of the maximum sum at each index, considering whether to include the current element or skip it.

  • At each ...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Amazon

Q. Could you describe the process for designing a data structure that allows for al...read more
Q. What is Java?
Q. Given two strings s and t, return true if they are equal when both are typed int...read more
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits