给定一个由N 个整数组成的排序数组arr[]和一个整数K ,任务是找到要插入数组的最小元素数,使得[1, K]范围内的任何值都可以通过添加修改后的数组的任何子集的元素。
例子:
Input: arr[] = {1, 3}, K = 6
Output: 1
Explanation:
Adding the number 2 to the array modifies the array arr[] to {1, 2, 3}. Now, every sum value over the range [1, 6] can be formed:
- Sum = 1: The subset is {1}.
- Sum = 2: The subset is {2}.
- Sum = 3: The subset is {3}.
- Sum = 4: The subset is {1, 3}.
- Sum = 5: The subset is {2, 3}.
- Sum = 6: The subset is {1, 2, 3}.
Therefore, the minimum number of elements to be added is 1.
Input: arr[] = {1, 5, 10}, K = 20
Output: 2
方法:给定的问题可以通过使用基于以下观察的贪心方法来解决:
- 将X视为最大数,这样范围[1, X]中的所有数都可以通过对数组的任何子集求和来形成。
- 然后可以观察到,通过向数组添加整数Y ,范围修改为[1, X+Y],因为现在可以形成范围[X, X+Y]中的每个数字。
- 因此,想法是贪婪地添加最能增加范围的元素,并且不会跳过获得的新范围中的任何数字。可以通过每次将X添加到数组并将X修改为2*X 来完成。
请按照以下步骤解决问题:
- 初始化两个整数变量,例如i和count为0 ,以分别存储数组元素的索引和所需元素的计数。
- 初始化一个变量,比如requiredNum为1 ,以存储可以形成每个数字的数字。
- 迭代直到requiredNum ≤ K,并执行以下操作:
- 如果i < N且requiredNum >= arr[i]则将requiredNum增加arr[i]并将i增加1 。
- 否则,将requiredNum增加requiredNum ,并将count增加1 。
- 最后,完成上述步骤后,打印count 中得到的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the count of minimum
// elements to be inserted to form every
// number in a range
int minElements(int arr[], int N, int K)
{
// Stores the count of numbers needed
int count = 0;
// Stores the numbers upto which every
// numbers can be formed
long long requiredNum = 1;
// Stores the index of the array arr[]
int i = 0;
// Iterate until requiredSum is less than
// or equal to K
while (requiredNum <= K) {
// If i is less than N and requiredSum
// is greater than or equal to arr[i]
if (i < N && requiredNum >= arr[i]) {
// Increment requiredSum
// by arr[i]
requiredNum += arr[i];
// Increment i by 1
i++;
}
// Otherwise
else {
// Increment count by 1
count++;
// Increment requiredSum
// by requiredSum
requiredNum += requiredNum;
}
}
// Return result
return count;
}
// Driver Code
int main()
{
// Input
int arr[] = { 1, 3 };
int K = 6;
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << minElements(arr, N, K) << endl;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the count of minimum
// elements to be inserted to form every
// number in a range
public static int minElements(int arr[], int N, int K)
{
// Stores the count of numbers needed
int count = 0;
// Stores the numbers upto which every
// numbers can be formed
long requiredNum = 1;
// Stores the index of the array arr[]
int i = 0;
// Iterate until requiredSum is less than
// or equal to K
while (requiredNum <= K) {
// If i is less than N and requiredSum
// is greater than or equal to arr[i]
if (i < N && requiredNum >= arr[i]) {
// Increment requiredSum
// by arr[i]
requiredNum += arr[i];
// Increment i by 1
i++;
}
// Otherwise
else {
// Increment count by 1
count++;
// Increment requiredSum
// by requiredSum
requiredNum += requiredNum;
}
}
// Return result
return count;
}
// Driver Code
public static void main(String[] args)
{
// Input
int arr[] = { 1, 3 };
int K = 6;
int N = arr.length;
// Function Call
System.out.println(minElements(arr, N, K));
// This code is contributed by Potta Lokesh
}
}
Python3
# Python 3 program for the above approach
# Function to find the count of minimum
# elements to be inserted to form every
# number in a range
def minElements(arr, N, K):
# Stores the count of numbers needed
count = 0
# Stores the numbers upto which every
# numbers can be formed
requiredNum = 1
# Stores the index of the array arr[]
i = 0
# Iterate until requiredSum is less than
# or equal to K
while (requiredNum <= K):
# If i is less than N and requiredSum
# is greater than or equal to arr[i]
if (i < N and requiredNum >= arr[i]):
# Increment requiredSum
# by arr[i]
requiredNum += arr[i]
# Increment i by 1
i += 1
# Otherwise
else:
# Increment count by 1
count += 1
# Increment requiredSum
# by requiredSum
requiredNum += requiredNum
# Return result
return count
# Driver Code
if __name__ == '__main__':
# Input
arr = [1, 3]
K = 6
N = len(arr)
# Function Call
print(minElements(arr, N, K))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the count of minimum
// elements to be inserted to form every
// number in a range
public static int minElements(int[] arr, int N, int K)
{
// Stores the count of numbers needed
int count = 0;
// Stores the numbers upto which every
// numbers can be formed
long requiredNum = 1;
// Stores the index of the array arr[]
int i = 0;
// Iterate until requiredSum is less than
// or equal to K
while (requiredNum <= K) {
// If i is less than N and requiredSum
// is greater than or equal to arr[i]
if (i < N && requiredNum >= arr[i]) {
// Increment requiredSum
// by arr[i]
requiredNum += arr[i];
// Increment i by 1
i++;
}
// Otherwise
else {
// Increment count by 1
count++;
// Increment requiredSum
// by requiredSum
requiredNum += requiredNum;
}
}
// Return result
return count;
}
// Driver Code
public static void Main(string[] args)
{
// Input
int[] arr = { 1, 3 };
int K = 6;
int N = arr.Length;
// Function Call
Console.Write(minElements(arr, N, K));
}
}
// This code is contributed by ukasp.
Javascript
输出
1
时间复杂度: O(N + log(K))
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。