Minimum number of insertions required such that first K natural numbers can be obtained as sum of a subsequence of the array
Last Updated :
09 May, 2022
Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the minimum number of elements that are required to be inserted such that all numbers from the range [1, K] can be obtained as the sum of any subsequence of the array.
Examples:
Input: arr[] = {1, 3, 5}, K = 10
Output: 1
Explanation:
Appending the element {1} to the array modifies the array to {1, 3, 5, 1}. Now the all the sum over the range [1, K] can be obtained as:
- Sum 1: The elements are {1}.
- Sum 2: The elements are {1, 1}.
- Sum 3: The elements are {3}.
- Sum 4: The elements are {1. 3}.
- Sum 5: The elements are {1, 3, 1}.
- Sum 6: The elements are {1, 5}.
- Sum 7: The elements are {1, 5, 1}.
- Sum 8: The elements are {3, 5}.
- Sum 9: The elements are {1, 3, 5}.
- Sum 10: The elements are {1, 3, 5, 1}.
Input: arr[] = {2, 6, 8, 12, 19}, K = 20
Output: 2
Approach: The given problem can be solved by sorting the array in increasing order and then try to make the sum value over the range [1, K] using the fact that if the sum of array elements X, then all the values over the range [1, X] can be formed. Otherwise, it is required to insert the value (sum + 1) as an array element. Follow the steps below to solve the problem:
- Sort the array arr[] in increasing order.
- Initialize the variable, say index as 0 to maintain the index of the array element and count as 0 to store the resultant total elements added.
- If the value of arr[0] is greater than 1, then 1 needs to be appended, so increase the value of the count by 1. Otherwise, increase the value of the index by 1.
- Initialize the variable, say expect as 2 to maintain the next value expected in the range from 1 to K to be formed from the array arr[].
- Iterate a loop until the value of expect is at most K and perform the following steps:
- If the index is greater than equal to N or arr[index] is greater than the value of expect, then increase the value of count by 1 and multiply the value of expect by 2.
- Otherwise, increase the value of expect by arr[index] and increase the value of the index by 1.
- After completing the above steps, print the value of count as the result.
Below is the implementation of the above approach.
C++14
#include <bits/stdc++.h>
using namespace std;
void findMinimumNumberOfElements( int n, int k, int arr[])
{
sort(arr, arr + n);
int index = 0, count = 0;
if (arr[0] > 1)
++count;
else
++index;
long long expect = 2;
while (expect <= k) {
if (index >= n || arr[index] > expect) {
++count;
expect += expect;
}
else {
expect += arr[index];
++index;
}
}
cout << count;
}
int main()
{
int arr[] = { 2, 6, 8, 12, 19 };
int K = 20;
int N = sizeof (arr) / sizeof (arr[0]);
findMinimumNumberOfElements(N, K, arr);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
int cmpfunc( const void * a, const void * b)
{
return (*( int *)a - *( int *)b);
}
void findMinimumNumberOfElements( int n, int k, int arr[])
{
qsort (arr, n, sizeof ( int ), cmpfunc);
int index = 0, count = 0;
if (arr[0] > 1)
++count;
else
++index;
long long expect = 2;
while (expect <= k) {
if (index >= n || arr[index] > expect) {
++count;
expect += expect;
}
else {
expect += arr[index];
++index;
}
}
printf ( "%d" , count);
}
int main()
{
int arr[] = { 2, 6, 8, 12, 19 };
int K = 20;
int N = sizeof (arr) / sizeof (arr[0]);
findMinimumNumberOfElements(N, K, arr);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
static void findMinimumNumberOfElements( int n, int k, int [] arr)
{
Arrays.sort(arr);
int index = 0 , count = 0 ;
if (arr[ 0 ] > 1 )
++count;
else
++index;
long expect = 2 ;
while (expect <= k) {
if (index >= n || arr[index] > expect) {
++count;
expect += expect;
}
else {
expect += arr[index];
++index;
}
}
System.out.println(count);
}
public static void main(String[] args)
{
int arr[] = { 2 , 6 , 8 , 12 , 19 };
int K = 20 ;
int N = arr.length;
findMinimumNumberOfElements(N, K, arr);
}
}
|
Python3
def findMinimumNumberOfElements(n, k, arr):
arr.sort()
index = 0
count = 0
if (arr[ 0 ] > 1 ):
count + = 1
else :
index + = 1
expect = 2
while (expect < = k):
if (index > = n or arr[index] > expect):
count + = 1
expect + = expect
else :
expect + = arr[index]
index + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 2 , 6 , 8 , 12 , 19 ]
K = 20
N = len (arr)
findMinimumNumberOfElements(N, K, arr)
|
C#
using System;
class GFG {
static void findMinimumNumberOfElements( int n, int k,
int [] arr)
{
Array.Sort(arr);
int index = 0;
int count = 0;
if (arr[0] > 1) {
++count;
}
else {
++index;
}
long expect = 2;
while (expect <= k) {
if (index >= n || arr[index] > expect) {
++count;
expect += expect;
}
else {
expect += arr[index];
++index;
}
}
Console.WriteLine(count);
}
public static void Main()
{
int [] arr = { 2, 6, 8, 12, 19 };
int K = 20;
int N = arr.Length;
findMinimumNumberOfElements(N, K, arr);
}
}
|
Javascript
<script>
function findMinimumNumberOfElements(n, k, arr) {
arr.sort((a, b) => a - b);
let index = 0;
let count = 0;
if (arr[0] > 1) {
++count;
}
else {
++index;
}
let expect = 2;
while (expect <= k) {
if (index >= n || arr[index] > expect) {
++count;
expect += expect;
}
else {
expect += arr[index];
++index;
}
}
document.write(count);
}
let arr = [2, 6, 8, 12, 19];
let K = 20;
let N = arr.length;
findMinimumNumberOfElements(N, K, arr);
</script>
|
Time Complexity: O(max(K, N*log N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...