# Find Maximum sum of non adjacent elements

Here given code implementation process.

``````/*
C program for
Find Maximum sum of non adjacent elements
*/
#include <stdio.h>

// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("  %d", arr[i]);
}
}
int maxValue(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
{
if (n <= 0)
{
return;
}
// Declare the auxiliary variables
int includeSum = arr[0];
int excludeSum = 0;
int result = 0;
// Execute this loop from 1...n
for (int i = 1; i < n; ++i)
{
// Get max value of include and exclude sum
result = maxValue(includeSum, excludeSum);
includeSum = excludeSum + arr[i];
// Assign max value
excludeSum = result;
}
result = maxValue(includeSum, excludeSum);
// Display calculated result
printf("\n  Result :  %d \n", result);
}
int main(int argc, char const *argv[])
{
int arr1[] = {
1 , 2 , 3 , -4 , -4 , -5 , 2
};
int arr2[] = {
1 , 4 , 0 , 6 , 2
};
int arr3[] = {
3 , 5 , 1 , 2
};
// Get the size of arrays
int l = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
int n = sizeof(arr3) / sizeof(arr3[0]);
// Test A
printArray(arr1, l);
/*
arr =  [1, 2, 3, -4, -4, -5, 2]

---------------------
Result sum : 6
*/
// Test B
printArray(arr2, m);
/*
arr =  [1, 4, 0, 6, 2]

---------------------
Result sum : 10
*/
// Test C
printArray(arr3, n);
/*
arr =  [3, 5, 1, 2]

---------------------
Result sum : 7
*/
return 0;
}``````

#### Output

``````  1  2  3  -4  -4  -5  2
Result :  6
1  4  0  6  2
Result :  10
3  5  1  2
Result :  7``````
``````/*
Java program for
Find Maximum sum of non adjacent elements
*/
public class MaximumSum
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
}
// Return a max value of given numbers
public int maxValue(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
public void maxNonAdjacentSum(int[] arr, int n)
{
if (n <= 0)
{
return;
}
// Declare the auxiliary variables
int includeSum = arr[0];
int excludeSum = 0;
int result = 0;
// Execute this loop from 1...n
for (int i = 1; i < n; ++i)
{
// Get max value of include and exclude sum
result = maxValue(includeSum, excludeSum);
includeSum = excludeSum + arr[i];
// Assign max value
excludeSum = result;
}
result = maxValue(includeSum, excludeSum);
// Display calculated result
System.out.print("\n Result : " + result + " \n");
}
public static void main(String[] args)
{
int[] arr1 = {
1 , 2 , 3 , -4 , -4 , -5 , 2
};
int[] arr2 = {
1 , 4 , 0 , 6 , 2
};
int[] arr3 = {
3 , 5 , 1 , 2
};
// Get the size of arrays
int l = arr1.length;
int m = arr2.length;
int n = arr3.length;
// Test A
/*
arr =  [1, 2, 3, -4, -4, -5, 2]

---------------------
Result sum : 6
*/
// Test B
/*
arr =  [1, 4, 0, 6, 2]

---------------------
Result sum : 10
*/
// Test C
/*
arr =  [3, 5, 1, 2]

---------------------
Result sum : 7
*/
}
}``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program for
Find Maximum sum of non adjacent elements
*/
class MaximumSum
{
public:
// Display array elements
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
}
// Return a max value of given numbers
int maxValue(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
{
if (n <= 0)
{
return;
}
// Declare the auxiliary variables
int includeSum = arr[0];
int excludeSum = 0;
int result = 0;
// Execute this loop from 1...n
for (int i = 1; i < n; ++i)
{
// Get max value of include and exclude sum
result = this->maxValue(includeSum, excludeSum);
includeSum = excludeSum + arr[i];
// Assign max value
excludeSum = result;
}
result = this->maxValue(includeSum, excludeSum);
// Display calculated result
cout << "\n Result : " << result << " \n";
}
};
int main()
{
int arr1[] = {
1 , 2 , 3 , -4 , -4 , -5 , 2
};
int arr2[] = {
1 , 4 , 0 , 6 , 2
};
int arr3[] = {
3 , 5 , 1 , 2
};
// Get the size of arrays
int l = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
int n = sizeof(arr3) / sizeof(arr3[0]);
// Test A
/*
arr =  [1, 2, 3, -4, -4, -5, 2]
---------------------
Result sum : 6

*/
// Test B
/*
arr =  [1, 4, 0, 6, 2]
---------------------
Result sum : 10

*/
// Test C
/*
arr =  [3, 5, 1, 2]
---------------------
Result sum : 7

*/
return 0;
}``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7``````
``````// Include namespace system
using System;
/*
Csharp program for
Find Maximum sum of non adjacent elements
*/
public class MaximumSum
{
// Display array elements
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
}
// Return a max value of given numbers
public int maxValue(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
public void maxNonAdjacentSum(int[] arr, int n)
{
if (n <= 0)
{
return;
}
// Declare the auxiliary variables
int includeSum = arr[0];
int excludeSum = 0;
int result = 0;
// Execute this loop from 1...n
for (int i = 1; i < n; ++i)
{
// Get max value of include and exclude sum
result = this.maxValue(includeSum, excludeSum);
includeSum = excludeSum + arr[i];
// Assign max value
excludeSum = result;
}
result = this.maxValue(includeSum, excludeSum);
// Display calculated result
Console.Write("\n Result : " + result + " \n");
}
public static void Main(String[] args)
{
int[] arr1 = {
1 , 2 , 3 , -4 , -4 , -5 , 2
};
int[] arr2 = {
1 , 4 , 0 , 6 , 2
};
int[] arr3 = {
3 , 5 , 1 , 2
};
// Get the size of arrays
int l = arr1.Length;
int m = arr2.Length;
int n = arr3.Length;
// Test A
/*
arr =  [1, 2, 3, -4, -4, -5, 2]
---------------------
Result sum : 6

*/
// Test B
/*
arr =  [1, 4, 0, 6, 2]
---------------------
Result sum : 10

*/
// Test C
/*
arr =  [3, 5, 1, 2]
---------------------
Result sum : 7

*/
}
}``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7``````
``````<?php
/*
Php program for
Find Maximum sum of non adjacent elements
*/
class MaximumSum
{
// Display array elements
public	function printArray(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
}
// Return a max value of given numbers
public	function maxValue(\$a, \$b)
{
if (\$a > \$b)
{
return \$a;
}
return \$b;
}
{
if (\$n <= 0)
{
return;
}
// Declare the auxiliary variables
\$includeSum = \$arr[0];
\$excludeSum = 0;
\$result = 0;
// Execute this loop from 1...n
for (\$i = 1; \$i < \$n; ++\$i)
{
// Get max value of include and exclude sum
\$result = \$this->maxValue(\$includeSum, \$excludeSum);
\$includeSum = \$excludeSum + \$arr[\$i];
// Assign max value
\$excludeSum = \$result;
}
\$result = \$this->maxValue(\$includeSum, \$excludeSum);
// Display calculated result
echo("\n Result : ".\$result." \n");
}
}

