There are two processes P1 and P2, and N resources where N is an even number. There is an array of N size and arr[i] represents the type of ith resource.There may be more then one instance of a resource.You are to divide these resources equally between P1 and P2 such that maximum number of distinct number of resources are allocated to P2. Print maximum number of distinct resources allocated to P2.
Input : arr = [1, 1, 2, 2, 3, 3]
There are three different kinds of resources (1, 2 and 3), and two for each kind. Optimal distribution: Process P1 has resources [1, 2, 3] and the process P2 has gifts [1, 2, 3], too. Process p2 has 3 distinct resources.
arr = [1, 1, 2, 1, 3, 4]
There are three different kinds of resources (1, 2, 3, 4), 3 instances of 1 and single single instances of resource 2, 3, 4. Optimal distribution: Process P1 has resources [1, 1, 1] and the process P2 has gifts [2, 3, 4].
Process p2 has 3 distinct resources.
Approach 1 (Using sorting):
1. Sort the Array of resources.
2. Find out the elements which are unique by comparing the adjacent elements of the sorted array.suppose count holds the distinct number of resources in array.
3. Return the minimum of count and N/2.
Time complexity- O(N log N)
# Python 3 program to equally divide n
# elements into two sets such that second
# set has maximum distinct elements.
def distribution(arr, n):
arr.sort(reverse = False)
count = 1
for i in range(1, n, 1):
if (arr[i] > arr[i – 1]):
count += 1
return min(count, n / 2)
# Driver code
if __name__ == ‘__main__’:
arr = [1, 1, 2, 1, 3, 4]
n = len(arr)
# This code is contributed by
Approach 2(using hash set)
Another way to find out distinct element is set, insert all the element in the set. By the property of a set, it will contain only unique elements. At the end, we can count the number of elements in the set, given by, say count. The value to be returned will again be given by min(count, n/2).
- Maximum distinct elements after removing k elements
- Find all numbers that divide maximum array elements
- Count of subsequences having maximum distinct elements
- Maximum number of chocolates to be distributed equally among k students
- Count array elements that divide the sum of all other elements
- Make two sets disjoint by removing minimum elements
- Sum of maximum of all subarrays | Divide and Conquer
- Maximum Sum SubArray using Divide and Conquer | Set 2
- Maximum Subarray Sum using Divide and Conquer algorithm
- Sum of distinct elements when elements are in range 1 to n
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Count elements which divide all numbers in range L-R
- Divide an array into k segments to maximize maximum of segment minimums
- Subarrays with distinct elements
- Distinct elements in subarray using Mo's Algorithm
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.