Maximize value of K such that a subsequence with sum i exists for each integer i in range [1, K]
Last Updated :
29 Jul, 2021
Given an array arr[] consisting of N integers, the task is to find the maximum value of K such that for every integer i over the range [1, K] there exists a subsequence whose sum is i.
Examples:
Input: arr[] = {1, 2, 1, 3}
Output: 7
Explanation:
Below are the possible values of the sum for all the subsequences that can be formed:
- Subsequence {1}, the sum of elements is 1.
- Subsequence {2}, the sum of elements is 2.
- Subsequence {3}, the sum of elements is 3.
- Subsequence {1, 3}, the sum of elements is 4.
- Subsequence {1, 1, 3}, the sum of elements is 5.
- Subsequence {1, 2, 3}, the sum of elements is 6.
- Subsequence {1, 1, 2, 3}, the sum of elements is 7.
Hence, the maximum value of K is 7. Hence print 7.
Input: arr[] = {2, 5, 2, 3}
Output: 0
Approach: Follow the steps below to solve the given problem:
- Sort the given array arr[].
- Initialize a variable, say next as 0 to store the resultant maximum value of K.
- Traverse the array arr[] over the range [0, N – 1] and perform the following steps:
- If the value of arr[i] is less than or equal to (next + 1), then the subsequences {0, 1, 2, …, next + arr[i]} can be generated using the array elements. Therefore, add the value arr[i] to the variable next.
- Otherwise, break out of the loop as the sum (next + 1) cannot be generated by any subsequence of the array, all the values followed by arr[i] are greater than that.
- After completing the above steps, print the value of the next as the resultant maximum value of K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximumK(vector< int >& arr)
{
sort(arr.begin(), arr.end());
int N = arr.size();
int next = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] > next + 1)
break ;
next += arr[i];
}
cout << next << endl;
}
int main()
{
vector< int > arr = { 1, 2, 1, 3 };
maximumK(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
static void maximumK( int [] arr)
{
Arrays.sort(arr);
int N = arr.length;
int next = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] > next + 1 )
break ;
next += arr[i];
}
System.out.println(next);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 1 , 3 };
maximumK(arr);
}
}
|
Python3
def maximumK(arr):
arr.sort();
N = len (arr);
next = 0 ;
for i in range (N):
if (arr[i] > next + 1 ):
break ;
next + = arr[i];
print ( next );
arr = [ 1 , 2 , 1 , 3 ];
maximumK(arr);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void maximumK(List< int > arr)
{
arr.Sort();
int N = arr.Count;
int next = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] > next + 1)
break ;
next += arr[i];
}
Console.Write(next);
}
public static void Main()
{
List< int > arr = new List< int >(){ 1, 2, 1, 3 };
maximumK(arr);
}
}
|
Javascript
<script>
function maximumK(arr) {
arr.sort((a, b) => a - b);
let N = arr.length;
let next = 0;
for (let i = 0; i < N; i++) {
if (arr[i] > next + 1)
break ;
next += arr[i];
}
document.write(next);
}
let arr = [1, 2, 1, 3];
maximumK(arr);
</script>
|
Time Complexity: O(N* log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...