Minimum number of segments required such that each segment has distinct elements
Last Updated :
24 Apr, 2023
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, 8
Output: 2
Explanation:
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 = 6; Array: 2, 2, 3, 3, 3, 5
Output : 4
Below is the implementation of the above approach:
- Count the number of segments required
- For counting the frequency
- Iterate over the array
- Increment the frequency
- Check if there is any duplicate in current segment.
- Increment the segment required by 1.
- Clear the map
- Increment the frequency of current element in map.
- Return the count.
C++
#include <bits/stdc++.h>
using namespace std;
int countSegment(vector< int >& arr)
{
int n = arr.size();
int count = 1;
unordered_map< int , int > unmap;
for ( int i = 0; i < n; i++) {
unmap[arr[i]]++;
if (unmap[arr[i]] > 1) {
count++;
unmap.clear();
unmap[arr[i]]++;
}
}
return count;
}
int main()
{
vector< int > arr = { 2, 2, 3, 3, 3, 5 };
int result = countSegment(arr);
cout << result << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int countSegment(ArrayList<Integer> arr)
{
int n = arr.size();
int count = 1 ;
HashMap<Integer, Integer> map
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
map.put(arr.get(i),
map.getOrDefault(arr.get(i), 0 ) + 1 );
if (map.get(arr.get(i)) > 1 ) {
count++;
map.clear();
map.put(arr.get(i), 1 );
}
}
return count;
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add( 2 );
arr.add( 2 );
arr.add( 3 );
arr.add( 3 );
arr.add( 3 );
arr.add( 5 );
int result = countSegment(arr);
System.out.println(result);
}
}
|
Python3
def countSegment(arr):
n = len (arr)
count = 1
unmap = {}
for i in range (n):
if arr[i] not in unmap:
unmap[arr[i]] = 1
else :
unmap[arr[i]] + = 1
if unmap[arr[i]] > 1 :
count + = 1
unmap = {}
unmap[arr[i]] = 1
return count
arr = [ 2 , 2 , 3 , 3 , 3 , 5 ]
result = countSegment(arr)
print (result)
|
C#
using System;
using System.Collections.Generic;
class Program {
static int CountSegment(List< int > arr)
{
int n = arr.Count;
int count = 1;
Dictionary< int , int > dict
= new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (dict.ContainsKey(arr[i]))
dict[arr[i]]++;
else
dict.Add(arr[i], 1);
if (dict[arr[i]] > 1) {
count++;
dict.Clear();
dict[arr[i]] = 1;
}
}
return count;
}
static void Main( string [] args)
{
List< int > arr = new List< int >{ 2, 2, 3, 3, 3, 5 };
int result = CountSegment(arr);
Console.WriteLine(result);
}
}
|
Javascript
function countSegment(arr) {
const n = arr.length;
let count = 1;
const unmap = new Map();
for (let i = 0; i < n; i++) {
if (unmap.has(arr[i])) {
unmap.set(arr[i], unmap.get(arr[i]) + 1);
} else {
unmap.set(arr[i], 1);
}
if (unmap.get(arr[i]) > 1) {
count++;
unmap.clear();
unmap.set(arr[i], 1);
}
}
return count;
}
const arr = [2, 2, 3, 3, 3, 5];
const result = countSegment(arr);
console.log(result);
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...