-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximumNonAdjacentSum.cpp
47 lines (38 loc) · 986 Bytes
/
maximumNonAdjacentSum.cpp
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
/*//^Given a list of positive numbers, find the largest possible set such that no elements are adjacent numbers of each other.
Here's some example and some starter code
def maxNonAdjacentSum(nums):
# Fill this in.
print(maxNonAdjacentSum([3, 4, 1, 1]))
# 5
# max sum is 4 (index 1) + 1 (index 3)
print(maxNonAdjacentSum([2, 1, 2, 7, 3]))
# 9
# max sum is 2 (index 0) + 7 (index 3)
*/
#include <iostream>
#include <vector>
#include "util.h"
int maxNonAdjacentSum(int nums[],int size)
{
int maxSum = INT_MIN; //^ thos will store the max sum
for(int i = 0;i<size;i++)
{
int sumOfPair = 0;
for(int j = i+1;j<size;j+=2)
{
sumOfPair += nums[j];
}
if (sumOfPair>maxSum)
{
maxSum = sumOfPair;
}
}
return maxSum;
}
int main()
{
int nums[]{3, 4, 1, 1};
int size = sizeof(nums) / sizeof(int);
std::cout<<maxNonAdjacentSum(nums,size)<<"\n";
return 0;
}