Count distinct sequences obtained by replacing all elements of subarrays having equal first and last elements with the first element any number of times
Last Updated :
24 Feb, 2022
Given an array arr[] consisting of N integers, the task is to find the number of different sequences that can be formed after performing the below operation on the given array arr[] any number of times.
Choose two indices i and j such that arr[i] is equal to arr[j] and update all the elements in the range [i, j] in the array to arr[i].
Examples:
Input: arr[] = {1, 2, 1, 2, 2}
Output: 3
Explanation:
There can be three possible sequences:
- The initial array {1, 2, 1, 2, 2}.
- Choose indices 0 and 2 and as arr[0](= 1) and arr[2](= 1) are equal and update the array elements arr[] over the range [0, 2] to arr[0](= 1). The new sequence obtained is {1, 1, 1, 2, 2}.
- Choose indices 1 and 3 and as arr[1](= 2) and arr[3](= 2) are equal and update the array elements arr[] over the range [1, 3] to arr[1](= 2). The new sequence obtained is {1, 2, 2, 2, 2}.
Therefore, the total number of sequences formed is 3.
Input: arr[] = {4, 2, 5, 4, 2, 4}
Output: 5
Approach: This problem can be solved using Dynamic Programming. Follow the steps below to solve the problem:
- Initialize an auxiliary array dp[] where dp[i] stores the number of different sequences that are possible by first i elements of the given array arr[] and initialize dp[0] as 1.
- Initialize an array lastOccur[] where lastOccur[i] stores the last occurrence of element arr[i] in the first i elements of the array arr[] and initialize lastOccur[0] with -1.
- Iterate over the range [1, N] using the variable i and perform the following steps:
- Update the value of dp[i] as dp[i – 1].
- If last occurrence of the current element is not equal to -1 and less than (i – 1), then add the value of dp[lastOccur[curEle]] to dp[i].
- Update the value of lastOccur[curEle] as i.
- After completing the above steps, print the value of dp[N] as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPossiblities( int arr[], int n)
{
int lastOccur[100000];
for ( int i = 0; i < n; i++) {
lastOccur[i] = -1;
}
int dp[n + 1];
dp[0] = 1;
for ( int i = 1; i <= n; i++) {
int curEle = arr[i - 1];
dp[i] = dp[i - 1];
if (lastOccur[curEle] != -1
& lastOccur[curEle] < i - 1) {
dp[i] += dp[lastOccur[curEle]];
}
lastOccur[curEle] = i;
}
cout << dp[n] << endl;
}
int main()
{
int arr[] = { 1, 2, 1, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
countPossiblities(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void countPossiblities( int arr[], int n)
{
int [] lastOccur = new int [ 100000 ];
for ( int i = 0 ; i < n; i++) {
lastOccur[i] = - 1 ;
}
int [] dp = new int [n + 1 ];
dp[ 0 ] = 1 ;
for ( int i = 1 ; i <= n; i++) {
int curEle = arr[i - 1 ];
dp[i] = dp[i - 1 ];
if (lastOccur[curEle] != - 1
& lastOccur[curEle] < i - 1 ) {
dp[i] += dp[lastOccur[curEle]];
}
lastOccur[curEle] = i;
}
System.out.println(dp[n]);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 1 , 2 , 2 };
int N = arr.length;
countPossiblities(arr, N);
}
}
|
Python3
def countPossiblities(arr, n):
lastOccur = [ - 1 ] * 100000
dp = [ 0 ] * (n + 1 )
dp[ 0 ] = 1
for i in range ( 1 , n + 1 ):
curEle = arr[i - 1 ]
dp[i] = dp[i - 1 ]
if (lastOccur[curEle] ! = - 1 and
lastOccur[curEle] < i - 1 ):
dp[i] + = dp[lastOccur[curEle]]
lastOccur[curEle] = i
print (dp[n])
if __name__ = = '__main__' :
arr = [ 1 , 2 , 1 , 2 , 2 ]
N = len (arr)
countPossiblities(arr, N)
|
C#
using System;
class GFG {
static void countPossiblities( int [] arr, int n)
{
int [] lastOccur = new int [100000];
for ( int i = 0; i < n; i++) {
lastOccur[i] = -1;
}
int [] dp = new int [n + 1];
dp[0] = 1;
for ( int i = 1; i <= n; i++) {
int curEle = arr[i - 1];
dp[i] = dp[i - 1];
if (lastOccur[curEle] != -1
& lastOccur[curEle] < i - 1) {
dp[i] += dp[lastOccur[curEle]];
}
lastOccur[curEle] = i;
}
Console.WriteLine(dp[n]);
}
public static void Main()
{
int [] arr = { 1, 2, 1, 2, 2 };
int N = arr.Length;
countPossiblities(arr, N);
}
}
|
Javascript
<script>
function countPossiblities(arr, n)
{
let lastOccur = new Array(100000);
for (let i = 0; i < n; i++) {
lastOccur[i] = -1;
}
dp = new Array(n + 1);
dp[0] = 1;
for (let i = 1; i <= n; i++) {
let curEle = arr[i - 1];
dp[i] = dp[i - 1];
if (lastOccur[curEle] != -1
& lastOccur[curEle] < i - 1) {
dp[i] += dp[lastOccur[curEle]];
}
lastOccur[curEle] = i;
}
document.write(dp[n] + "<br>" );
}
let arr = [1, 2, 1, 2, 2];
let N = arr.length;
countPossiblities(arr, N);
</script>
|
Output:
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...