Count of maximum occurring subsequence using only those characters whose indices are in GP | C ++ program for the above approach ; Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Initialize 1 - D array and 2 - D dp array to 0 ; Iterate till the length of the given string ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Driver Code ; Given string s ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxTimes ( string S ) { long long int arr [ 26 ] ; long long int dp [ 26 ] [ 26 ] ; memset ( arr , 0 , sizeof ( arr ) ) ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < S . size ( ) ; i ++ ) { int now = S [ i ] - ' a ' ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ now ] += arr [ j ] ; } arr [ now ] ++ ; } long long int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = max ( ans , arr [ i ] ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = max ( ans , dp [ i ] [ j ] ) ; } } return ans ; } int main ( ) { string S = " ddee " ; cout << findMaxTimes ( S ) ; return 0 ; }