Maximum even sum of a pair of given Array
Last Updated :
09 Dec, 2022
Given an array arr[] of N distinct non-negative integers, the task is to determine if there is an even number that can be represented as the sum of two different elements of arr. If it exists return the maximum such number otherwise return -1.
Examples:
Input: N = 4, arr[] = {2, 3, 4, 5}
Output: 8
Explanation: The values that can be represented as the sum of two distinct elements of arr are 5, 6, 7, 8 and 9.
We have two even number here, and the maximum is 8.
Input: N = 2, arr[] = {0, 3}
Output: -1
Explanation: The value represented as the sum of two distinct elements of arr is 1.
We have no even number here, so -1 should be printed.
Naive Approach: The basic approach is:
Consider each pair sum by nested traversal and consider the maximum of all such pair sums that are even and present in the array.
Follow the steps to solve the problem:
- Initialize the result as 0 at starting.
- Traverse the array using a loop from 0 to N – 2
- For each element consider pairing with every element in the range i + 1 to N – 1 and check if the pair sum is even or odd.
- If the sum is even, update the result with the maximum pair sum at each step.
- At last, check if the result is 0 then return -1 otherwise return the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maxEvenSumPair( int arr[], int n)
{
int result = 0;
for ( int i = 0; i < (n - 1); i++) {
for ( int j = (i + 1); j < n; j++) {
int curSum = arr[i] + arr[j];
if (curSum % 2 == 0)
result = max(result, curSum);
}
}
if (result == 0)
return -1;
return result;
}
int main()
{
int arr[] = { 2, 3, 4, 5 };
int size = sizeof (arr) / sizeof (arr[0]);
cout << maxEvenSumPair(arr, size);
return 0;
}
|
Java
class GFG {
public static int maxEvenSumPair( int arr[], int n) {
int result = 0 ;
for ( int i = 0 ; i < (n - 1 ); i++) {
for ( int j = (i + 1 ); j < n; j++) {
int curSum = arr[i] + arr[j];
if (curSum % 2 == 0 )
result = max(result, curSum);
}
}
if (result == 0 )
return - 1 ;
return result;
}
public static int max( int a, int b) {
if (a > b) return a;
return b;
}
public static void main(String[] args) {
int arr[] = { 2 , 3 , 4 , 5 };
int size = arr.length;
System.out.println(maxEvenSumPair(arr, size));
}
}
|
Python3
def maxEvenSumPair(arr, n) :
result = 0 ;
for i in range (n - 1 ) :
for j in range (i + 1 , n) :
curSum = arr[i] + arr[j];
if (curSum % 2 = = 0 ) :
result = max (result, curSum);
if (result = = 0 ) :
return - 1 ;
return result;
if __name__ = = "__main__" :
arr = [ 2 , 3 , 4 , 5 ];
size = len (arr);
print (maxEvenSumPair(arr, size));
|
C#
using System;
class HelloWorld
{
static int maxEvenSumPair( int [] arr, int n)
{
int result = 0;
for ( int i = 0; i < (n - 1); i++) {
for ( int j = (i + 1); j < n; j++) {
int curSum = arr[i] + arr[j];
if (curSum % 2 == 0)
result = Math.Max(result, curSum);
}
}
if (result == 0)
return -1;
return result;
}
static void Main()
{
int [] arr = { 2, 3, 4, 5 };
int size = 4;
Console.Write(maxEvenSumPair(arr, size));
}
}
|
Javascript
<script>
function maxEvenSumPair(arr, n) {
let result = 0;
for (let i = 0; i < (n - 1); i++) {
for (let j = (i + 1); j < n; j++) {
let curSum = arr[i] + arr[j];
if (curSum % 2 == 0)
result = Math.max(result, curSum);
}
}
if (result == 0)
return -1;
return result;
}
let arr = [2, 3, 4, 5];
let size = arr.length;
document.write(maxEvenSumPair(arr, size));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Find maximum sum Pair by finding maximum even and odd numbers:
To solve the problem efficiently follow the below idea:
The key point/ idea is that sum of any two even numbers or any two odd numbers is always even. Find two greatest even numbers and two greatest odd numbers, the final result will be the maximum sum of the two odd numbers and the sum of the two even numbers.
Follow the steps to solve the problem:
- Initialize first and second maximum odd and even numbers equal to -1.
- Traverse the array to find the first maximum even and first maximum odd number.
- Traverse the array a second time to find the second maximum odd and second maximum even number.
- Calculate the sum of the pair of maximum two odd and the pair of maximum two even numbers and consider the result equal to the maximum among them.
- If the result is zero it means no even sum pair exist so return -1, otherwise return the maximum result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxEvenSumPair( int arr[], int n)
{
int firstEvenMax = -1, secondEvenMax = -1,
firstOddMax = -1, secondOddMax = -1;
for ( int i = 0; i < n; i++) {
if (arr[i] & 1)
firstOddMax = max(firstOddMax, arr[i]);
else
firstEvenMax = max(firstEvenMax, arr[i]);
}
for ( int i = 0; i < n; i++) {
if (arr[i] & 1) {
if (arr[i] != firstOddMax)
secondOddMax = max(secondOddMax, arr[i]);
}
else {
if (arr[i] != firstEvenMax)
secondEvenMax = max(secondEvenMax, arr[i]);
}
}
int sumOdd = 0, sumEven = 0;
if (firstEvenMax != -1 and secondEvenMax != -1)
sumEven = firstEvenMax + secondEvenMax;
if (firstOddMax != -1 and secondOddMax != -1)
sumOdd = firstOddMax + secondOddMax;
int res = max(sumEven, sumOdd);
if (res == 0)
return -1;
return res;
}
int main()
{
int arr[] = { 2, 3, 4, 5 };
int size = sizeof (arr) / sizeof (arr[0]);
cout << maxEvenSumPair(arr, size);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int maxEvenSumPair( int arr[], int n)
{
int firstEvenMax = - 1 , secondEvenMax = - 1 ,
firstOddMax = - 1 , secondOddMax = - 1 ;
for ( int i = 0 ; i < n; i++) {
if ((arr[i] & 1 )== 1 )
firstOddMax = Math.max(firstOddMax, arr[i]);
else
firstEvenMax = Math.max(firstEvenMax, arr[i]);
}
for ( int i = 0 ; i < n; i++) {
if ((arr[i] & 1 )== 1 ) {
if (arr[i] != firstOddMax)
secondOddMax = Math.max(secondOddMax, arr[i]);
}
else {
if (arr[i] != firstEvenMax)
secondEvenMax = Math.max(secondEvenMax, arr[i]);
}
}
int sumOdd = 0 , sumEven = 0 ;
if (firstEvenMax != - 1 && secondEvenMax != - 1 )
sumEven = firstEvenMax + secondEvenMax;
if (firstOddMax != - 1 && secondOddMax != - 1 )
sumOdd = firstOddMax + secondOddMax;
int res = Math.max(sumEven, sumOdd);
if (res == 0 )
return - 1 ;
return res;
}
public static void main (String[] args) {
int arr[] = { 2 , 3 , 4 , 5 };
int size = arr.length;
System.out.println(maxEvenSumPair(arr, size));
}
}
|
Python3
def maxEvenSumPair(arr, n) :
firstEvenMax = - 1 ; secondEvenMax = - 1 ;
firstOddMax = - 1 ; secondOddMax = - 1 ;
for i in range (n) :
if (arr[i] & 1 ) :
firstOddMax = max (firstOddMax, arr[i]);
else :
firstEvenMax = max (firstEvenMax, arr[i]);
for i in range (n) :
if (arr[i] & 1 ) :
if (arr[i] ! = firstOddMax) :
secondOddMax = max (secondOddMax, arr[i]);
else :
if (arr[i] ! = firstEvenMax) :
secondEvenMax = max (secondEvenMax, arr[i]);
sumOdd = 0 ; sumEven = 0 ;
if (firstEvenMax ! = - 1 and secondEvenMax ! = - 1 ) :
sumEven = firstEvenMax + secondEvenMax;
if (firstOddMax ! = - 1 and secondOddMax ! = - 1 ) :
sumOdd = firstOddMax + secondOddMax;
res = max (sumEven, sumOdd);
if (res = = 0 ) :
return - 1 ;
return res;
if __name__ = = "__main__" :
arr = [ 2 , 3 , 4 , 5 ];
size = len (arr);
print (maxEvenSumPair(arr, size));
|
C#
using System;
class GFG {
static int maxEvenSumPair( int [] arr, int n)
{
int firstEvenMax = -1, secondEvenMax = -1,
firstOddMax = -1, secondOddMax = -1;
for ( int i = 0; i < n; i++) {
if ((arr[i] & 1) != 0)
firstOddMax = Math.Max(firstOddMax, arr[i]);
else
firstEvenMax
= Math.Max(firstEvenMax, arr[i]);
}
for ( int i = 0; i < n; i++) {
if ((arr[i] & 1) != 0) {
if (arr[i] != firstOddMax)
secondOddMax
= Math.Max(secondOddMax, arr[i]);
}
else {
if (arr[i] != firstEvenMax)
secondEvenMax
= Math.Max(secondEvenMax, arr[i]);
}
}
int sumOdd = 0, sumEven = 0;
if (firstEvenMax != -1 && secondEvenMax != -1)
sumEven = firstEvenMax + secondEvenMax;
if (firstOddMax != -1 && secondOddMax != -1)
sumOdd = firstOddMax + secondOddMax;
int res = Math.Max(sumEven, sumOdd);
if (res == 0)
return -1;
return res;
}
static void Main()
{
int [] arr = { 2, 3, 4, 5 };
int size = arr.Length;
Console.Write(maxEvenSumPair(arr, size));
}
}
|
Javascript
<script>
function maxEvenSumPair(arr, n)
{
let firstEvenMax = -1;
let secondEvenMax = -1;
let firstOddMax = -1;
let secondOddMax = -1;
for (let i = 0; i < n; i++) {
if (arr[i] & 1)
firstOddMax = Math.max(firstOddMax, arr[i]);
else
firstEvenMax = Math.max(firstEvenMax, arr[i]);
}
for (let i = 0; i < n; i++) {
if (arr[i] & 1) {
if (arr[i] != firstOddMax)
secondOddMax = Math.max(secondOddMax, arr[i]);
}
else {
if (arr[i] != firstEvenMax)
secondEvenMax = Math.max(secondEvenMax, arr[i]);
}
}
let sumOdd = 0;
let sumEven = 0;
if (firstEvenMax != -1 && secondEvenMax != -1)
sumEven = firstEvenMax + secondEvenMax;
if (firstOddMax != -1 && secondOddMax != -1)
sumOdd = firstOddMax + secondOddMax;
let res = Math.max(sumEven, sumOdd);
if (res == 0)
return -1;
return res;
}
let arr = [ 2, 3, 4, 5 ];
let size = arr.length;
console.log(maxEvenSumPair(arr, size));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...