检查给定的数组是否可以通过删除小于 K 的元素并将其添加到 K 来减少到 0
给定一个数组,大小为N的arr[]和一个整数K 。如果arr[]中的值小于或等于K ,则该值将从数组中删除并添加到K 。任务是检查是否可以吸收arr[]中的所有元素。
例子:
Input: K = 10, arr[] = {3, 9, 19, 5, 21}
Output: true
Explanation: The array elements are absorbed in following way.
One way to absorption is {9, 19, 5, 3, 21}
value is 9 and the New k value: 10 + 9 = 19
value is 19 and the New k value: 19 + 19 = 38
value is 5 and the New k value: 38 + 5 = 43
value is 3 and the New k value: 43 + 3 = 46
value is 9 and the New k value: 46 + 21 = 6.
Hence, All values are absorbed.
Input: K = 5, arr[] = {4, 9, 23, 4}
Output: false
方法:这个问题可以通过使用贪心方法来解决。请按照以下步骤解决给定的问题。
- arr[]中任何最有可能小于或等于K的元素都是最小元素。
- 因此,以非递减顺序对数组进行排序,并尝试从左到右删除元素。
- 从左到右迭代,同时删除arr[]的值。
- 如果无法删除任何元素,请随时返回false 。
- 否则返回true 。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to check if all the elements
// can be absorbed or not
bool absorbption(int K, vector& arr)
{
// Sort the array in non-decreasing order
sort(arr.begin(), arr.end());
// Long long prevent from integer overflow
long long m = K;
for (int i = 0; i < arr.size(); i++) {
int value = arr[i];
if (m < value) {
return false;
}
else {
m += arr[i];
}
}
return true;
}
// Driver Code
int main()
{
vector arr{ 3, 9, 19, 5, 21 };
int K = 10;
// Check if all the elements
// can be removed or not.
if (absorbption(K, arr))
cout << "true";
else
cout << "false";
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to check if all the elements
// can be absorbed or not
static Boolean absorbption(int K, int arr[ ])
{
// Sort the array in non-decreasing order
Arrays.sort(arr);
// Long long prevent from integer overflow
long m = K;
for (int i = 0; i < arr.length; i++) {
int value = arr[i];
if (m < value) {
return false;
}
else {
m += arr[i];
}
}
return true;
}
public static void main (String[] args) {
int arr[ ] = { 3, 9, 19, 5, 21 };
int K = 10;
// Check if all the elements
// can be removed or not.
if (absorbption(K, arr))
System.out.print("true");
else
System.out.print("false");
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python 3 program for above approach
# Function to check if all the elements
# can be absorbed or not
def absorbption(K, arr):
# Sort the array in non-decreasing order
arr.sort()
# Long long prevent from integer overflow
m = K
for i in range(len(arr)):
value = arr[i]
if (m < value):
return False
else:
m += arr[i]
return True
# Driver Code
if __name__ == "__main__":
arr = [3, 9, 19, 5, 21]
K = 10
# Check if all the elements
# can be removed or not.
if (absorbption(K, arr)):
print("true")
else:
print("false")
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
class GFG
{
// Function to check if all the elements
// can be absorbed or not
static bool absorbption(int K, int []arr)
{
// Sort the array in non-decreasing order
Array.Sort(arr);
// Long long prevent from integer overflow
long m = K;
for (int i = 0; i < arr.Length; i++) {
int value = arr[i];
if (m < value) {
return false;
}
else {
m += arr[i];
}
}
return true;
}
// Driver Code
public static void Main()
{
int []arr = { 3, 9, 19, 5, 21 };
int K = 10;
// Check if all the elements
// can be removed or not.
if (absorbption(K, arr))
Console.Write("true");
else
Console.Write("false");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
true
时间复杂度: O(N * logN)
辅助空间: O(1)