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: 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
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++
#include <bits/stdc++.h>
using namespace std;
bool absorption( int K, vector< int >& arr)
{
sort(arr.begin(), arr.end());
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 ;
}
int main()
{
vector< int > arr{ 3, 9, 19, 5, 21 };
int K = 10;
if (absorption(K, arr))
cout << "true" ;
else
cout << "false" ;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean absorption( int K, int arr[ ])
{
Arrays.sort(arr);
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 ;
if (absorption(K, arr))
System.out.print( "true" );
else
System.out.print( "false" );
}
}
|
Python3
def absorption(K, arr):
arr.sort()
m = K
for i in range ( len (arr)):
value = arr[i]
if (m < value):
return False
else :
m + = arr[i]
return True
if __name__ = = "__main__" :
arr = [ 3 , 9 , 19 , 5 , 21 ]
K = 10
if (absorption(K, arr)):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
class GFG
{
static bool absorption( int K, int []arr)
{
Array.Sort(arr);
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()
{
int []arr = { 3, 9, 19, 5, 21 };
int K = 10;
if (absorption(K, arr))
Console.Write( "true" );
else
Console.Write( "false" );
}
}
|
Javascript
<script>
function absorption(K, arr) {
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 ;
}
let arr = [3, 9, 19, 5, 21];
let K = 10;
if (absorption(K, arr))
document.write( "true" );
else
document.write( "false" );
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)