📌  相关文章
📜  所需的最少插入次数,以便可以获得作为数组子序列之和的前 K 个自然数

📅  最后修改于: 2021-10-26 05:24:39             🧑  作者: Mango

给定一个由N 个正整数和一个正整数K组成的数组arr[] ,任务是找到需要插入的最小元素数,使得范围[1, K]中的所有数字都可以作为数组的任何子序列的总和。

例子:

方法:可以通过按递增顺序对数组进行排序来解决给定的问题,然后尝试使范围[1, K] 上的总和值使用以下事实:如果数组元素X的总和,则该范围内的所有值[1, X]可以形成。否则,需要将值(sum + 1)作为数组元素插入。请按照以下步骤解决问题:

  • 按升序对数组arr[]进行排序。
  • 初始化变量,假设index0以维护数组元素的索引,并计数0以存储添加的结果总元素。
  • 如果arr[0]的值大于1 ,则需要追加1 ,因此将count的值增加1 。否则,将索引的值增加1
  • 初始化变量,说期待作为2保持在预期从1K的范围内的下一个值将被从阵列ARR形成[]。
  • 迭代一个循环,直到expect的值最多为K,然后执行以下步骤:
    • 如果索引大于等于Narr[index]大于expect的值,则将count的值增加1并将expect的值乘以2。
    • 否则,增加通过ARR [索引]期望的值和由1增加索引的值。
  • 完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现。

C++14
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of elements that must be added to
// make the sum of array element over
// the range [1, K]
void findMinimumNumberOfElements(
    int n, int k, int arr[])
{
    // Sort the given array
    sort(arr, arr + n);
 
    // Stores the index for the
    // array
    int index = 0;
    int count = 0;
 
    if (arr[0] > 1) {
 
        // If 1 is not present, then
        // append it
        ++count;
    }
 
    // Move on to next index
    else {
        ++index;
    }
 
    // The expected value in the array
    long long expect = 2;
    while (expect <= k) {
 
        // Need to append this number
        if (index >= n || arr[index] > expect) {
            ++count;
            expect += expect;
        }
 
        // Otherwise, expand the range
        // by current number
        else {
            expect += arr[index];
            ++index;
        }
    }
 
    // Print the answer
    cout << count;
}
 
// Driver Code
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
// Java program for the above approach
import java.io.*;
import java.util.Arrays;
 
class GFG {
 
    // Function to find the minimum number
    // of elements that must be added to
    // make the sum of array element over
    // the range [1, K]
    static void findMinimumNumberOfElements(int n, int k,
                                            int[] arr)
    {
        // Sort the given array
        Arrays.sort(arr);
 
        // Stores the index for the
        // array
        int index = 0;
        int count = 0;
 
        if (arr[0] > 1) {
 
            // If 1 is not present, then
            // append it
            ++count;
        }
 
        // Move on to next index
        else {
            ++index;
        }
 
        // The expected value in the array
        long expect = 2;
        while (expect <= k) {
 
            // Need to append this number
            if (index >= n || arr[index] > expect) {
                ++count;
                expect += expect;
            }
 
            // Otherwise, expand the range
            // by current number
            else {
                expect += arr[index];
                ++index;
            }
        }
 
        // Print the answer
        System.out.println(count);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 2, 6, 8, 12, 19 };
        int K = 20;
        int N = arr.length;
        findMinimumNumberOfElements(N, K, arr);
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 program for the above approach
 
# Function to find the minimum number
# of elements that must be added to
# make the sum of array element over
# the range [1, K]
def findMinimumNumberOfElements(n, k, arr):
    # Sort the given array
    arr.sort()
 
    # Stores the index for the
    # array
    index = 0
    count = 0
 
    if (arr[0] > 1):
        # If 1 is not present, then
        # append it
        count += 1
 
    # Move on to next index
    else:
        index += 1
 
    # The expected value in the array
    expect = 2
    while (expect <= k):
 
        # Need to append this number
        if (index >= n or arr[index] > expect):
            count += 1
            expect += expect
 
        # Otherwise, expand the range
        # by current number
        else:
            expect += arr[index]
            index += 1
 
    # Print the answer
    print(count)
 
# Driver Code
if __name__ == '__main__':
    arr = [2, 6, 8, 12, 19]
    K = 20
    N = len(arr)
    findMinimumNumberOfElements(N, K, arr)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
class GFG {
     
 // Function to find the minimum number
    // of elements that must be added to
    // make the sum of array element over
    // the range [1, K]
    static void findMinimumNumberOfElements(int n, int k,
                                            int[] arr)
    {
        // Sort the given array
        Array.Sort(arr);
 
        // Stores the index for the
        // array
        int index = 0;
        int count = 0;
 
        if (arr[0] > 1) {
 
            // If 1 is not present, then
            // append it
            ++count;
        }
 
        // Move on to next index
        else {
            ++index;
        }
 
        // The expected value in the array
        long expect = 2;
        while (expect <= k) {
 
            // Need to append this number
            if (index >= n || arr[index] > expect) {
                ++count;
                expect += expect;
            }
 
            // Otherwise, expand the range
            // by current number
            else {
                expect += arr[index];
                ++index;
            }
        }
 
        // Print the answer
        Console.WriteLine(count);
    }
     
    // Driver code
    public static void Main()
    {
        int[] arr = { 2, 6, 8, 12, 19 };
        int K = 20;
        int N = arr.Length;
        findMinimumNumberOfElements(N, K, arr);
    }
}
 
// This code is contributed by avijitmondal1998.


Javascript


输出:
2

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

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