114. Maximum sum of non adjacent elements

Given an integer array nums of size n. Return the maximum sum possible using the elements of nums such that no two elements taken are adjacent in nums.

Example 1:

Input: nums = [1, 2, 4]

Output: 5

Explanation:

[1, 2, 4], the underlined elements are taken to get the maximum sum.

Example 2:

Input: nums = [2, 1, 4, 9]

Output: 11

Explanation:

[2, 1, 4, 9], the underlined elements are taken to get the maximum sum.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1, 7, 16, 8]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • 0 <= nums[i] <= 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int nonAdjacent(vector<int>& nums) {
 
}
};
Test Case

Input:

Nums