Modify array to a permutation of consecutive numbers of longest length by at most K insertions
Last Updated :
14 Sep, 2021
Given an array arr[] of length N and an integer K, the task is to find the maximize the length of the array by appending at most K elements such that the array becomes a permutation of consecutive numbers starting from 1. Once K elements are added, sum of two or more array elements can be inserted.
Note: Elements can be formed as sum of the elements originally present in the array or the appended elements. But elements added as the sum of two or more elements of the array cannot be used to generate further elements.
Examples:
Input: N = 3, K = 1, arr[] = {1, 2, 4}
Output: 8
Explanation:
Original array elements = {1, 2, 4}
Array elements that can be obtained using these elements are {3, 5, 6, 7}.
Insert 8 into the array.
Now, all numbers starting from 9 to 15 can be appended into the array.
Hence, the array becomes a consecutive sequence of 15 numbers.
Input : N = 5, K=4, arr[N] = {1, 3, 10, 3, 1}
Output : 223
Approach: The idea is to sort the array arr[] in ascending order and then use the fact that if the sum of array arr[] elements is sum then all the elements from 1 to sum can be formed and if not, then it is required to insert sum+1 element into the array arr[] and subtract the value of K by 1. Follow the steps below to solve the problem:
- Sort the array arr[].
- Initialize the variable index as 0 to maintain the index of the element in the array arr[] and x as 0 to store the answer.
- Iterate in a while loop till index is less than N and perform the following steps:
- If arr[index] is greater than x, and if K is equal to 0, then break.
- Else increase the value of x by x and subtract the value of k by 1.
- Else, add the value of arr[index] to the value of x and increase the value of index by 1.
- Iterate in a while loop till K is not equal to 0 and perform the following steps:
- Increase the value of x by x and subtract the value of k by 1.
- After performing the above steps, print the value of x-1 as the answer.
Below is the implementation of the above approach.
C++14
#include <bits/stdc++.h>
using namespace std;
void findMaximumLength( int n, int k, int arr[])
{
sort(arr, arr + n);
int x = 1;
int index = 0;
while (index < n) {
if (arr[index] > x) {
if (k == 0)
break ;
x = x + x;
k--;
}
else {
x += arr[index++];
}
}
while (k != 0) {
x = x + x;
k--;
}
cout << x - 1 << endl;
}
int main()
{
int n = 5, k = 4;
int arr[n] = { 1, 3, 10, 3, 1 };
findMaximumLength(n, k, arr);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
static void findMaximumLength( int n, int k, int arr[])
{
Arrays.sort(arr);
int x = 1 ;
int index = 0 ;
while (index < n) {
if (arr[index] > x) {
if (k == 0 )
break ;
x = x + x;
k--;
}
else {
x += arr[index++];
}
}
while (k != 0 ) {
x = x + x;
k--;
}
System.out.println(x - 1 );
}
public static void main(String[] args)
{
int n = 5 , k = 4 ;
int arr[] = { 1 , 3 , 10 , 3 , 1 };
findMaximumLength(n, k, arr);
}
}
|
Python3
def findMaximumLength(n, k, arr):
arr.sort();
x = 1 ;
index = 0 ;
while (index < n):
if (arr[index] > x):
if (k = = 0 ):
break ;
x = x + x;
k - = 1 ;
else :
x + = arr[index];
index + = 1 ;
while (k ! = 0 ):
x = x + x;
k - = 1 ;
print (x - 1 );
if __name__ = = '__main__' :
n = 5 ; k = 4 ;
arr = [ 1 , 3 , 10 , 3 , 1 ];
findMaximumLength(n, k, arr);
|
C#
using System;
class GFG{
static void findMaximumLength( int n, int k, int []arr)
{
Array.Sort(arr);
int x = 1;
int index = 0;
while (index < n)
{
if (arr[index] > x)
{
if (k == 0)
break ;
x = x + x;
k--;
}
else
{
x += arr[index++];
}
}
while (k != 0)
{
x = x + x;
k--;
}
Console.Write(x - 1);
}
public static void Main(String[] args)
{
int n = 5, k = 4;
int []arr = { 1, 3, 10, 3, 1 };
findMaximumLength(n, k, arr);
}
}
|
Javascript
<script>
function findMaximumLength(n, k, arr) {
arr.sort((a, b) => a - b);
let x = 1;
let index = 0;
while (index < n) {
if (arr[index] > x) {
if (k == 0) break ;
x = x + x;
k--;
} else {
x += arr[index++];
}
}
while (k != 0) {
x = x + x;
k--;
}
document.write(x - 1);
}
let n = 5,
k = 4;
let arr = [1, 3, 10, 3, 1];
findMaximumLength(n, k, 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...