# Modify array to a permutation of consecutive numbers of longest length by at most K insertions

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: 8Explanation: 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 : 223Approach : 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 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 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); Output: 223Time Complexity : O(N*log(N))Auxiliary Space : O(1)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.