Given an array arr[] consisting of N positive integers, and an integer K, the task is to find the maximum possible even sum of any subsequence of size K. If it is not possible to find any even sum subsequence of size K, then print -1.
Examples:
Input: arr[] ={4, 2, 6, 7, 8}, K = 3
Output: 18
Explanation: Subsequence having maximum even sum of size K( = 3 ) is {4, 6, 8}.
Therefore, the required output is 4 + 6 + 8 = 18.
Input: arr[] = {5, 5, 1, 1, 3}, K = 3
Output: -1
Naive Approach: The simplest approach to solve this problem to generate all possible subsequences of size K from the given array and print the value of the maximum possible even sum the possible subsequences of the given array.
Time Complexity: O(K * NK)
Auxiliary Space: O(K)
Efficient Approach: To optimize the above approach the idea is to store all even and odd numbers of the given array into two separate arrays and sort both these arrays. Finally, use the Greedy technique to calculate the maximum sum even subsequence of size K. Follow the steps below to solve the problem:
- Initialize a variable, say maxSum to store the maximum even sum of a subsequence of the given array.
- Initialize two arrays, say Even[] and Odd[] to store all the even numbers and odd numbers of the given array respectively.
- Traverse the given array and store all the even numbers and odd numbers of the given array into Even[] and Odd[] array respectively.
- Sort Even[] and Odd[] arrays.
- Initialize two variables, say i and j to store the index of Even[] and Odd[] array respectively.
- Traverse Even[], Odd[] arrays and check the following conditions:
- If K % 2 == 1 then increment the value of maxSum by Even[i].
- Otherwise, increment the value of maxSum by max(Even[i] + Even[i – 1], Odd[j] + Odd[j – 1]).
- Finally, print the value of maxSum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int evenSumK( int arr[], int N, int K)
{
if (K > N) {
return -1;
}
int maxSum = 0;
vector< int > Even;
vector< int > Odd;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2) {
Odd.push_back(arr[i]);
}
else {
Even.push_back(arr[i]);
}
}
sort(Odd.begin(), Odd.end());
sort(Even.begin(), Even.end());
int i = Even.size() - 1;
int j = Odd.size() - 1;
while (K > 0) {
if (K % 2 == 1) {
if (i >= 0) {
maxSum += Even[i];
i--;
}
else {
return -1;
}
K--;
}
else if (i >= 1 && j >= 1) {
if (Even[i] + Even[i - 1]
<= Odd[j] + Odd[j - 1]) {
maxSum += Odd[j] + Odd[j - 1];
j -= 2;
}
else {
maxSum += Even[i] + Even[i - 1];
i -= 2;
}
K -= 2;
}
else if (i >= 1) {
maxSum += Even[i] + Even[i - 1];
i -= 2;
K -= 2;
}
else if (j >= 1) {
maxSum += Odd[j] + Odd[j - 1];
j -= 2;
K -= 2;
} else {
return -1;
}
}
return maxSum;
}
int main()
{
int arr[] = { 2, 4, 10, 3, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 3;
cout << evenSumK(arr, N, K);
}
|
Java
import java.util.*;
class GFG {
static int evenSumK( int arr[], int N, int K)
{
if (K > N) {
return - 1 ;
}
int maxSum = 0 ;
ArrayList<Integer> Even = new ArrayList<Integer>();
ArrayList<Integer> Odd = new ArrayList<Integer>();
for ( int i = 0 ; i < N; i++) {
if (arr[i] % 2 == 1 ) {
Odd.add(arr[i]);
}
else {
Even.add(arr[i]);
}
}
Collections.sort(Odd);
Collections.sort(Even);
int i = Even.size() - 1 ;
int j = Odd.size() - 1 ;
while (K > 0 ) {
if (K % 2 == 1 ) {
if (i >= 0 ) {
maxSum += Even.get(i);
i--;
}
else {
return - 1 ;
}
K--;
}
else if (i >= 1 && j >= 1 ) {
if (Even.get(i) + Even.get(i - 1 )
<= Odd.get(j) + Odd.get(j - 1 )) {
maxSum += Odd.get(j) + Odd.get(j - 1 );
j -= 2 ;
}
else {
maxSum += Even.get(i) + Even.get(i - 1 );
i -= 2 ;
}
K -= 2 ;
}
else if (i >= 1 ) {
maxSum += Even.get(i) + Even.get(i - 1 );
i -= 2 ;
K -= 2 ;
}
else if (j >= 1 ) {
maxSum += Odd.get(j) + Odd.get(j - 1 );
j -= 2 ;
K -= 2 ;
}
else
return - 1 ;
}
return maxSum;
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 10 , 3 , 5 };
int N = arr.length;
int K = 3 ;
System.out.println(evenSumK(arr, N, K));
}
}
|
Python3
def evenSumK(arr, N, K):
if (K > N):
return - 1
maxSum = 0
Even = []
Odd = []
for i in range (N):
if (arr[i] % 2 ):
Odd.append(arr[i])
else :
Even.append(arr[i])
Odd.sort(reverse = False )
Even.sort(reverse = False )
i = len (Even) - 1
j = len (Odd) - 1
while (K > 0 ):
if (K % 2 = = 1 ):
if (i > = 0 ):
maxSum + = Even[i]
i - = 1
else :
return - 1
K - = 1
elif (i > = 1 and j > = 1 ):
if (Even[i] + Even[i - 1 ] < =
Odd[j] + Odd[j - 1 ]):
maxSum + = Odd[j] + Odd[j - 1 ]
j - = 2
else :
maxSum + = Even[i] + Even[i - 1 ]
i - = 2
K - = 2
elif (i > = 1 ):
maxSum + = Even[i] + Even[i - 1 ]
i - = 2
K - = 2
elif (j > = 1 ):
maxSum + = Odd[j] + Odd[j - 1 ]
j - = 2
K - = 2
else :
return - 1 ;
return maxSum
if __name__ = = '__main__' :
arr = [ 2 , 4 , 9 ]
N = len (arr)
K = 3
print (evenSumK(arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int evenSumK( int [] arr, int N, int K)
{
if (K > N) {
return -1;
}
int maxSum = 0;
List< int > Even = new List< int >();
List< int > Odd = new List< int >();
for ( int l = 0; l < N; l++) {
if (arr[l] % 2 == 1) {
Odd.Add(arr[l]);
}
else {
Even.Add(arr[l]);
}
}
Odd.Sort();
Even.Sort();
int i = Even.Count - 1;
int j = Odd.Count - 1;
while (K > 0) {
if (K % 2 == 1) {
if (i >= 0) {
maxSum += Even[i];
i--;
}
else {
return -1;
}
K--;
}
else if (i >= 1 && j >= 1) {
if (Even[i] + Even[i - 1]
<= Odd[j] + Odd[j - 1]) {
maxSum += Odd[j] + Odd[j - 1];
j -= 2;
}
else {
maxSum += Even[i] + Even[i - 1];
i -= 2;
}
K -= 2;
}
else if (i >= 1) {
maxSum += Even[i] + Even[i - 1];
i -= 2;
K -= 2;
}
else if (j >= 1) {
maxSum += Odd[j] + Odd[j - 1];
j -= 2;
K -= 2;
}
else
return -1;
}
return maxSum;
}
public static void Main(String[] args)
{
int [] arr = { 2, 4, 10, 3, 5 };
int N = arr.Length;
int K = 3;
Console.WriteLine(evenSumK(arr, N, K));
}
}
|
Javascript
<script>
function evenSumK(arr, N, K)
{
if (K > N) {
return -1;
}
var maxSum = 0;
var Even = [];
var Odd = [];
for ( var i = 0; i < N; i++) {
if (arr[i] % 2) {
Odd.push(arr[i]);
}
else {
Even.push(arr[i]);
}
}
Odd.sort((a,b)=> a-b);
Even.sort((a,b)=> a-b);
var i = Even.length - 1;
var j = Odd.length - 1;
while (K > 0) {
if (K % 2 == 1) {
if (i >= 0) {
maxSum += Even[i];
i--;
}
else {
return -1;
}
K--;
}
else if (i >= 1 && j >= 1) {
if (Even[i] + Even[i - 1]
<= Odd[j] + Odd[j - 1]) {
maxSum += Odd[j] + Odd[j - 1];
j -= 2;
}
else {
maxSum += Even[i] + Even[i - 1];
i -= 2;
}
K -= 2;
}
else if (i >= 1) {
maxSum += Even[i] + Even[i - 1];
i -= 2;
K -= 2;
}
else if (j >= 1) {
maxSum += Odd[j] + Odd[j - 1];
j -= 2;
K -= 2;
}
else
return -1;
}
return maxSum;
}
var arr = [2, 4, 10, 3, 5];
var N = arr.length;
var K = 3;
document.write( evenSumK(arr, N, K));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)