Given an array of positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array. So 3 2 7 10 should return 13 (sum of 3 and 10) or 3 2 5 10 7 should return 15 (sum of 3, 5 and 7).Answer the question in most efficient way.

Examples :

Input : arr[] = {5, 5, 10, 100, 10, 5} Output : 110 Input : arr[] = {1, 2, 3} Output : 4 Input : arr[] = {1, 20, 3} Output : 20

**Algorithm:**

Loop for all elements in arr[] and maintain two sums incl and excl where incl = Max sum including the previous element and excl = Max sum excluding the previous element.

Max sum excluding the current element will be max(incl, excl) and max sum including the current element will be excl + current element (Note that only excl is considered because elements cannot be adjacent).

At the end of the loop return max of incl and excl.

**Example:**

arr[] = {5, 5, 10, 40, 50, 35} incl = 5 excl = 0 For i = 1 (current element is 5) incl = (excl + arr[i]) = 5 excl = max(5, 0) = 5 For i = 2 (current element is 10) incl = (excl + arr[i]) = 15 excl = max(5, 5) = 5 For i = 3 (current element is 40) incl = (excl + arr[i]) = 45 excl = max(5, 15) = 15 For i = 4 (current element is 50) incl = (excl + arr[i]) = 65 excl = max(45, 15) = 45 For i = 5 (current element is 35) incl = (excl + arr[i]) = 80 excl = max(65, 45) = 65 And 35 is the last element. So, answer is max(incl, excl) = 80

Thanks to Debanjan for providing code.

**Implementation:**

## C/C++

