Length of largest subsequence consisting of a pair of alternating digits | C ++ program for the above approach ; Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Variable initialization ; Nested loops for iteration ; Check if i is not equal to j ; Initialize length as 0 ; Iterate from 0 till the size of the string ; Increment length ; Increment length ; Update maxi ; Check if maxi is not equal to 1 the print it otherwise print 0 ; Driver Code ; Given string ; Function call
#include <iostream> NEW_LINE using namespace std ; void largestSubsequence ( string s ) { int maxi = 0 ; char prev1 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { if ( i != j ) { int len = 0 ; prev1 = j + '0' ; for ( int k = 0 ; k < s . size ( ) ; k ++ ) { if ( s [ k ] == i + '0' && prev1 == j + '0' ) { prev1 = s [ k ] ; len ++ ; } else if ( s [ k ] == j + '0' && prev1 == i + '0' ) { prev1 = s [ k ] ; len ++ ; } } maxi = max ( len , maxi ) ; } } } if ( maxi != 1 ) cout << maxi << endl ; else cout << 0 << endl ; } int main ( ) { string s = "1542745249842" ; largestSubsequence ( s ) ; return 0 ; }