Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum length
// possible
void findMaximumLength(int n, int k, int arr[])
{
    // Sort the array
    sort(arr, arr + n);
 
    // Initializing the variables
    int x = 1;
    int index = 0;
 
    // Iterate over the range
    while (index < n) {
        if (arr[index] > x) {
 
            // If k is 0, then no
            // element can be inserted
            if (k == 0)
                break;
 
            // Insert the element
            x = x + x;
            k--;
        }
        else {
            x += arr[index++];
        }
    }
 
    // Insert the remaining
    // possible elements
    while (k != 0) {
        x = x + x;
        k--;
    }
 
    // Print the answer
    cout << x - 1 << endl;
}
 
// Driver Code
int main()
{
    int n = 5, k = 4;
    int arr[n] = { 1, 3, 10, 3, 1 };
 
    findMaximumLength(n, k, arr);
 
    return 0;
}

Java




// Java code for the above approach
import java.io.*;
import java.util.Arrays;
class GFG
{
   
    // Function to find the maximum length
    // possible
    static void findMaximumLength(int n, int k, int arr[])
    {
       
        // Sort the array
        Arrays.sort(arr);
 
        // Initializing the variables
        int x = 1;
        int index = 0;
 
        // Iterate over the range
        while (index < n) {
            if (arr[index] > x) {
 
                // If k is 0, then no
                // element can be inserted
                if (k == 0)
                    break;
 
                // Insert the element
                x = x + x;
                k--;
            }
            else {
                x += arr[index++];
            }
        }
 
        // Insert the remaining
        // possible elements
        while (k != 0) {
            x = x + x;
            k--;
        }
 
        // Print the answer
        System.out.println(x - 1);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5, k = 4;
        int arr[] = { 1, 3, 10, 3, 1 };
 
        findMaximumLength(n, k, arr);
    }
}
 
// This code is contributed by Potta Lokesh

Python3




# Python code for the above approach
 
# Function to find the maximum length
# possible
def findMaximumLength(n, k, arr):
 
    # Sort the array
    arr.sort();
 
    # Initializing the variables
    x = 1;
    index = 0;
 
    # Iterate over the range
    while (index < n):
        if (arr[index] > x):
 
            # If k is 0, then no
            # element can be inserted
            if (k == 0):
                break;
 
            # Insert the element
            x = x + x;
            k-=1;
         
        else:
            x += arr[index];
            index += 1;
         
    # Insert the remaining
    # possible elements
    while (k != 0):
        x = x + x;
        k -= 1;
     
    # Print answer
    print(x - 1);
 
# Driver Code
if __name__ == '__main__':
    n = 5; k = 4;
    arr = [ 1, 3, 10, 3, 1 ];
 
    findMaximumLength(n, k, arr);
     
# This code is contributed by 29AjayKumar

C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum length
// possible
static void findMaximumLength(int n, int k, int []arr)
{
     
    // Sort the array
    Array.Sort(arr);
 
    // Initializing the variables
    int x = 1;
    int index = 0;
 
    // Iterate over the range
    while (index < n)
    {
        if (arr[index] > x)
        {
             
            // If k is 0, then no
            // element can be inserted
            if (k == 0)
                break;
 
            // Insert the element
            x = x + x;
            k--;
        }
        else
        {
            x += arr[index++];
        }
    }
 
    // Insert the remaining
    // possible elements
    while (k != 0)
    {
        x = x + x;
        k--;
    }
 
    // Print the answer
    Console.Write(x - 1);
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 5, k = 4;
    int []arr = { 1, 3, 10, 3, 1 };
 
    findMaximumLength(n, k, arr);
}
}
 
// This code is contributed by shivanisinghss2110

Javascript




<script>
// Javascript program for the above approach
 
// Function to find the maximum length
// possible
function findMaximumLength(n, k, arr) {
  // Sort the array
  arr.sort((a, b) => a - b);
 
  // Initializing the variables
  let x = 1;
  let index = 0;
 
  // Iterate over the range
  while (index < n) {
    if (arr[index] > x) {
      // If k is 0, then no
      // element can be inserted
      if (k == 0) break;
 
      // Insert the element
      x = x + x;
      k--;
    } else {
      x += arr[index++];
    }
  }
 
  // Insert the remaining
  // possible elements
  while (k != 0) {
    x = x + x;
    k--;
  }
 
  // Print the answer
  document.write(x - 1);
}
 
// Driver Code
let n = 5,
  k = 4;
let arr = [1, 3, 10, 3, 1];
 
findMaximumLength(n, k, arr);
 
// This code is contributed by _saurabh_jaiswal.
</script>
Output: 
223

 

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

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :