Check if given Array can be reduced to 0 by removing element less than K and adding it to K Last Updated : 06 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Given an array, arr[] of size N and an integer K. If a value in arr[] is less than or equal to K, then that value will be removed from the array and added to K. The task is to check if all the elements in arr[] can be absorbed or not. Examples: Input: K = 10, arr[] = {3, 9, 19, 5, 21}Output: trueExplanation: 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 = 19value is 19 and the New k value: 19 + 19 = 38value is 5 and the New k value: 38 + 5 = 43value is 3 and the New k value: 43 + 3 = 46value 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 Approach: This problem can be solved by using the Greedy Approach. Follow the steps below to solve the given problem. Any element in arr[] that has the highest probability to be less than or equal to K would be the smallest element.So, sort the array in non-decreasing order and try to remove elements from left to right.Iterate from left to right while removing values of arr[].At any time if it is not possible to remove any element, return false.Else return true. Below is the implementation of the above approach. C++ // C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to check if all the elements // can be absorbed or not bool absorption(int K, vector<int>& 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<int> arr{ 3, 9, 19, 5, 21 }; int K = 10; // Check if all the elements // can be removed or not. if (absorption(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 absorption(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 (absorption(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 absorption(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 (absorption(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 absorption(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 (absorption(K, arr)) Console.Write("true"); else Console.Write("false"); } } // This code is contributed by Samim Hossain Mondal. JavaScript <script> // JavaScript code for the above approach // Function to check if all the elements // can be absorbed or not function absorption(K, arr) { // Sort the array in non-decreasing order arr.sort(function (a, b) { return a - b }) let m = K; for (let i = 0; i < arr.length; i++) { let value = arr[i]; if (m < value) { return false; } else { m += arr[i]; } } return true; } // Driver Code let arr = [3, 9, 19, 5, 21]; let K = 10; // Check if all the elements // can be removed or not. if (absorption(K, arr)) document.write("true"); else document.write("false"); // This code is contributed by Potta Lokesh </script> Outputtrue Time Complexity: O(N * logN)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if given Array can be reduced to 0 by removing element less than K and adding it to K R rishabhbatra53 Follow Improve Article Tags : Greedy Sorting Algo Geek DSA Arrays Algo-Geek 2021 +2 More Practice Tags : ArraysGreedySorting Similar Reads Check if sum of the given array can be reduced to 0 by reducing array elements by K Given an array arr[] consisting of N integers and an integer K, the task is to check if the sum of the array can be reduced to 0 by subtracting array elements by K any number of times. Examples: Input: arr[ ]= {-3, 2, -1, 5, 1}, K=2Output: "Yes"Explanation: Sum of the array is 4. Therefore, decreasi 4 min read Check if an array can be reduced to at most length K by removal of distinct elements Given an array arr[] consisting of N positive integers and an integer K, the task is to check if it is possible to reduce the size of the array to at most K or not by removing a subset of the distinct array elements. If it is possible, then print "Yes". Otherwise, print "No". Examples: Input: arr[] 5 min read Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum Given an array arr[] consisting of N integers, the task is to check if it is possible to reduce the array elements to 0 by repeatedly subtracting the minimum of any pair of consecutive array elements from both the elements in the pair. If it is possible, then print "Yes". Otherwise, print "No". Exam 10 min read Reduce the Array to 0 by decreasing elements by 1 or replacing at most K elements by 0 Given an array arr[] of N integers and a positive integer K, the task is to find the minimum number of operations required to reduce all array elements to 0 such that in each operation reduce any array element by 1 and independently at most K array element can be reduced to 0. Examples: Input: arr[] 6 min read Check if all array elements can be converted to K using given operations Given an integer array arr of size N and an integer K, the task is to make all elements of the array equal to K using the following operations: Choose an arbitrary subarray [l....r] of the input arrayReplace all values of this subarray equal to the [((r - l) + 2) / 2]th value in sorted subarray [l.. 9 min read Remove array elements to reduce frequency of each array element to at most K Given a sorted array arr[] of size N and an integer K, the task is to print the array by removing the minimum number of array elements to make frequency of each element at most K. Examples: Input: arr[] = { 1, 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5 }, K = 3 Output: { 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5 } Ex 14 min read Check if array can be divided into two sub-arrays such that their absolute difference is K Given an array arr[] and an integer K, the task is to find whether the array can be divided into two sub-arrays such that the absolute difference of the sum of the elements of both the sub-arrays is K. Examples: Input: arr[] = {2, 4, 5, 1}, K = 0 Output: Yes {2, 4} and {5, 1} are the two possible su 6 min read Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K Given an array arr[] consisting of N integers and a positive integer K, the task is to check if the given array can be reduced to a single element by repeatedly removing the larger of the two elements present in a pair whose absolute difference is at most K. If the array can be reduced to a single e 11 min read Minimum times K should be added to any element to sort given Array Given an unsorted array A[] of N integers and integer K, the task is to count the minimum number of times K should be added to the array elements to make the array sorted. Examples: Input: A[] = {2, 4, 3, 5, 9}, K = 3Output: 2Explanation: In the above array element at index 3 is 3 which is less than 6 min read Like