📜  将数组修改为最多 K 次插入的最长长度的连续数字的排列

📅  最后修改于: 2021-10-25 09:17:09             🧑  作者: Mango

给定一个长度为N的数组arr[]和一个整数K ,任务是通过添加最多K 个元素来找到数组长度的最大值,以使数组成为从1开始的连续数字的排列。一旦添加了K 个元素,就可以插入两个或多个数组元素的总和。

注意:元素可以形成为数组中原始元素或附加元素的总和。但是作为数组的两个或多个元素的总和添加的元素不能用于生成更多元素。

例子:

方法:思路是将数组arr[]按升序排序,然后利用这样一个事实,如果数组arr[]元素之和sum,则可以形成从1sum的所有元素,如果不是,则为需要将sum+1元素插入数组arr[] 中,并将K的值减去1。按照以下步骤解决问题:

  • 对数组arr[] 进行排序
  • 将变量index初始化为0以维护数组arr[] 中元素的索引,将x初始化为0以存储答案。
  • 在 while 循环中迭代 直到索引小于N并执行以下步骤:
    • 如果arr[index]大于x ,并且K等于0 ,则中断。
    • 否则,将x的值增加x并将k的值减去1。
    • 否则,ARR [索引]的值添加到x的值和增加1索引的值
  • 在 while 循环中迭代 直到K不等于0并执行以下步骤:
    • x的值增加x并将k的值减去1
  • 执行上述步骤后,打印x-1的值作为答案。

下面是上述方法的实现。

C++14
// C++ program for the above approach
#include 
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;
     
    # Prthe 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


输出:
223

时间复杂度: O(N*log(N))
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程