Maximum types of candies a person can eat if only N/2 of them can be eaten (Distribute Candies)
Given an array arr[] of N candies, with N being an even number and arr[i] representing the type of candy. The task is to find the maximum number of different types of candies a person can eat if only N/2 of them can be eaten.
Examples:
Input: arr[] = {4, 4, 5, 5, 6, 6, 7, 7}
Output: 4
Explanation: Person can only eat 8/2 = 4 candies and there are 4 types of candies in the array. Thus, the person can eat one candy of each type.
Input: arr[] = {2, 2, 3, 1}
Output: 2
Explanation: Person can only eat 4/2 = 2 and there are 3 types of candies in the array. Thus, the answer is 2, since maximum 2 candies allowed.
Naive Approach: The idea is to find the number of candy types in the given array. If the maximum number of candies that are allowed to eat is greater than the given number of candy types, then the answer is the number of candy types given. Else, the answer is the maximum number of candies that are allowed to eat.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use hashing. Follow the steps below to solve the problem:
- Initialize a hashset s to store the unique candy types.
- Traverse the array, arr[] and insert all the elements in the set, s.
- Store the size of the hashset s, in a variable M.
- If the value of (M < N/2), then print M, else print N/2 as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int num_candyTypes(vector< int >& candies)
{
unordered_set< int > s;
for ( int i = 0; i < candies.size(); i++) {
s.insert(candies[i]);
}
return s.size();
}
void distribute_candies(vector< int >& candies)
{
int allowed = candies.size() / 2;
int types = num_candyTypes(candies);
if (types < allowed)
cout << types;
else
cout << allowed;
}
int main()
{
vector< int > candies = { 4, 4, 5, 5, 3, 3 };
distribute_candies(candies);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int num_candyTypes( int []candies)
{
Dictionary<Integer,
Integer> s = new Hashtable<Integer,
Integer>();
for ( int i = 0 ; i < candies.length; i++)
{
s.put(candies[i], 1 );
}
return s.size();
}
public static void distribute_candies( int []candies)
{
int allowed = candies.length / 2 ;
int types = num_candyTypes(candies);
if (types < allowed)
System.out.println(types);
else
System.out.println(allowed);
}
public static void main(String[] args)
{
int candies[] = { 4 , 4 , 5 , 5 , 3 , 3 };
distribute_candies(candies);
}
}
|
Python3
def num_candyTypes(candies):
s = set ()
for i in range ( len (candies)):
s.add(candies[i])
return len (s)
def distribute_candies(candies):
allowed = len (candies) / 2
types = num_candyTypes(candies)
if (types < allowed):
print ( int (types))
else :
print ( int (allowed))
if __name__ = = '__main__' :
candies = [ 4 , 4 , 5 , 5 , 3 , 3 ]
distribute_candies(candies)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
public static int num_candyTypes( int []candies)
{
Dictionary< int , int > s = new Dictionary< int , int >();
for ( int i = 0; i < candies.Length; i++)
{
if (!s.ContainsKey(candies[i]))
s.Add(candies[i], 1);
}
return s.Count;
}
public static void distribute_candies( int []candies)
{
int allowed = candies.Length / 2;
int types = num_candyTypes(candies);
if (types < allowed)
Console.WriteLine(types);
else
Console.WriteLine(allowed);
}
static public void Main (){
int [] candies = { 4, 4, 5, 5, 3, 3 };
distribute_candies(candies);
}
}
|
Javascript
<script>
function num_candyTypes(candies) {
let s = new Set();
for (let i = 0; i < candies.length; i++) {
s.add(candies[i]);
}
return s.size;
}
function distribute_candies(candies)
{
let allowed = candies.length / 2;
let types = num_candyTypes(candies);
if (types < allowed)
document.write(types);
else
document.write(allowed);
}
let candies = [4, 4, 5, 5, 3, 3];
distribute_candies(candies);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
10 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...