function main()
{
\$arr1 = array(1, 2, 3, -4, -4, -5, 2);
\$arr2 = array(1, 4, 0, 6, 2);
\$arr3 = array(3, 5, 1, 2);
// Get the size of arrays
\$l = count(\$arr1);
\$m = count(\$arr2);
\$n = count(\$arr3);
// Test A
/*
arr =  [1, 2, 3, -4, -4, -5, 2]
---------------------
Result sum : 6

*/
// Test B
/*
arr =  [1, 4, 0, 6, 2]
---------------------
Result sum : 10

*/
// Test C
/*
arr =  [3, 5, 1, 2]
---------------------
Result sum : 7

*/
}
main();``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7``````
``````package main
import "fmt"
/*
Go program for
Find Maximum sum of non adjacent elements
*/

// Display array elements
func printArray(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
}
// Return a max value of given numbers
func maxValue(a, b int) int {
if a > b {
return a
}
return b
}
func maxNonAdjacentSum(arr[] int, n int) {
if n <= 0 {
return
}
// Declare the auxiliary variables
var includeSum int = arr[0]
var excludeSum int = 0
var result int = 0
// Execute this loop from 1...n
for i := 1 ; i < n ; i++ {
// Get max value of include and exclude sum
result = maxValue(includeSum, excludeSum)
includeSum = excludeSum + arr[i]
// Assign max value
excludeSum = result
}
result = maxValue(includeSum, excludeSum)
// Display calculated result
fmt.Print("\n Result : ", result, " \n")
}
func main() {

var arr1 = [] int { 1 , 2 , 3 , - 4 , - 4 , - 5 , 2 }
var arr2 = [] int { 1 , 4 , 0 , 6 , 2 }
var arr3 = [] int { 3 , 5 , 1 , 2 }
// Get the size of arrays
var l int = len(arr1)
var m int = len(arr2)
var n int = len(arr3)
// Test A
printArray(arr1, l)
/*
arr =  [1, 2, 3, -4, -4, -5, 2]
---------------------
Result sum : 6

*/
// Test B
printArray(arr2, m)
/*
arr =  [1, 4, 0, 6, 2]
---------------------
Result sum : 10

*/
// Test C
printArray(arr3, n)
/*
arr =  [3, 5, 1, 2]
---------------------
Result sum : 7

*/
}``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7``````
``````/*
Node JS program for
Find Maximum sum of non adjacent elements
*/
class MaximumSum
{
// Display array elements
printArray(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
// Return a max value of given numbers
maxValue(a, b)
{
if (a > b)
{
return a;
}
return b;
}
{
if (n <= 0)
{
return;
}
// Declare the auxiliary variables
var includeSum = arr[0];
var excludeSum = 0;
var result = 0;
// Execute this loop from 1...n
for (var i = 1; i < n; ++i)
{
// Get max value of include and exclude sum
result = this.maxValue(includeSum, excludeSum);
includeSum = excludeSum + arr[i];
// Assign max value
excludeSum = result;
}
result = this.maxValue(includeSum, excludeSum);
// Display calculated result
process.stdout.write("\n Result : " + result + " \n");
}
}

function main()
{
var arr1 = [1, 2, 3, -4, -4, -5, 2];
var arr2 = [1, 4, 0, 6, 2];
var arr3 = [3, 5, 1, 2];
// Get the size of arrays
var l = arr1.length;
var m = arr2.length;
var n = arr3.length;
// Test A
/*
arr =  [1, 2, 3, -4, -4, -5, 2]
---------------------
Result sum : 6

*/
// Test B
/*
arr =  [1, 4, 0, 6, 2]
---------------------
Result sum : 10

*/
// Test C
/*
arr =  [3, 5, 1, 2]
---------------------
Result sum : 7

*/
}
main();``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7``````
``````#    Python 3 program for
#    Find Maximum sum of non adjacent elements
class MaximumSum :
#  Display list elements
def printArray(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

#  Return a max value of given numbers
def maxValue(self, a, b) :
if (a > b) :
return a

return b

if (n <= 0) :
return

#  Declare the auxiliary variables
includeSum = arr[0]
excludeSum = 0
result = 0
i = 1
#  Execute this loop from 1...n
while (i < n) :
#  Get max value of include and exclude sum
result = self.maxValue(includeSum, excludeSum)
includeSum = excludeSum + arr[i]
#  Assign max value
excludeSum = result
i += 1

result = self.maxValue(includeSum, excludeSum)
#  Display calculated result
print("\n Result : ", result ," ")

def main() :
arr1 = [1, 2, 3, -4, -4, -5, 2]
arr2 = [1, 4, 0, 6, 2]
arr3 = [3, 5, 1, 2]
#  Get the size of lists
l = len(arr1)
m = len(arr2)
n = len(arr3)
#  Test A
#   arr =  [1, 2, 3, -4, -4, -5, 2]
#   ---------------------
#   Result sum : 6
#  Test B
#   arr =  [1, 4, 0, 6, 2]
#   ---------------------
#   Result sum : 10
#  Test C
#   arr =  [3, 5, 1, 2]
#   ---------------------
#   Result sum : 7

if __name__ == "__main__": main()``````

#### Output

``````  1  2  3  -4  -4  -5  2
Result :  6
1  4  0  6  2
Result :  10
3  5  1  2
Result :  7``````
``````#    Ruby program for
#    Find Maximum sum of non adjacent elements
class MaximumSum
#  Display array elements
def printArray(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

end

#  Return a max value of given numbers
def maxValue(a, b)
if (a > b)
return a
end

return b
end

if (n <= 0)
return
end

#  Declare the auxiliary variables
includeSum = arr[0]
excludeSum = 0
result = 0
i = 1
#  Execute this loop from 1...n
while (i < n)
#  Get max value of include and exclude sum
result = self.maxValue(includeSum, excludeSum)
includeSum = excludeSum + arr[i]
#  Assign max value
excludeSum = result
i += 1
end

result = self.maxValue(includeSum, excludeSum)
#  Display calculated result
print("\n Result : ", result ," \n")
end

end

def main()
arr1 = [1, 2, 3, -4, -4, -5, 2]
arr2 = [1, 4, 0, 6, 2]
arr3 = [3, 5, 1, 2]
#  Get the size of arrays
l = arr1.length
m = arr2.length
n = arr3.length
#  Test A
#   arr =  [1, 2, 3, -4, -4, -5, 2]
#   ---------------------
#   Result sum : 6
#  Test B
#   arr =  [1, 4, 0, 6, 2]
#   ---------------------
#   Result sum : 10
#  Test C
#   arr =  [3, 5, 1, 2]
#   ---------------------
#   Result sum : 7
end

main()``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7
``````
``````/*
Scala program for
Find Maximum sum of non adjacent elements
*/
class MaximumSum()
{
// Display array elements
def printArray(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
}
// Return a max value of given numbers
def maxValue(a: Int, b: Int): Int = {
if (a > b)
{
return a;
}
return b;
}
def maxNonAdjacentSum(arr: Array[Int], n: Int): Unit = {
if (n <= 0)
{
return;
}
// Declare the auxiliary variables
var includeSum: Int = arr(0);
var excludeSum: Int = 0;
var result: Int = 0;
var i: Int = 1;
// Execute this loop from 1...n
while (i < n)
{
// Get max value of include and exclude sum
result = maxValue(includeSum, excludeSum);
includeSum = excludeSum + arr(i);
// Assign max value
excludeSum = result;
i += 1;
}
result = maxValue(includeSum, excludeSum);
// Display calculated result
print("\n Result : " + result + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: MaximumSum = new MaximumSum();
var arr1: Array[Int] = Array(1, 2, 3, -4, -4, -5, 2);
var arr2: Array[Int] = Array(1, 4, 0, 6, 2);
var arr3: Array[Int] = Array(3, 5, 1, 2);
// Get the size of arrays
var l: Int = arr1.length;
var m: Int = arr2.length;
var n: Int = arr3.length;
// Test A
/*
arr =  [1, 2, 3, -4, -4, -5, 2]
---------------------
Result sum : 6

*/
// Test B
/*
arr =  [1, 4, 0, 6, 2]
---------------------
Result sum : 10

*/
// Test C
/*
arr =  [3, 5, 1, 2]
---------------------
Result sum : 7

*/
}
}``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7``````
``````import Foundation;
/*
Swift 4 program for
Find Maximum sum of non adjacent elements
*/
class MaximumSum
{
// Display array elements
func printArray(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
}
// Return a max value of given numbers
func maxValue(_ a: Int, _ b: Int) -> Int
{
if (a > b)
{
return a;
}
return b;
}
func maxNonAdjacentSum(_ arr: [Int], _ n: Int)
{
if (n <= 0)
{
return;
}
// Declare the auxiliary variables
var includeSum: Int = arr[0];
var excludeSum: Int = 0;
var result: Int = 0;
var i: Int = 1;
// Execute this loop from 1...n
while (i < n)
{
// Get max value of include and exclude sum
result = self.maxValue(includeSum, excludeSum);
includeSum = excludeSum + arr[i];
// Assign max value
excludeSum = result;
i += 1;
}
result = self.maxValue(includeSum, excludeSum);
// Display calculated result
print("\n Result : ", result ," ");
}
}
func main()
{
let arr1: [Int] = [1, 2, 3, -4, -4, -5, 2];
let arr2: [Int] = [1, 4, 0, 6, 2];
let arr3: [Int] = [3, 5, 1, 2];
// Get the size of arrays
let l: Int = arr1.count;
let m: Int = arr2.count;
let n: Int = arr3.count;
// Test A
/*
arr =  [1, 2, 3, -4, -4, -5, 2]
---------------------
Result sum : 6

*/
// Test B
/*
arr =  [1, 4, 0, 6, 2]
---------------------
Result sum : 10

*/
// Test C
/*
arr =  [3, 5, 1, 2]
---------------------
Result sum : 7

*/
}
main();``````

#### Output

``````  1  2  3  -4  -4  -5  2
Result :  6
1  4  0  6  2
Result :  10
3  5  1  2
Result :  7``````
``````/*
Kotlin program for
Find Maximum sum of non adjacent elements
*/
class MaximumSum
{
// Display array elements
fun printArray(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
}
// Return a max value of given numbers
fun maxValue(a: Int, b: Int): Int
{
if (a > b)
{
return a;
}
return b;
}
fun maxNonAdjacentSum(arr: Array < Int > , n: Int): Unit
{
if (n <= 0)
{
return;
}
// Declare the auxiliary variables
var includeSum: Int = arr[0];
var excludeSum: Int = 0;
var result: Int ;
var i: Int = 1;
// Execute this loop from 1...n
while (i < n)
{
// Get max value of include and exclude sum
result = this.maxValue(includeSum, excludeSum);
includeSum = excludeSum + arr[i];
// Assign max value
excludeSum = result;
i += 1;
}
result = this.maxValue(includeSum, excludeSum);
// Display calculated result
print("\n Result : " + result + " \n");
}
}
fun main(args: Array < String > ): Unit
{
val arr1: Array < Int > = arrayOf(1, 2, 3, -4, -4, -5, 2);
val arr2: Array < Int > = arrayOf(1, 4, 0, 6, 2);
val arr3: Array < Int > = arrayOf(3, 5, 1, 2);
// Get the size of arrays
val l: Int = arr1.count();
val m: Int = arr2.count();
val n: Int = arr3.count();
// Test A
/*
arr =  [1, 2, 3, -4, -4, -5, 2]
---------------------
Result sum : 6

*/
// Test B
/*
arr =  [1, 4, 0, 6, 2]
---------------------
Result sum : 10

*/
// Test C
/*
arr =  [3, 5, 1, 2]
---------------------
Result sum : 7

*/
}``````

#### Output

`````` 1 2 3 -4 -4 -5 2
Result : 6
1 4 0 6 2
Result : 10
3 5 1 2
Result : 7``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.