Covering maximum array elements with given value
Last Updated :
27 Apr, 2023
Given total number of candies 'X', number of students '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 maximize 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
?>
JavaScript
<script>
// JavaScript 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
a.sort();
// To store the number
// of happy students
let cc = 0;
// To store the running sum
let s = 0;
for (let 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;
}
var sum = a.reduce(function(a, b){
return a + b;
}, 0);
// If the sum = x
// then answer is n
if(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 code
let n = 3;
let x = 70;
let a = [ 10, 20, 30 ];
document.write(maxArrayCover(a, n, x));
</script>
Time Complexity: O(NlogN), where N is the size of the array
Space Complexity: O(N)
Similar Reads
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read
Find array elements with frequencies in range [l , r] Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
9 min read
Check if Array with mean X can be made using N elements of given Array Given an array arr[] and two integers N and X, the task is to find if it is possible to create an array using N distinct elements from arr[] such that the mean of the newly formed array is X. Examples: Input: N = 5, X = 8, arr[] = {1, 10, 3, 2, 6, 7, 4, 5}Output: YESExplanation: Many arrays using 5
6 min read
Minimum Value X to Delete All Arrays Given 2d array A[][] of size N*M. Your Task is to choose the minimum possible number X to perform the following operation. In one operation choose any array i from N arrays and delete the first element, if it is less than X, and increase X by 1. Keep performing this operation until the array becomes
12 min read
Find missing elements from an Array with duplicates Given an array arr[] of size N having integers in the range [1, N] with some of the elements missing. The task is to find the missing elements. Note: There can be duplicates in the array. Examples: Input: arr[] = {1, 3, 3, 3, 5}, N = 5Output: 2 4Explanation: The numbers missing from the list are 2 a
12 min read
Minimum capacity of small arrays needed to contain all element of the given array Given an array of positive integers and a value K, The task is to empty the array in less than or equal to K small arrays such that each small array can only contain at max P elements from a single slot / index of the given array. Find the minimum value of P.Examples: Input: arr[] = {1, 2, 3, 4, 5},
9 min read
Find four missing numbers in an array containing elements from 1 to N Given an array of unique integers where each integer of the given array lies in the range [1, N]. The size of array is (N-4). No Single element is repeated. Hence four numbers from 1 to N are missing in the array. Find the 4 missing numbers in sorted order. Examples: Input : arr[] = {2, 5, 6, 3, 9}O
10 min read
Search an element in given N ranges Given an array of N sorted ranges and a number K. The task is to find the index of the range in which K lies. If K does not lie in any of the given ranges then print -1. Note: None of the given ranges coincide. Examples: Input: arr[] = { { 1, 3 }, { 4, 7 }, { 8, 11 } }, K = 6 Output: 1 6 lies in the
10 min read
Find the Prefix-MEX Array for given Array Given an array A[] of N elements, the task is to create a Prefix-MEX array for this given array. Prefix-MEX array B[] of an array A[] is created such that MEX of A[0] till A[i] is B[i]. MEX of an array refers to the smallest missing non-negative integer of the array. Examples: Input: A[] = {1, 0, 2,
13 min read
Minimum operations required to make two elements equal in Array Given array A[] of size N and integer X, the task is to find the minimum number of operations to make any two elements equal in the array. In one operation choose any element A[i] and replace it with A[i] & X. where & is bitwise AND. If such operations do not exist print -1. Examples: Input:
9 min read