Minimum count of elements to be inserted in Array to form all values in [1, K] using subset sum
Given a sorted array arr[] consisting of N integers, and an integer K, the task is to find the minimum number of elements to be inserted in the array such that any value in the range [1, K] can be formed by adding elements of any subset of the modified array.
Examples:
Input: arr[] = {1, 3}, K = 6
Output: 1
Explanation:
Adding the number 2 to the array modifies the array arr[] to {1, 2, 3}. Now, every sum value over the range [1, 6] can be formed:
- Sum = 1: The subset is {1}.
- Sum = 2: The subset is {2}.
- Sum = 3: The subset is {3}.
- Sum = 4: The subset is {1, 3}.
- Sum = 5: The subset is {2, 3}.
- Sum = 6: The subset is {1, 2, 3}.
Therefore, the minimum number of elements to be added is 1.
Input: arr[] = {1, 5, 10}, K = 20
Output: 2
Approach: The given problem can be solved by using the Greedy Approach which is based on the following observations:
- Consider X as the maximum number such that all the numbers in the range [1, X] can be formed by summing any subset of the array.
- Then it can be observed that by adding an integer Y to the array, the range modifies to [1, X+Y] as now every number in the range [X, X+Y] can be formed.
- Therefore, the idea is to greedily add the element which will increase the range most and will not skip any numbers in the new range obtained. It can be done by adding X every time to the array and modifying the X to 2*X.
Follow the steps below to solve the problem:
- Initialize two integer variables, say i and count as 0, to store the index of the array elements, and the count of elements needed respectively.
- Initialize a variable, say requiredNum as 1, to store the numbers up to which every number can be formed.
- Iterate until requiredNum ? K, and perform the following operations:
- If i < N and requiredNum >= arr[i] then increment requiredNum by arr[i] and i by 1.
- Otherwise, increment requiredNum by requiredNum, and count by 1.
- Finally, after completing the above steps, print the answer obtained in count.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int minElements( int arr[], int N, int K)
{
int count = 0;
long long requiredNum = 1;
int i = 0;
while (requiredNum <= K) {
if (i < N && requiredNum >= arr[i]) {
requiredNum += arr[i];
i++;
}
else {
count++;
requiredNum += requiredNum;
}
}
return count;
}
int main()
{
int arr[] = { 1, 3 };
int K = 6;
int N = sizeof (arr) / sizeof (arr[0]);
cout << minElements(arr, N, K) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int minElements( int arr[], int N, int K)
{
int count = 0 ;
long requiredNum = 1 ;
int i = 0 ;
while (requiredNum <= K) {
if (i < N && requiredNum >= arr[i]) {
requiredNum += arr[i];
i++;
}
else {
count++;
requiredNum += requiredNum;
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 };
int K = 6 ;
int N = arr.length;
System.out.println(minElements(arr, N, K));
}
}
|
Python3
def minElements(arr, N, K):
count = 0
requiredNum = 1
i = 0
while (requiredNum < = K):
if (i < N and requiredNum > = arr[i]):
requiredNum + = arr[i]
i + = 1
else :
count + = 1
requiredNum + = requiredNum
return count
if __name__ = = '__main__' :
arr = [ 1 , 3 ]
K = 6
N = len (arr)
print (minElements(arr, N, K))
|
C#
using System;
class GFG {
public static int minElements( int [] arr, int N, int K)
{
int count = 0;
long requiredNum = 1;
int i = 0;
while (requiredNum <= K) {
if (i < N && requiredNum >= arr[i]) {
requiredNum += arr[i];
i++;
}
else {
count++;
requiredNum += requiredNum;
}
}
return count;
}
public static void Main( string [] args)
{
int [] arr = { 1, 3 };
int K = 6;
int N = arr.Length;
Console.Write(minElements(arr, N, K));
}
}
|
Javascript
<script>
function minElements(arr, N, K) {
let count = 0;
let requiredNum = 1;
let i = 0;
while (requiredNum <= K) {
if (i < N && requiredNum >= arr[i]) {
requiredNum += arr[i];
i++;
}
else {
count++;
requiredNum += requiredNum;
}
}
return count;
}
let arr = [1, 3];
let K = 6;
let N = arr.length;
document.write(minElements(arr, N, K) + "<br>" );
</script>
|
Time Complexity: O(N + log(K))
Auxiliary Space: O(1)
Last Updated :
11 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...