Open In App

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
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Subsequence {1}, the sum of elements is 1.
  2. Subsequence {2}, the sum of elements is  2.
  3. Subsequence {3}, the sum of elements is 3.
  4. Subsequence {1, 3}, the sum of elements is 4.
  5. Subsequence {1, 1, 3}, the sum of elements is 5.
  6. Subsequence {1, 2, 3}, the sum of elements is 6.
  7. 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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum value
// of K such that for all value i over
// the range [1, K] there exists a
// subsequence whose value is i
void maximumK(vector<int>& arr)
{
    // Sort the given array
    sort(arr.begin(), arr.end());
 
    int N = arr.size();
 
    // Stores the maximum value up to
    // which subsequences can be
    // generated by the array
    int next = 0;
 
    // Traverse the given array
    for (int i = 0; i < N; i++) {
 
        // Check if the current element
        // is greater than next + 1
        if (arr[i] > next + 1)
            break;
 
        // Add the contribution from
        // the current element and
        // update the next value
        next += arr[i];
    }
 
    // Print the answer
    cout << next << endl;
}
 
// Driver Code
int main()
{
    vector<int> arr = { 1, 2, 1, 3 };
    maximumK(arr);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.util.Arrays;
 
class GFG
 
{
    // Function to find the maximum value
    // of K such that for all value i over
    // the range [1, K] there exists a
    // subsequence whose value is i
    static void maximumK(int[] arr)
    {
       
        // Sort the given array
        Arrays.sort(arr);
 
        int N = arr.length;
 
        // Stores the maximum value up to
        // which subsequences can be
        // generated by the array
        int next = 0;
 
        // Traverse the given array
        for (int i = 0; i < N; i++) {
 
            // Check if the current element
            // is greater than next + 1
            if (arr[i] > next + 1)
                break;
 
            // Add the contribution from
            // the current element and
            // update the next value
            next += arr[i];
        }
 
        // Print the answer
        System.out.println(next);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 1, 3 };
        maximumK(arr);
    }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python program for the above approach
 
# Function to find the maximum value
# of K such that for all value i over
# the range [1, K] there exists a
# subsequence whose value is i
def maximumK(arr):
    # Sort the given array
    arr.sort();
 
    N = len(arr);
 
    # Stores the maximum value up to
    # which subsequences can be
    # generated by the array
    next = 0;
 
    # Traverse the given array
    for i in range(N):
 
        # Check if the current element
        # is greater than next + 1
        if (arr[i] > next + 1):
            break;
 
        # Add the contribution from
        # the current element and
        # update the next value
        next += arr[i];
 
    # Print the answer
    print(next);
 
# Driver Code
 
arr = [1, 2, 1, 3];
maximumK(arr);
 
# This code is contributed by gfgking


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find the maximum value
// of K such that for all value i over
// the range [1, K] there exists a
// subsequence whose value is i
static void maximumK(List<int> arr)
{
    // Sort the given array
    arr.Sort();
    int N = arr.Count;
 
    // Stores the maximum value up to
    // which subsequences can be
    // generated by the array
    int next = 0;
 
    // Traverse the given array
    for (int i = 0; i < N; i++) {
 
        // Check if the current element
        // is greater than next + 1
        if (arr[i] > next + 1)
            break;
 
        // Add the contribution from
        // the current element and
        // update the next value
        next += arr[i];
    }
 
    // Print the answer
    Console.Write(next);
}
 
// Driver Code
public static void Main()
{
    List<int> arr = new List<int>(){ 1, 2, 1, 3 };
    maximumK(arr);
}
}
 
// This code is contributed by ipg2016107.


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to find the maximum value
// of K such that for all value i over
// the range [1, K] there exists a
// subsequence whose value is i
function maximumK(arr) {
    // Sort the given array
    arr.sort((a, b) => a - b);
 
    let N = arr.length;
 
    // Stores the maximum value up to
    // which subsequences can be
    // generated by the array
    let next = 0;
 
    // Traverse the given array
    for (let i = 0; i < N; i++) {
 
        // Check if the current element
        // is greater than next + 1
        if (arr[i] > next + 1)
            break;
 
        // Add the contribution from
        // the current element and
        // update the next value
        next += arr[i];
    }
 
    // Print the answer
    document.write(next);
}
 
// Driver Code
 
let arr = [1, 2, 1, 3];
maximumK(arr);
 
// This code is contributed by _saurabh_jaiswal.
</script>


Output: 

7

 

Time Complexity: O(N* log N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads