# Count distinct sequences obtained by replacing all elements of subarrays having equal first and last elements with the first element any number of times

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:

1. The initial array {1, 2, 1, 2, 2}.
2. Choose indices 0 and 2 and as arr(= 1) and arr(= 1) are equal and update the array elements arr[] over the range [0, 2] to arr(= 1). The new sequence obtained is {1, 1, 1, 2, 2}.
3. Choose indices 1 and 3 and as arr(= 2) and arr(= 2) are equal and update the array elements arr[] over the range [1, 3] to arr(= 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 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 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++

 `// C++ program for the above approach`   `#include ` `using` `namespace` `std;`   `// Function to count number of sequences` `// satisfying the given criteria` `void` `countPossiblities(``int` `arr[], ``int` `n)` `{` `    ``// Stores the index of the last` `    ``// occurrence of the element` `    ``int` `lastOccur;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``lastOccur[i] = -1;` `    ``}`   `    ``// Initialize an array to store the` `    ``// number of different sequences` `    ``// that are possible of length i` `    ``int` `dp[n + 1];`   `    ``// Base Case` `    ``dp = 1;`   `    ``for` `(``int` `i = 1; i <= n; i++) {`   `        ``int` `curEle = arr[i - 1];`   `        ``// If no operation is applied` `        ``// on ith element` `        ``dp[i] = dp[i - 1];`   `        ``// If operation is applied on` `        ``// ith element` `        ``if` `(lastOccur[curEle] != -1` `            ``& lastOccur[curEle] < i - 1) {` `            ``dp[i] += dp[lastOccur[curEle]];` `        ``}`   `        ``// Update the last occurrence` `        ``// of curEle` `        ``lastOccur[curEle] = i;` `    ``}`   `    ``// Finally, print the answer` `    ``cout << dp[n] << endl;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `arr[] = { 1, 2, 1, 2, 2 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``countPossiblities(arr, N);`   `    ``return` `0;` `}`

## Java

 `// Java Program for the above approach` `import` `java.io.*;`   `class` `GFG {`   `    ``// Function to count number of sequences` `    ``// satisfying the given criteria` `    ``static` `void` `countPossiblities(``int` `arr[], ``int` `n)` `    ``{` `        ``// Stores the index of the last` `        ``// occurrence of the element` `        ``int``[] lastOccur = ``new` `int``[``100000``];` `        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``lastOccur[i] = -``1``;` `        ``}`   `        ``// Initialize an array to store the` `        ``// number of different sequences` `        ``// that are possible of length i` `        ``int``[] dp = ``new` `int``[n + ``1``];`   `        ``// Base Case` `        ``dp[``0``] = ``1``;`   `        ``for` `(``int` `i = ``1``; i <= n; i++) {`   `            ``int` `curEle = arr[i - ``1``];`   `            ``// If no operation is applied` `            ``// on ith element` `            ``dp[i] = dp[i - ``1``];`   `            ``// If operation is applied on` `            ``// ith element` `            ``if` `(lastOccur[curEle] != -``1` `                ``& lastOccur[curEle] < i - ``1``) {` `                ``dp[i] += dp[lastOccur[curEle]];` `            ``}`   `            ``// Update the last occurrence` `            ``// of curEle` `            ``lastOccur[curEle] = i;` `        ``}`   `        ``// Finally, print the answer` `        ``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);`   `    ``}` `}`   `    ``// This code is contributed by Potta Lokesh`

## Python3

 `# Python3 program for the above approach`   `# Function to count number of sequences` `# satisfying the given criteria` `def` `countPossiblities(arr, n):` `    `  `    ``# Stores the index of the last` `    ``# occurrence of the element` `    ``lastOccur ``=` `[``-``1``] ``*` `100000`   `    ``# Initialize an array to store the` `    ``# number of different sequences` `    ``# that are possible of length i` `    ``dp ``=` `[``0``] ``*` `(n ``+` `1``)`   `    ``# Base Case` `    ``dp[``0``] ``=` `1`   `    ``for` `i ``in` `range``(``1``, n ``+` `1``):` `        ``curEle ``=` `arr[i ``-` `1``]`   `        ``# If no operation is applied` `        ``# on ith element` `        ``dp[i] ``=` `dp[i ``-` `1``]`   `        ``# If operation is applied on` `        ``# ith element` `        ``if` `(lastOccur[curEle] !``=` `-``1` `and` `            ``lastOccur[curEle] < i ``-` `1``):` `            ``dp[i] ``+``=` `dp[lastOccur[curEle]]`   `        ``# Update the last occurrence` `        ``# of curEle` `        ``lastOccur[curEle] ``=` `i`   `    ``# Finally, print the answer` `    ``print``(dp[n])`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``arr ``=` `[ ``1``, ``2``, ``1``, ``2``, ``2` `]` `    ``N ``=` `len``(arr)` `    `  `    ``countPossiblities(arr, N)`   `# This code is contributed by mohit kumar 29`

## C#

 `// C# Program for the above approach` `using` `System;`   `class` `GFG {`   `    ``// Function to count number of sequences` `    ``// satisfying the given criteria` `    ``static` `void` `countPossiblities(``int``[] arr, ``int` `n)` `    ``{` `        ``// Stores the index of the last` `        ``// occurrence of the element` `        ``int``[] lastOccur = ``new` `int``;` `        ``for` `(``int` `i = 0; i < n; i++) {` `            ``lastOccur[i] = -1;` `        ``}`   `        ``// Initialize an array to store the` `        ``// number of different sequences` `        ``// that are possible of length i` `        ``int``[] dp = ``new` `int``[n + 1];`   `        ``// Base Case` `        ``dp = 1;`   `        ``for` `(``int` `i = 1; i <= n; i++) {`   `            ``int` `curEle = arr[i - 1];`   `            ``// If no operation is applied` `            ``// on ith element` `            ``dp[i] = dp[i - 1];`   `            ``// If operation is applied on` `            ``// ith element` `            ``if` `(lastOccur[curEle] != -1` `                ``& lastOccur[curEle] < i - 1) {` `                ``dp[i] += dp[lastOccur[curEle]];` `            ``}`   `            ``// Update the last occurrence` `            ``// of curEle` `            ``lastOccur[curEle] = i;` `        ``}`   `        ``// Finally, print the answer` `        ``Console.WriteLine(dp[n]);` `    ``}`   `    ``public` `static` `void` `Main()` `    ``{` `        ``int``[] arr = { 1, 2, 1, 2, 2 };` `        ``int` `N = arr.Length;` `        ``countPossiblities(arr, N);` `    ``}` `}`   `// This code is contributed by subham348.`

## Javascript

 ``

Output:

`3`

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next