给定一个由N 个正整数和一个正整数K组成的数组arr[] ,任务是找到需要插入的最小元素数,使得范围[1, K]中的所有数字都可以作为数组的任何子序列的总和。
例子:
Input: arr[] = {1, 3, 5}, K = 10
Output: 1
Explanation:
Appending the element {1} to the array modifies the array to {1, 3, 5, 1}. Now the all the sum over the range [1, K] can be obtained as:
- Sum 1: The elements are {1}.
- Sum 2: The elements are {1, 1}.
- Sum 3: The elements are {3}.
- Sum 4: The elements are {1. 3}.
- Sum 5: The elements are {1, 3, 1}.
- Sum 6: The elements are {1, 5}.
- Sum 7: The elements are {1, 5, 1}.
- Sum 8: The elements are {3, 5}.
- Sum 9: The elements are {1, 3, 5}.
- Sum 10: The elements are {1, 3, 5, 1}.
Input: arr[] = {2, 6, 8, 12, 19}, K = 20
Output: 2
方法:可以通过按递增顺序对数组进行排序来解决给定的问题,然后尝试使范围[1, K] 上的总和值使用以下事实:如果数组元素X的总和,则该范围内的所有值[1, X]可以形成。否则,需要将值(sum + 1)作为数组元素插入。请按照以下步骤解决问题:
- 按升序对数组arr[]进行排序。
- 初始化变量,假设index为0以维护数组元素的索引,并计数为0以存储添加的结果总元素。
- 如果arr[0]的值大于1 ,则需要追加1 ,因此将count的值增加1 。否则,将索引的值增加1 。
- 初始化变量,说期待作为2保持在预期从1到K的范围内的下一个值将被从阵列ARR形成[]。
- 迭代一个循环,直到expect的值最多为K,然后执行以下步骤:
- 如果索引大于等于N或arr[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 现场工作专业课程和学生竞争性编程现场课程。