`#include<stdio.h> ` ` ` `/*Function to return max sum such that no two elements ` ` ` `are adjacent */` `int` `FindMaxSum(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `incl = arr[0]; ` ` ` `int` `excl = 0; ` ` ` `int` `excl_new; ` ` ` `int` `i; ` ` ` ` ` `for` `(i = 1; i < n; i++) ` ` ` `{ ` ` ` `/* current max excluding i */` ` ` `excl_new = (incl > excl)? incl: excl; ` ` ` ` ` `/* current max including i */` ` ` `incl = excl + arr[i]; ` ` ` `excl = excl_new; ` ` ` `} ` ` ` ` ` `/* return max of incl and excl */` ` ` `return` `((incl > excl)? incl : excl); ` `} ` ` ` `/* Driver program to test above function */` `int` `main() ` `{ ` ` ` `int` `arr[] = {5, 5, 10, 100, 10, 5}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `printf` `(` `"%d n"` `, FindMaxSum(arr, n)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`class` `MaximumSum ` `{ ` ` ` `/*Function to return max sum such that no two elements ` ` ` `are adjacent */` ` ` `int` `FindMaxSum(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `int` `incl = arr[` `0` `]; ` ` ` `int` `excl = ` `0` `; ` ` ` `int` `excl_new; ` ` ` `int` `i; ` ` ` ` ` `for` `(i = ` `1` `; i < n; i++) ` ` ` `{ ` ` ` `/* current max excluding i */` ` ` `excl_new = (incl > excl) ? incl : excl; ` ` ` ` ` `/* current max including i */` ` ` `incl = excl + arr[i]; ` ` ` `excl = excl_new; ` ` ` `} ` ` ` ` ` `/* return max of incl and excl */` ` ` `return` `((incl > excl) ? incl : excl); ` ` ` `} ` ` ` ` ` `// Driver program to test above functions ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `MaximumSum sum = ` `new` `MaximumSum(); ` ` ` `int` `arr[] = ` `new` `int` `[]{` `5` `, ` `5` `, ` `10` `, ` `100` `, ` `10` `, ` `5` `}; ` ` ` `System.out.println(sum.FindMaxSum(arr, arr.length)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by Mayank Jaiswal ` |

*chevron_right*

*filter_none*

## Python

`# Function to return max sum such that ` `# no two elements are adjacent ` `def` `find_max_sum(arr): ` ` ` `incl ` `=` `0` ` ` `excl ` `=` `0` ` ` ` ` `for` `i ` `in` `arr: ` ` ` ` ` `# Current max excluding i (No ternary in ` ` ` `# Python) ` ` ` `new_excl ` `=` `excl ` `if` `excl>incl ` `else` `incl ` ` ` ` ` `# Current max including i ` ` ` `incl ` `=` `excl ` `+` `i ` ` ` `excl ` `=` `new_excl ` ` ` ` ` `# return max of incl and excl ` ` ` `return` `(excl ` `if` `excl>incl ` `else` `incl) ` ` ` `# Driver program to test above function ` `arr ` `=` `[` `5` `, ` `5` `, ` `10` `, ` `100` `, ` `10` `, ` `5` `] ` `print` `find_max_sum(arr) ` ` ` `# This code is contributed by Kalai Selvan ` |

*chevron_right*

*filter_none*

## C#

`/* Program to return max sum such that no ` `two elements are adjacent */` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `/* Function to return max sum such ` ` ` `that no two elements are adjacent */` ` ` `static` `int` `FindMaxSum(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `int` `incl = arr[0]; ` ` ` `int` `excl = 0; ` ` ` `int` `excl_new; ` ` ` `int` `i; ` ` ` ` ` `for` `(i = 1; i < n; i++) ` ` ` `{ ` ` ` `/* current max excluding i */` ` ` `excl_new = (incl > excl) ? ` ` ` `incl : excl; ` ` ` ` ` `/* current max including i */` ` ` `incl = excl + arr[i]; ` ` ` `excl = excl_new; ` ` ` `} ` ` ` ` ` `/* return max of incl and excl */` ` ` `return` `((incl > excl) ? ` ` ` `incl : excl); ` ` ` `} ` ` ` ` ` `// Driver program to test above ` ` ` `// functions ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = ` `new` `int` `[]{5, 5, 10, ` ` ` `100, 10, 5}; ` ` ` ` ` `Console.Write( ` ` ` `FindMaxSum(arr, arr.Length)); ` ` ` `} ` `} ` ` ` `// This code has been contributed by ` `// nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to find Maximum sum ` `// such that no two elements ` `// are adjacent ` ` ` `/* Function to return max sum ` ` ` `such that no two elements ` ` ` `are adjacent */` `function` `FindMaxSum(` `$arr` `, ` `$n` `) ` `{ ` ` ` `$incl` `= ` `$arr` `[0]; ` ` ` `$excl` `= 0; ` ` ` `$excl_new` `; ` ` ` `$i` `; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<` `$n` `; ` `$i` `++) ` `{ ` ` ` ` ` `// current max excluding i ` ` ` `$excl_new` `= (` `$incl` `> ` `$excl` `)? ` `$incl` `: ` `$excl` `; ` ` ` ` ` `// current max including i ` ` ` `$incl` `= ` `$excl` `+ ` `$arr` `[` `$i` `]; ` ` ` `$excl` `= ` `$excl_new` `; ` `} ` ` ` `// return max of incl and excl ` `return` `((` `$incl` `> ` `$excl` `)? ` `$incl` `: ` `$excl` `); ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(5, 5, 10, 100, 10, 5); ` `$n` `= sizeof(` `$arr` `); ` `echo` `FindMaxSum(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

Output:

110

**Time Complexity: **O(n)

Refer Find maximum possible stolen value from houses for more explanation.

Now try the same problem for an array with negative numbers also.

Please write comments if you find any bug in the above program/algorithm or other ways to solve the same problem.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- Ways to paint stairs with two colors such that two adjacent are not yellow
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array
- Maximum subset sum such that no two elements in set have same digit in them
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Maximum subsequence sum of at most K-distant adjacent elements
- Rearrange characters in a string such that no two adjacent are same
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Longest subsequence such that adjacent elements have at least one common digit
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing