# Equally divide into two sets such that one set has maximum distinct elements

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.

Examples:

Input : arr[] = [1, 1, 2, 2, 3, 3]
Output: 3
Explanation:
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]
Output: 3
Explanation:
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)

## C++

 `// C++ program to equally divide n elements ` `// into two sets such that second set has ` `// maximum distinct elements. ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `int` `distribution(``int` `arr[], ``int` `n) ` `{ ` `    ``sort(arr, arr + n); ` `    ``int` `count = 1; ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `        ``if` `(arr[i] > arr[i - 1]) ` `            ``count++; ` `     `  `    ``return` `min(count, n / 2); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 1, 2, 1, 3, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << distribution(arr, n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to equally divide n elements ` `// into two sets such that second set has ` `// maximum distinct elements. ` `import` `java.util.*; ` `class` `Geeks { ` `     `  `static` `int` `distribution(``int` `arr[], ``int` `n) ` `{ ` `    ``Arrays.sort(arr); ` `    ``int` `count = ``1``; ` `    ``for` `(``int` `i = ``1``; i < n; i++)  ` `        ``if` `(arr[i] > arr[i - ``1``]) ` `            ``count++; ` `     `  `    ``return` `Math.min(count, n / ``2``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``1``, ``1``, ``2``, ``1``, ``3``, ``4` `}; ` `    ``int` `n = arr.length; ` `    ``System.out.println(distribution(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by ankita_saini `

## Python3

 `# 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) ` `    ``print``(``int``(distribution(arr, n))) ` ` `  `# This code is contributed by ` `# Shashank_Sharma `

## C#

 `// C# program to equally divide  ` `// n elements into two sets such  ` `// that second set has maximum ` `// distinct elements. ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `static` `int` `distribution(``int` `[]arr, ``int` `n) ` `{ ` `    ``Array.Sort(arr); ` `    ``int` `count = 1; ` `    ``for` `(``int` `i = 1; i < n; i++)  ` `        ``if` `(arr[i] > arr[i - 1]) ` `            ``count++; ` `     `  `    ``return` `Math.Min(count, n / 2); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `[]arr= { 1, 1, 2, 1, 3, 4 }; ` `    ``int` `n = arr.Length; ` `    ``Console.WriteLine(distribution(arr, n)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by ankita_saini `

## PHP

 ` ``\$arr``[``\$i` `- 1]) ` `            ``\$count``++; ` `     `  `    ``return` `min(``\$count``, ``\$n` `/ 2); ` `} ` ` `  `// Driver code ` `\$arr` `= ``array``(1, 1, 2, 1, 3, 4 ); ` `\$n` `= ``count``(``\$arr``); ` `echo``(distribution(``\$arr``, ``\$n``)); ` ` `  `// This code is contributed  ` `// by inder_verma ` `?> `

Output:

`3`

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).

## C++

 `// C++ program to equally divide n elements ` `// into two sets such that second set has ` `// maximum distinct elements. ` `#include ` `using` `namespace` `std; ` ` `  `int` `distribution(``int` `arr[], ``int` `n) ` `{ ` `    ``unordered_set<``int``, greater<``int``> > resources; ` ` `  `    ``// Insert all the resources in the set ` `    ``// There will be unique resources in the set ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `        ``resources.insert(arr[i]);     ` ` `  `    ``// return minimum of distinct resources ` `    ``// and n/2 ` `    ``return` `min(resources.size(), n / 2); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 1, 2, 1, 3, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << distribution(arr, n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to equally divide n elements ` `// into two sets such that second set has ` `// maximum distinct elements. ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `static` `int` `distribution(``int` `arr[], ``int` `n) ` `{ ` `    ``Set resources = ``new` `HashSet(); ` ` `  `    ``// Insert all the resources in the set ` `    ``// There will be unique resources in the set ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``resources.add(arr[i]);  ` ` `  `    ``// return minimum of distinct resources ` `    ``// and n/2 ` `    ``return` `Math.min(resources.size(), n / ``2``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``1``, ``1``, ``2``, ``1``, ``3``, ``4` `}; ` `    ``int` `n = arr.length; ` `    ``System.out.print(distribution(arr, n) +``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 program to equally divide n elements ` `# into two sets such that second set has ` `# maximum distinct elements. ` `def` `distribution(arr, n): ` `    ``resources ``=` `set``() ` `     `  `    ``# Insert all the resources in the set ` `    ``# There will be unique resources in the set ` `    ``for` `i ``in` `range``(n): ` `        ``resources.add(arr[i]); ` ` `  `        ``# return minimum of distinct resources ` `        ``# and n/2 ` `    ``return` `min``(``len``(resources), n ``/``/` `2``); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[ ``1``, ``1``, ``2``, ``1``, ``3``, ``4` `]; ` `    ``n ``=` `len``(arr); ` `    ``print``(distribution(arr, n), ""); ` `     `  `# This code is contributed by PrinciRaj1992 `

## C#

 `// C# program to equally divide n elements ` `// into two sets such that second set has ` `// maximum distinct elements. ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` ` `  `static` `int` `distribution(``int` `[]arr, ``int` `n) ` `{ ` `    ``HashSet<``int``> resources = ``new` `HashSet<``int``>(); ` ` `  `    ``// Insert all the resources in the set ` `    ``// There will be unique resources in the set ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `        ``resources.Add(arr[i]);  ` ` `  `    ``// return minimum of distinct resources ` `    ``// and n/2 ` `    ``return` `Math.Min(resources.Count, n / 2); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]arr = { 1, 1, 2, 1, 3, 4 }; ` `    ``int` `n = arr.Length; ` `    ``Console.Write(distribution(arr, n) +``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

`3`

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.