# Minimum number of segments required such that each segment has distinct elements

Given an array of integers, the task is to find the minimum number of segments that the array elements can be divided into such that all the segments contain distinct elements.

**Examples:**

Input:n=6 ; Array: 1, 7, 4, 3, 3, 8Output:2Explanation:Optimal way to create segments here is {1, 7, 4, 3} {3, 8} Clearly, the answer is the maximum frequency of any element within the array i.e. '2'. as '3' is the element which appears the most in the array (twice).Input :n=5 ; Array: 2, 2, 3, 3, 3, 5Output :3

**Approach: **

- The optimal approach is to put all the distinct elements in a single segment.
- And then, put all other elements that have several occurrences one by one in new segments such that no segment contains repetitions of elements.
- So, the answer is the maximum frequency of any element within the given array.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that counts the ` `// minimum segments required ` `void` `CountSegments(` `int` `N, ` `int` `a[]) ` `{ ` ` ` `// all values are '0' initially ` ` ` `int` `frequency[10001] = { 0 }; ` ` ` ` ` `// count of segments ` ` ` `int` `c = 0; ` ` ` ` ` `// store frequency of every element ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `frequency[a[i]]++; ` ` ` `} ` ` ` ` ` `// find maximum frequency ` ` ` `for` `(` `int` `i = 0; i <= 10000; i++) ` ` ` `c = max(c, frequency[i]); ` ` ` ` ` `cout << c << ` `"\n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 6; ` ` ` `int` `a[] = { 1, 3, 4, 3, 2, 3 }; ` ` ` ` ` `CountSegments(N, a); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function that counts the ` `// minimum segments required ` `static` `void` `CountSegments(` `int` `N, ` `int` `a[]) ` `{ ` ` ` `// all values are '0' initially ` ` ` `int` `frequency[] = ` `new` `int` `[` `10001` `]; ` ` ` ` ` `// count of segments ` ` ` `int` `c = ` `0` `; ` ` ` ` ` `// store frequency of every element ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` `frequency[a[i]]++; ` ` ` `} ` ` ` ` ` `// find maximum frequency ` ` ` `for` `(` `int` `i = ` `0` `; i <= ` `10000` `; i++) ` ` ` `c = Math.max(c, frequency[i]); ` ` ` ` ` `System.out.println( c); ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `N = ` `6` `; ` ` ` `int` `[]a = { ` `1` `, ` `3` `, ` `4` `, ` `3` `, ` `2` `, ` `3` `}; ` ` ` ` ` `CountSegments(N, a); ` ` ` `} ` `} ` ` ` `// This Code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 implementation of the approach ` ` ` `# Function that counts the ` `# minimum segments required ` `def` `CountSegments(N, a): ` ` ` ` ` `# all values are '0' initially ` ` ` `frequency ` `=` `[` `0` `] ` `*` `10001` ` ` ` ` `# count of segments ` ` ` `c ` `=` `0` ` ` ` ` `# store frequency of every element ` ` ` `for` `i ` `in` `range` `(N) : ` ` ` `frequency[a[i]] ` `+` `=` `1` ` ` ` ` `# find maximum frequency ` ` ` `for` `i ` `in` `range` `(` `10001` `): ` ` ` `c ` `=` `max` `(c, frequency[i]) ` ` ` ` ` `print` `(c) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `N ` `=` `6` ` ` `a ` `=` `[ ` `1` `, ` `3` `, ` `4` `, ` `3` `, ` `2` `, ` `3` `] ` ` ` `CountSegments(N, a) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that counts the ` `// minimum segments required ` `static` `void` `CountSegments(` `int` `N, ` `int` `[]a) ` `{ ` ` ` `// all values are '0' initially ` ` ` `int` `[]frequency = ` `new` `int` `[10001]; ` ` ` ` ` `// count of segments ` ` ` `int` `c = 0; ` ` ` ` ` `// store frequency of every element ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `frequency[a[i]]++; ` ` ` `} ` ` ` ` ` `// find maximum frequency ` ` ` `for` `(` `int` `i = 0; i <= 10000; i++) ` ` ` `c = Math.Max(c, frequency[i]); ` ` ` ` ` `Console.WriteLine( c); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `N = 6; ` ` ` `int` `[]a = { 1, 3, 4, 3, 2, 3 }; ` ` ` ` ` `CountSegments(N, a); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Find minimum changes required in an array for it to contain k distinct elements
- Find element using minimum segments in Seven Segment Display
- Minimum number of subsets with distinct elements
- Minimum number of distinct elements after removing m items
- Minimum number of operations required to delete all elements of the array
- Find the minimum number of operations required to make all array elements equal
- Number of segments where all elements are greater than X
- Divide an array into k segments to maximize maximum of segment minimums
- Partition the array in K segments such that bitwise AND of individual segment sum is maximized
- Minimum swaps required to bring all elements less than or equal to k together
- Minimum no. of operations required to make all Array Elements Zero
- Minimum operations required to make all the array elements equal
- Making elements distinct in a sorted array by minimum increments
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Minimum operations required to modify the array such that parity of adjacent elements is different

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.