Given an array arr. The task is to divide the array into the minimum number of subarrays containing unique elements and return the count of such subarrays.
Note: An array element cannot be present in more than one subarray.
Examples :
Input : arr[] = {1, 2, 1, 1, 2, 3}
Output : 3
Explanation : The subarrays having unique elements are { 1, 2 }, { 1 }, and { 1, 2, 3 }Input : arr[] = {1, 2, 3, 4, 5}
Output : 1
Explanation : The subarray having unique elements is { 1, 2, 3, 4, 5 }
Approach:
The idea is to maintain a set while traversing the array. While traversing, if an element is already found in the set, then increase the count of subarray by 1 as we have to include the current element in the next subarray and clear the set for new subarray. Then, proceed for the complete array in a self-similar manner. The variable storing the count will be the answer.
Below is the implementation of the above approach:
// C++ program to count minimum subarray having // unique elements #include <bits/stdc++.h> using namespace std;
// Function to count minimum number of subarrays int minimumSubarrays( int ar[], int n)
{ set< int > se;
int cnt = 1;
for ( int i = 0; i < n; i++) {
// Checking if an element already exist in
// the current sub-array
if (se.count(ar[i]) == 0) {
// inserting the current element
se.insert(ar[i]);
}
else {
cnt++;
// clear set for new possible value of subarrays
se.clear();
// inserting the current element
se.insert(ar[i]);
}
}
return cnt;
} // Driver Code int main()
{ int ar[] = { 1, 2, 1, 3, 4, 2, 4, 4, 4 };
int n = sizeof (ar) / sizeof (ar[0]);
cout << minimumSubarrays(ar, n);
return 0;
} |
// Java implementation of the approach import java.util.*;
class GFG
{ // Function to count minimum number of subarrays
static int minimumSubarrays( int ar[], int n)
{
Vector se = new Vector();
int cnt = 1 ;
for ( int i = 0 ; i < n; i++)
{
// Checking if an element already exist in
// the current sub-array
if (se.contains(ar[i]) == false )
{
// inserting the current element
se.add(ar[i]);
}
else
{
cnt++;
// clear set for new possible value
// of subarrays
se.clear();
// inserting the current element
se.add(ar[i]);
}
}
return cnt;
}
// Driver Code
public static void main (String[] args)
{
int ar[] = { 1 , 2 , 1 , 3 , 4 , 2 , 4 , 4 , 4 };
int n = ar.length ;
System.out.println(minimumSubarrays(ar, n));
}
} // This code is contributed by AnkitRai01 |
# Python 3 implementation of the approach # Function to count minimum number of subarrays def minimumSubarrays(ar, n) :
se = []
cnt = 1 ;
for i in range (n) :
# Checking if an element already exist in
# the current sub-array
if se.count(ar[i]) = = 0 :
# inserting the current element
se.append(ar[i])
else :
cnt + = 1
# clear set for new possible value
# of subarrays
se.clear()
# inserting the current element
se.append(ar[i])
return cnt
# Driver Code ar = [ 1 , 2 , 1 , 3 , 4 , 2 , 4 , 4 , 4 ]
n = len (ar)
print (minimumSubarrays(ar, n))
# This code is contributed by # divyamohan123 |
// C# implementation of the approach using System;
using System.Collections.Generic;
class GFG
{ // Function to count minimum number of subarrays
static int minimumSubarrays( int []ar, int n)
{
List< int > se = new List< int >();
int cnt = 1;
for ( int i = 0; i < n; i++)
{
// Checking if an element already exist in
// the current sub-array
if (se.Contains(ar[i]) == false )
{
// inserting the current element
se.Add(ar[i]);
}
else
{
cnt++;
// clear set for new possible value
// of subarrays
se.Clear();
// inserting the current element
se.Add(ar[i]);
}
}
return cnt;
}
// Driver Code
public static void Main(String[] args)
{
int []ar = { 1, 2, 1, 3, 4, 2, 4, 4, 4 };
int n = ar.Length ;
Console.WriteLine(minimumSubarrays(ar, n));
}
} // This code is contributed by 29AjayKumar |
<script> // Javascript implementation of the approach
// Function to count minimum number of subarrays
function minimumSubarrays(ar, n)
{
let se = new Set();
let cnt = 1;
for (let i = 0; i < n; i++)
{
// Checking if an element already exist in
// the current sub-array
if (se.has(ar[i]) == false )
{
// inserting the current element
se.add(ar[i]);
}
else
{
cnt++;
// clear set for new possible value
// of subarrays
se.clear();
// inserting the current element
se.add(ar[i]);
}
}
return cnt;
}
// Driver code
let ar = [ 1, 2, 1, 3, 4, 2, 4, 4, 4 ];
let n = ar.length ;
document.write(minimumSubarrays(ar, n));
// This code is contributed by susmitakundugoaldanga. </script> |
Output
5
Time Complexity :
Auxiliary Space: O(n)
Method 2 Using Unordered_map
instead of using set we can use unordered_map for counting subarray without repeating elements in them. as above discussed if we use set in solution then it takes O(n*log(n)) time complexity rather than that if we use unordered_map in solution then time complexity will be O(n). so it’s better in terms of time complexity. below is the code for this solution
// C++ program to count minimum subarray having unique elements #include <bits/stdc++.h> using namespace std;
// Function to count minimum number of subarrays int minimumSubarrays( int ar[], int n)
{ // map to store the element
unordered_map< int , int > mp;
int cnt = 1;
for ( int i = 0; i < n; i++) {
// Checking if an element already exist in the current sub-array
if (mp[ar[i]]) {
cnt++;
// clear map for new possible value of subarrays
mp.clear();
// inserting the current element
mp[ar[i]]++;
}
else {
// inserting the current element
mp[ar[i]]++;
}
}
return cnt;
} // Driver Code int main() {
int ar[] = { 1, 2, 1, 3, 4, 2, 4, 4, 4 };
int n = sizeof (ar) / sizeof (ar[0]);
// function call
cout << minimumSubarrays(ar, n);
return 0;
} |
// Java program to count minimum subarray having unique elements import java.util.*;
class GFG {
// Function to count minimum number of subarrays
public static int minimumSubarrays( int ar[], int n)
{
// map to store the element
Map < Integer, Integer > mp = new HashMap < Integer, Integer > ();
int cnt = 1 ;
for ( int i = 0 ; i < n; i++) {
// Checking if an element already exist in the current sub-array
if (mp.containsKey(ar[i])) {
cnt++;
// clear map for new possible value of subarrays
mp.clear();
// inserting the current element
mp.put(ar[i], 1 );
} else {
// inserting the current element
mp.put(ar[i], 1 );
}
}
return cnt;
}
public static void main(String[] args) {
int ar[] = { 1 , 2 , 1 , 3 , 4 , 2 , 4 , 4 , 4 };
int n = ar.length;
// function call
System.out.println(minimumSubarrays(ar, n));
}
} // This code is contributed by ajaymakavana. |
# Python program to count minimum subarray # having unique elements # function to count minimum number of subarrays def minimumSubarrays(ar, n):
# map to store the element
mp = {}
cnt = 1
for i in range (n):
# checking if an element already
# exist in the current sub-array
if (ar[i] in mp):
cnt + = 1
# clear map for new possible
# value of subarrays
mp.clear()
# Inserting the current element
mp[ar[i]] = 1
else :
# Inserting the current element
mp[ar[i]] = 1
return cnt
ar = [ 1 , 2 , 1 , 3 , 4 , 2 , 4 , 4 , 4 ]
n = len (ar)
# Function call print (minimumSubarrays(ar, n))
# This code is contributed by lokesh. |
// C# program to count minimum subarray having unique elements using System;
using System.Collections.Generic;
public class GFG {
// Function to count minimum number of subarrays
public static int minimumSubarrays( int [] ar, int n) {
// map to store the element
Dictionary < int , int > mp = new Dictionary < int , int > ();
int cnt = 1;
for ( int i = 0; i < n; i++) {
// Checking if an element already exist in the current sub-array
if (mp.ContainsKey(ar[i])) {
cnt++;
// clear map for new possible value of subarrays
mp.Clear();
// inserting the current element
mp[ar[i]] = 1;
} else {
// inserting the current element
mp[ar[i]] = 1;
}
}
return cnt;
}
public static void Main( string [] args) {
int [] ar = {1, 2, 1, 3, 4, 2, 4, 4, 4 };
int n = ar.Length;
// function call
Console.WriteLine(minimumSubarrays(ar, n));
}
} // This code is contributed by ajaymakvana |
// Javascript program to count minimum subarray having unique elements // Function to count minimum number of subarrays function minimumSubarrays(ar,n)
{ // map to store the element
const mp = new Map();
let cnt = 1;
for (let i = 0; i < n; i++)
{
// Checking if an element already exist in the current sub-array
if (mp.has(ar[i]) == true )
{
cnt++;
// clear map for new possible value of subarrays
mp.clear();
// inserting the current element
mp.set(ar[i],1);
}
else
{
// inserting the current element
mp.set(ar[i],1);
}
}
return cnt;
} // Driver Code let ar = [1, 2, 1, 3, 4, 2, 4, 4, 4 ];
let n = ar.length;
// function call
console.log(minimumSubarrays(ar, n));
// This code is contributed by garg28harsh.
|
Output
5
Time Complexity: O(n)
Auxiliary Space: O(n)