Given an array, arr[] of size N, the task is to split the array into the maximum number of subarrays such that the first and the last occurrence of all distinct array element lies in a single subarray.
Examples:
Input: arr[] = {1, 1, 2, 2}
Output: 2
Explanation:
Split the array into subarrays {1, 1} and {2, 2}.
Therefore, the required output is 2.
Input: arr[] = {1, 2, 4, 1, 4, 7, 7, 8}
Output: 3
Explanation:
Split the array into subarrays {1, 2, 4, 1, 4}, {7, 7} and {8}.
Therefore, the required output is 3.
Approach: The idea is to use Hashing to store the index of the last occurrence of every array element. Follow the steps below to solve the problem:
- Initialize an array, say hash[] to store the index of the last occurrence of every array element.
- Traverse the array and check if the maximum index of the last occurrence of all the previous elements of the current subarray is less than or equal to the current index, then increment the count by 1.
- Finally, print the value of count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxCtSubarrays( int arr[], int N)
{
int hash[1000001] = { 0 };
for ( int i = 0; i < N; i++) {
hash[arr[i]] = i;
}
int maxIndex = -1;
int res = 0;
for ( int i = 0; i < N; i++) {
maxIndex = max(maxIndex,
hash[arr[i]]);
if (maxIndex == i) {
res++;
}
}
return res;
}
int main()
{
int arr[] = { 1, 2, 4, 1,
4, 7, 7, 8 };
int N = sizeof (arr)
/ sizeof (arr[0]);
cout << maxCtSubarrays(arr, N);
}
|
Java
import java.util.*;
class GFG {
static int maxCtSubarrays( int arr[],
int N)
{
int hash[] = new int [ 1000001 ];
for ( int i = 0 ; i < N; i++)
{
hash[arr[i]] = i;
}
int maxIndex = - 1 ;
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
maxIndex = Math.max(maxIndex,
hash[arr[i]]);
if (maxIndex == i)
{
res++;
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 1 ,
4 , 7 , 7 , 8 };
int N = arr.length;
System.out.print(maxCtSubarrays(arr, N));
}
}
|
Python3
def maxCtSubarrays(arr, N):
hash = [ 0 ] * ( 1000001 )
for i in range (N):
hash [arr[i]] = i
maxIndex = - 1
res = 0
for i in range (N):
maxIndex = max (maxIndex,
hash [arr[i]])
if (maxIndex = = i):
res + = 1
return res
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 1 ,
4 , 7 , 7 , 8 ]
N = len (arr)
print (maxCtSubarrays(arr, N))
|
C#
using System;
class GFG {
static int maxCtSubarrays( int []arr,
int N)
{
int []hash = new int [1000001];
for ( int i = 0; i < N; i++)
{
hash[arr[i]] = i;
}
int maxIndex = -1;
int res = 0;
for ( int i = 0; i < N; i++)
{
maxIndex = Math.Max(maxIndex,
hash[arr[i]]);
if (maxIndex == i)
{
res++;
}
}
return res;
}
public static void Main(String[] args)
{
int []arr = {1, 2, 4, 1,
4, 7, 7, 8};
int N = arr.Length;
Console.Write(maxCtSubarrays(arr, N));
}
}
|
Javascript
<script>
function maxCtSubarrays(arr, N)
{
let hash = new Array(1000001).fill(0);
for (let i = 0; i < N; i++)
{
hash[arr[i]] = i;
}
let maxIndex = -1;
let res = 0;
for (let i = 0; i < N; i++)
{
maxIndex = Math.max(maxIndex,
hash[arr[i]]);
if (maxIndex == i)
{
res++;
}
}
return res;
}
let arr = [1, 2, 4, 1,
4, 7, 7, 8];
let N = arr.length;
document.write(maxCtSubarrays(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(X) where X = 1000001
Related Topic: Subarrays, Subsequences, and Subsets in Array