Data Structure Array: Maximum sum such that no two elements are adjacent
http://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <queue> 5 #include <stack> 6 #include <string> 7 #include <fstream> 8 #include <map> 9 using namespace std; 10 11 int maxsum(int arr[], int n) { 12 int insum = arr[0]; 13 int exsum = 0; 14 for (int i = 1; i < n; i++) { 15 int tmp = max(insum, exsum); 16 insum = exsum + arr[i]; 17 exsum = tmp; 18 } 19 return max(insum, exsum); 20 } 21 22 int main() { 23 int arr[6] = {5, 5, 10, 100, 10, 5}; 24 cout << maxsum(arr, 6) << endl; 25 return 0; 26 }