Minimize Array partitions such that an element is repeated atmost once in each partition
Given an array arr[] consisting of positive integers. The task is to minimize the contiguous groups formed if each element from arr[] can be a part of atmost one group, and a number can be repeated only once.
Examples:
Input: arr[] = {1, 2, 1, 1, 1, 1, 1, 2, 3}
Output: { {1, 2, 1}, {1, 1}, {1, 1, 2, 3} }
Explanation: Following are the groups formed with given conditions.
In 1 2 1, 1 repeats 1 times.
In 1 1, 1 repeats 1 times.
In 1 1 2 3, 1 repeats 1 times.
Therefore, in total there are three groups formed, which is minimum possible.
Input: arr[] = {1, 1}
Output: { {1, 1} }
Approach: This problem can be solved by using HashMaps. Follow the steps below to solve the given problem.
- Take a variable say, count = 0 for storing the number of groups formed and a 2D vector for storing groups.
- Traverse the array arr[] and start storing the frequency of array elements in a map and storing the current element in the vector.
- Whenever the frequency of any element becomes 2, take a flag variable to track one number with frequency 2 has encountered before and just move forward. Use that flag variable to break any group and increment the count variable and push the vector in 2D vector and clear the whole temporary vector and map.
- Now print the 2D vector as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinimumGroups( int a[], int n)
{
map< int , int > m;
vector<vector< int > > v;
int c = 0;
vector< int > v1;
bool to = 0;
for ( int i = 0; i < n; i++) {
v1.push_back(a[i]);
m[a[i]]++;
if (m[a[i]] >= 2) {
if (to or m[a[i]] == 3) {
c++;
m.clear();
v1.pop_back();
v.push_back(v1);
v1.clear();
to = 0;
i--;
}
else
to = 1;
}
else if (i == n - 1) {
c++;
v.push_back(v1);
}
}
for ( int i = 0; i < v.size(); i++) {
for ( int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
int arr[] = { 1, 2, 1, 1, 1, 1, 1, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
findMinimumGroups(arr, N);
}
|
Java
import java.util.*;
class Main {
static void findMinimumGroups( int [] a, int n) {
Map<Integer, Integer> m = new HashMap<>();
List<List<Integer>> v = new ArrayList<>();
int c = 0 ;
List<Integer> v1 = new ArrayList<>();
boolean to = false ;
for ( int i = 0 ; i < n; i++) {
v1.add(a[i]);
m.put(a[i], m.getOrDefault(a[i], 0 ) + 1 );
if (m.get(a[i]) >= 2 ) {
if (to || m.get(a[i]) == 3 ) {
c++;
m.clear();
v1.remove(v1.size() - 1 );
v.add( new ArrayList<>(v1));
v1.clear();
to = false ;
i--;
} else {
to = true ;
}
} else if (i == n - 1 ) {
c++;
v.add( new ArrayList<>(v1));
}
}
for (List<Integer> list : v) {
for (Integer num : list) {
System.out.print(num + " " );
}
System.out.println();
}
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 1 , 1 , 1 , 1 , 1 , 2 , 3 };
int N = arr.length;
findMinimumGroups(arr, N);
}
}
|
Python3
def findMinimumGroups(a, n):
m = dict ()
v = []
c = 0
v1 = []
to = 0
i = 0
while (i < n):
v1.append(a[i])
if (a[i] in m):
m[a[i]] = m[a[i]] + 1
else :
m[a[i]] = 1
if (m[a[i]] > = 2 ):
if (to or m[a[i]] = = 3 ):
c + = 1
m.clear()
v1.pop()
v.append(v1)
v1 = []
to = 0
i - = 1
else :
to = 1
elif (i = = n - 1 ):
c + = 1
v.append(v1)
i + = 1
for i in range ( len (v)):
for j in range ( len (v[i])):
print (v[i][j], end = " " )
print ()
arr = [ 1 , 2 , 1 , 1 , 1 , 1 , 1 , 2 , 3 ]
N = len (arr)
findMinimumGroups(arr, N)
|
Javascript
<script>
function findMinimumGroups(a, n) {
let m = new Map();
let v = [];
let c = 0;
let v1 = [];
let to = 0;
for (let i = 0; i < n; i++) {
v1.push(a[i]);
if (m.has(a[i])) {
m.set(a[i], m.get(a[i]) + 1)
}
else {
m.set(a[i], 1)
}
if (m.get(a[i]) >= 2) {
if (to || m.get(a[i]) == 3) {
c++;
m.clear();
v1.pop();
v.push(v1);
v1 = [];
to = 0;
i--;
}
else
to = 1;
}
else if (i == n - 1) {
c++;
v.push(v1);
}
}
for (let i = 0; i < v.length; i++) {
for (let j = 0; j < v[i].length; j++) {
document.write(v[i][j] + " " );
}
document.write( '<br>' )
}
}
let arr = [1, 2, 1, 1, 1, 1, 1, 2, 3];
let N = arr.length;
findMinimumGroups(arr, N);
</script>
|
C#
using System;
using System.Collections.Generic;
class Program
{
static void FindMinimumGroups( int [] a, int n)
{
Dictionary< int , int > m = new Dictionary< int , int >();
List<List< int >> v = new List<List< int >>();
int c = 0;
List< int > v1 = new List< int >();
bool to = false ;
for ( int i = 0; i < n; i++)
{
v1.Add(a[i]);
if (m.ContainsKey(a[i]))
{
m[a[i]]++;
}
else
{
m[a[i]] = 1;
}
if (m[a[i]] >= 2)
{
if (to || m[a[i]] == 3)
{
c++;
m.Clear();
v1.RemoveAt(v1.Count - 1);
v.Add( new List< int >(v1));
v1.Clear();
to = false ;
i--;
}
else
{
to = true ;
}
}
else if (i == n - 1)
{
c++;
v.Add( new List< int >(v1));
}
}
foreach (List< int > list in v)
{
foreach ( int num in list)
{
Console.Write(num + " " );
}
Console.WriteLine();
}
}
static void Main( string [] args)
{
int [] arr = { 1, 2, 1, 1, 1, 1, 1, 2, 3 };
int N = arr.Length;
FindMinimumGroups(arr, N);
Console.ReadLine();
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
08 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...