-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMaximumSumSubsequenceNonAdjacent.cpp
More file actions
61 lines (46 loc) · 2 KB
/
Copy pathMaximumSumSubsequenceNonAdjacent.cpp
File metadata and controls
61 lines (46 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Given a list of non-negative integers arr, find out the maximum sum subsequence consisting of non-adjacent elements.
We are required to return the sum.
We use dynamic programming to solve the given problem.
We use two variables - max_val1 and max_val2 such that
arr[i]+max_val1 is the maximum sum subsequence consisting of non-adjacent elements starting at position i.
And , max_val2 is the maximum sum subsequence consisting of non-adjacent elements starting at position i+1 or greater.
Time complexity = O(n) ; Space Complexity = O(1)
Input: nums = [1,2,3,1]
Output: 4
Input: nums = [2,7,9,3,1]
Output: 12
*/
#include <bits/stdc++.h>
using namespace std;
int MaxSumSeqNonAdjacent(vector<int>& nums) {
int n=nums.size();
if(n==0)
return 0;
if(n==1)
return nums[0];
if(n==2)
return max(nums[0],nums[1]);
int max_value1=nums[n-1],max_value2=nums[n-2];
for(int i=n-3;i>=0;i--)
{
int curr=nums[i]+max_value1; // curr is the maximum sum of the non-adjacent subsequence staring at position i;
max_value1=max(max_value1,max_value2); // max_value1 is updated accordingly
max_value2=curr; // max_value2 is updated accordingly
}
max_value1=max(max_value1,max_value2); //final answer is the maximum of max_value1 and max_value2
return max_value1;
}
int main() // Driver function
{
int n; // Taking input
cin>>n;
vector<int> nums;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
nums.push_back(x);
}
cout<<MaxSumSeqNonAdjacent(nums)<<endl;
return 0;
}