Open In App

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, 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:  




#include <bits/stdc++.h>
using namespace std;
 
// Function to count the minimum minimum number of segments
// that the array elements can be divided into such that all
// the segments contain distinct elements.
int countSegment(vector<int>& arr)
{
    int n = arr.size();
 
    // count the number of segment required
    int count = 1;
 
    // For counting the frequency
    unordered_map<int, int> unmap;
 
    // Iterate over the array
    for (int i = 0; i < n; i++) {
 
        // Increment the frequency
        unmap[arr[i]]++;
 
        // Check if there is any duplicate in current
        // segment.
        if (unmap[arr[i]] > 1) {
 
            // Increment the segment required by 1.
            count++;
 
            // Clear the map
            unmap.clear();
 
            // Increment the frequency of current element in
            // map
            unmap[arr[i]]++;
        }
    }
 
    // Return the count.
    return count;
}
 
int main()
{
 
    // Input array.
    vector<int> arr = { 2, 2, 3, 3, 3, 5 };
 
    // Function call and store the result
    int result = countSegment(arr);
 
    // Print the result
    cout << result << endl;
 
    return 0;
}




import java.util.*;
 
public class Main {
 
    // Function to count the minimum minimum number of
    // segments that the array elements can be divided into
    // such that all the segments contain distinct elements.
    public static int countSegment(ArrayList<Integer> arr)
    {
        int n = arr.size();
 
        // count the number of segments required
        int count = 1;
 
        // For counting the frequency
        HashMap<Integer, Integer> map
            = new HashMap<Integer, Integer>();
 
        // Iterate over the array
        for (int i = 0; i < n; i++) {
 
            // Increment the frequency
            map.put(arr.get(i),
                    map.getOrDefault(arr.get(i), 0) + 1);
 
            // Check if there is any duplicate in current
            // segment.
            if (map.get(arr.get(i)) > 1) {
 
                // Increment the segment required by 1.
                count++;
 
                // Clear the map
                map.clear();
 
                // Increment the frequency of current
                // element in map
                map.put(arr.get(i), 1);
            }
        }
 
        // Return the count.
        return count;
    }
 
    public static void main(String[] args)
    {
 
        // Input array.
        ArrayList<Integer> arr = new ArrayList<Integer>();
        arr.add(2);
        arr.add(2);
        arr.add(3);
        arr.add(3);
        arr.add(3);
        arr.add(5);
 
        // Function call and store the result
        int result = countSegment(arr);
 
        // Print the result
        System.out.println(result);
    }
}




# Function to count the minimum number of segments that the array elements
# can be divided into such that all the segments contain distinct elements.
 
 
def countSegment(arr):
    n = len(arr)
 
    # Count the number of segments required
    count = 1
 
    # For counting the frequency
    unmap = {}
 
    # Iterate over the array
    for i in range(n):
 
        # Increment the frequency
        if arr[i] not in unmap:
            unmap[arr[i]] = 1
        else:
            unmap[arr[i]] += 1
 
        # Check if there is any duplicate in current segment.
        if unmap[arr[i]] > 1:
 
            # Increment the segment required by 1.
            count += 1
 
            # Clear the map
            unmap = {}
 
            # Increment the frequency of current element in map
            unmap[arr[i]] = 1
 
    # Return the count.
    return count
 
 
# Input array
arr = [2, 2, 3, 3, 3, 5]
 
# Function call and store the result
result = countSegment(arr)
 
# Print the result
print(result)




using System;
using System.Collections.Generic;
 
class Program {
    // Function to count the minimum minimum number of
    // segments that the array elements can be divided into
    // such that all the segments contain distinct elements.
    static int CountSegment(List<int> arr)
    {
        int n = arr.Count;
 
        // count the number of segment required
        int count = 1;
 
        // For counting the frequency
        Dictionary<int, int> dict
            = new Dictionary<int, int>();
 
        // Iterate over the array
        for (int i = 0; i < n; i++) {
            // Increment the frequency
            if (dict.ContainsKey(arr[i]))
                dict[arr[i]]++;
            else
                dict.Add(arr[i], 1);
 
            // Check if there is any duplicate in current
            // segment.
            if (dict[arr[i]] > 1) {
                // Increment the segment required by 1.
                count++;
 
                // Clear the map
                dict.Clear();
 
                // Increment the frequency of current
                // element in map
                dict[arr[i]] = 1;
            }
        }
 
        // Return the count.
        return count;
    }
 
    static void Main(string[] args)
    {
        // Input array.
        List<int> arr = new List<int>{ 2, 2, 3, 3, 3, 5 };
 
        // Function call and store the result
        int result = CountSegment(arr);
 
        // Print the result
        Console.WriteLine(result);
    }
}




// Function to count the minimum number of segments that the array elements can be divided into such that all the segments contain distinct elements.
function countSegment(arr) {
    const n = arr.length;
 
    // count the number of segment required
    let count = 1;
 
    // For counting the frequency
    const unmap = new Map();
 
    // Iterate over the array
    for (let i = 0; i < n; i++) {
        // Increment the frequency
        if (unmap.has(arr[i])) {
            unmap.set(arr[i], unmap.get(arr[i]) + 1);
        } else {
            unmap.set(arr[i], 1);
        }
 
        // Check if there is any duplicate in current segment.
        if (unmap.get(arr[i]) > 1) {
            // Increment the segment required by 1.
            count++;
 
            // Clear the map
            unmap.clear();
 
            // Increment the frequency of current element in map
            unmap.set(arr[i], 1);
        }
    }
 
    // Return the count.
    return count;
}
 
// Input array.
const arr = [2, 2, 3, 3, 3, 5];
 
// Function call and store the result
const result = countSegment(arr);
 
// Print the result
console.log(result);

Output
4

Time Complexity: O(n)
Auxiliary Space: O(n)


Article Tags :