Covering maximum array elements with given value
Given total number of candies ‘X’, number of student ‘N’ and an array ‘arr’ which holds the value for the exact number of candies that must be given to a student to make the student happy where arr[i] is the exact amount of candies that make the student ‘i’ happy. The task is to distribute the candies in such a way that makes maximum students happy.
Examples:
Input : X = 70, arr = {20, 30, 10}
Output: 2
One optimal way of distribution is (20, 40, 10)
So, the number of happy students are 2.
Input: X = 10, arr = {20, 30, 10}
Output: 1
One optimal way of distribution is (0, 0, 10)
Only 1 student can be made happy in this case
Approach: We can maximise the number of happy students by starting to give candies to the students who are happy with lesser candies.
So we sort the list of students in ascending based on candies. Then, we will simply go through the array and take the students until the sum is less than or equal to the total candies.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach # include<bits/stdc++.h> using namespace std; // Function to find value for // covering maximum array elements int maxArrayCover(vector<int> a, int n, int x){ // sort the students in // ascending based on // the candies sort(a.begin(), a.end()); // To store the number // of happy students int cc = 0; // To store the running sum int s = 0; for (int i = 0; i < n; i++){ s += a[i]; // If the current student // can't be made happy if(s > x){ break; } // increment the count // if we can make the // ith student happy cc += 1; } // If the sum = x // then answer is n if(accumulate(a.begin(), a.end(), 0) == x){ return n; } else{ // If the count is // equal to n then // the answer is n-1 if(cc == n){ return n-1; } else{ return cc; } } } // Driver function int main(){ int n = 3; int x = 70; vector<int> a = {10, 20, 30}; printf("%d\n",maxArrayCover(a, n, x)); return 0; } // This code is contributed // by Harshit Saini |
Java
// Java implementation of the approach import java.util.*; class GFG{ // Function to find value for // covering maximum array elements public static int maxArrayCover(int[] a, int n, int x){ // sort the students in // ascending based on // the candies Arrays.sort(a); // To store the number // of happy students int cc = 0; // To store the running sum int s = 0; for (int i = 0; i < n; i++){ s += a[i]; // If the current student // can't be made happy if(s > x){ break; } // increment the count // if we can make the // ith student happy cc += 1; } // If the sum = x // then answer is n if(Arrays.stream(a).sum() == x){ return n; } else{ // If the count is // equal to n then // the answer is n-1 if(cc == n){ return n-1; } else{ return cc; } } } // Driver function public static void main(String []args){ int n = 3; int x = 70; int[] a = new int[]{10, 20, 30}; System.out.println(maxArrayCover(a, n, x)); System.exit(0); } } // This code is contributed // by Harshit Saini |
Python3
# Python implementation of the approach # Function to find value for # covering maximum array elements def maxArrayCover(a, n, x): # sort the students in # ascending based on # the candies a.sort() # To store the number # of happy students cc = 0 # To store the running sum s = 0 for i in range(n): s+= a[i] # If the current student # can't be made happy if(s > x): break # increment the count # if we can make the # ith student happy cc += 1 # If the sum = x # then answer is n if(sum(a) == x): return n else: # If the count is # equal to n then # the answer is n-1 if(cc == n): return n-1 else: return cc # Driver function if __name__ == '__main__': n, x = 3, 70 a = [10, 20, 30] print(maxArrayCover(a, n, x)) |
C#
// C# implementation of the approach using System; using System.Linq; class GFG{ // Function to find value for // covering maximum array elements static int maxArrayCover(int[] a, int n, int x){ // sort the students in // ascending based on // the candies Array.Sort(a); // To store the number // of happy students int cc = 0; // To store the running sum int s = 0; for (int i = 0; i < n; i++){ s += a[i]; // If the current student // can't be made happy if(s > x){ break; } // increment the count // if we can make the // ith student happy cc += 1; } // If the sum = x // then answer is n if(a.Sum() == x){ return n; } else{ // If the count is // equal to n then // the answer is n-1 if(cc == n){ return n-1; } else{ return cc; } } } // Driver function public static void Main(){ int n = 3; int x = 70; int[] a = new int[]{10, 20, 30}; Console.WriteLine(maxArrayCover(a, n, x)); } } // This code is contributed // by Harshit Saini |
PHP
<?php // PHP implementation of the approach // Function to find value for // covering maximum array elements function maxArrayCover($a, $n, $x){ // sort the students in // ascending based on // the candies sort($a); // To store the number // of happy students $cc = 0; // To store the running sum $s = 0; for ($i = 0; $i < $n; $i++){ $s += $a[$i]; // If the current student // can't be made happy if($s > $x){ break; } // increment the count // if we can make the // ith student happy $cc += 1; } // If the sum = x // then answer is n if(array_sum($a) == $x){ return $n; } else{ // If the count is // equal to n then // the answer is n-1 if($cc == $n){ return $n-1; } else{ return $cc; } } } $n = 3; $x = 70; $a = array(10, 20, 30); echo maxArrayCover($a, $n, $x); // This code is contributed // by Harshit Saini ?> |
2
Time Complexity : O(N * log(N))
Space Complexity : O(N)
Recommended Posts:
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Maximum in array which is at-least twice of other elements
- Maximum value K such that array has at-least K elements that are >= K
- Find maximum xor of k elements in an array
- Maximum value after merging all elements in the array
- Maximum length of the sub-array whose first and last elements are same
- Maximum set bit sum in array without considering adjacent elements
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array
- Maximum sum of all elements of array after performing given operations
- Find the maximum elements in the first and the second halves of the Array
- Maximum sum in circular array such that no two elements are adjacent
- Find a number that divides maximum array elements
- Find k maximum elements of array in original order
- Maximum difference between group of k-elements and rest of the array.
- Count the maximum number of elements that can be selected from the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Harshit Saini



