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++
# include<bits/stdc++.h>
using namespace std;
int maxArrayCover(vector< int > a, int n, int x){
sort(a.begin(), a.end());
int cc = 0;
int s = 0;
for ( int i = 0; i < n; i++){
s += a[i];
if (s > x){
break ;
}
cc += 1;
}
if (accumulate(a.begin(), a.end(), 0) == x){
return n;
}
else {
if (cc == n){
return n-1;
}
else {
return cc;
}
}
}
int main(){
int n = 3;
int x = 70;
vector< int > a = {10, 20, 30};
printf ( "%d\n" ,maxArrayCover(a, n, x));
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int maxArrayCover( int [] a, int n, int x){
Arrays.sort(a);
int cc = 0 ;
int s = 0 ;
for ( int i = 0 ; i < n; i++){
s += a[i];
if (s > x){
break ;
}
cc += 1 ;
}
if (Arrays.stream(a).sum() == x){
return n;
}
else {
if (cc == n){
return n- 1 ;
}
else {
return cc;
}
}
}
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 );
}
}
|
Python3
def maxArrayCover(a, n, x):
a.sort()
cc = 0
s = 0
for i in range (n):
s + = a[i]
if (s > x):
break
cc + = 1
if ( sum (a) = = x):
return n
else :
if (cc = = n):
return n - 1
else :
return cc
if __name__ = = '__main__' :
n, x = 3 , 70
a = [ 10 , 20 , 30 ]
print (maxArrayCover(a, n, x))
|
C#
using System;
using System.Linq;
class GFG{
static int maxArrayCover( int [] a, int n, int x){
Array.Sort(a);
int cc = 0;
int s = 0;
for ( int i = 0; i < n; i++){
s += a[i];
if (s > x){
break ;
}
cc += 1;
}
if (a.Sum() == x){
return n;
}
else {
if (cc == n){
return n-1;
}
else {
return cc;
}
}
}
public static void Main(){
int n = 3;
int x = 70;
int [] a = new int []{10, 20, 30};
Console.WriteLine(maxArrayCover(a, n, x));
}
}
|
PHP
<?php
function maxArrayCover( $a , $n , $x ){
sort( $a );
$cc = 0;
$s = 0;
for ( $i = 0; $i < $n ; $i ++){
$s += $a [ $i ];
if ( $s > $x ){
break ;
}
$cc += 1;
}
if ( array_sum ( $a ) == $x ){
return $n ;
}
else {
if ( $cc == $n ){
return $n -1;
}
else {
return $cc ;
}
}
}
$n = 3;
$x = 70;
$a = array (10, 20, 30);
echo maxArrayCover( $a , $n , $x );
?>
|
Javascript
<script>
function maxArrayCover(a, n, x){
a.sort();
let cc = 0;
let s = 0;
for (let i = 0; i < n; i++){
s += a[i];
if (s > x){
break ;
}
cc += 1;
}
var sum = a.reduce( function (a, b){
return a + b;
}, 0);
if (sum == x){
return n;
}
else {
if (cc == n){
return n-1;
}
else {
return cc;
}
}
}
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...