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:
- 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++
// C++ program for the above approach #include <bits/stdc++.h> 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[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[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 cout << dp[n] << endl; } // Driver Code int main() { int arr[] = { 1, 2, 1, 2, 2 }; int N = sizeof (arr) / sizeof (arr[0]); 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 [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 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
<script> // JavaScript Program for the above approach // Function to count number of sequences // satisfying the given criteria function countPossiblities(arr, n) { // Stores the index of the last // occurrence of the element let lastOccur = new Array(100000); for (let i = 0; i < n; i++) { lastOccur[i] = -1; } // Initialize an array to store the // number of different sequences // that are possible of length i dp = new Array(n + 1); // Base Case dp[0] = 1; for (let i = 1; i <= n; i++) { let 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 document.write(dp[n] + "<br>" ); } // Driver Code let arr = [1, 2, 1, 2, 2]; let N = arr.length; countPossiblities(arr, N); // This code is contributed by Potta Lokesh </script> |
Output:
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...