codeparrot/xlcost-text-to-code huggingface dataset:
Longest Increasing consecutive subsequence | CPP program to find length of the longest increasing subsequence whose adjacent element differ by 1 ; function that returns the length of the longest increasing subsequence whose adjacent element differ by 1 ; stores the index of elements ; stores the length of the longest subsequence that ends with a [ i ] ; iterate for all element ; if a [ i ] - 1 is present before i - th index ; last index of a [ i ] - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubsequence ( int a [ ] , int n ) { unordered_map < int , int > mp ; int dp [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; int maximum = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . find ( a [ i ] - 1 ) != mp . end ( ) ) { int lastIndex = mp [ a [ i ] - 1 ] - 1 ; dp [ i ] = 1 + dp [ lastIndex ] ; } else dp [ i ] = 1 ; mp [ a [ i ] ] = i + 1 ; maximum = max ( maximum , dp [ i ] ) ; } return maximum ; } int main ( ) { int a [ ] = { 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << longestSubsequence ( a , n ) ; return 0 ; }
Longest subsequence such that difference between adjacents is one | Set 2 | C ++ implementation to find longest subsequence such that difference between adjacents is one ; function to find longest subsequence such that difference between adjacents is one ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last element of that subsequence ; to store the longest length subsequence ; traverse the array elements ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' um ' and its length of subsequence is greater than ' len ' ; if ' arr [ i ] + 1' is in ' um ' and its length of subsequence is greater than ' len ' ; update arr [ i ] subsequence length in ' um ' ; update longest length ; required longest length subsequence ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longLenSub ( int arr [ ] , int n ) { unordered_map < int , int > um ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( um . find ( arr [ i ] - 1 ) != um . end ( ) && len < um [ arr [ i ] - 1 ] ) len = um [ arr [ i ] - 1 ] ; if ( um . find ( arr [ i ] + 1 ) != um . end ( ) && len < um [ arr [ i ] + 1 ] ) len = um [ arr [ i ] + 1 ] ; um [ arr [ i ] ] = len + 1 ; if ( longLen < um [ arr [ i ] ] ) longLen = um [ arr [ i ] ] ; } return longLen ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Longest ▁ length ▁ subsequence ▁ = ▁ " << longLenSub ( arr , n ) ; return 0 ; }
Count nodes having highest value in the path from root to itself in a Binary Tree | C ++ 14 program for the above approach ; Stores the ct of nodes which are maximum in the path from root to the current node ; Binary Tree Node ; Function that performs Inorder Traversal on the Binary Tree ; If root does not exist ; Check if the node satisfies the condition ; Update the maximum value and recursively traverse left and right subtree ; Function that counts the good nodes in the given Binary Tree ; Perform inorder Traversal ; Return the final count ; Driver code ; A Binary Tree 3 / \ 2 5 / \ 4 6 ; Function call ; Print the count of good nodes
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ct = 0 ; struct Node { int val ; Node * left , * right ; Node ( int x ) { val = x ; left = right = NULL ; } } ; void find ( Node * root , int mx ) { if ( root == NULL ) return ; if ( root -> val >= mx ) ct ++ ; find ( root -> left , max ( mx , root -> val ) ) ; find ( root -> right , max ( mx , root -> val ) ) ; } int NodesMaxInPath ( Node * root ) { find ( root , INT_MIN ) ; return ct ; } int main ( ) { Node * root = new Node ( 3 ) ; root -> left = new Node ( 2 ) ; root -> right = new Node ( 5 ) ; root -> left -> left = new Node ( 4 ) ; root -> right -> right = new Node ( 7 ) ; int answer = NodesMaxInPath ( root ) ; cout << ( answer ) ; return 0 ; }
Longest Consecutive Subsequence | C ++ program to find longest contiguous subsequence ; Returns length of the longest contiguous subsequence ; sort the array ; insert repeated elements only once in the vector ; find the maximum length by traversing the array ; Check if the current element is equal to previous element + 1 ; update the maximum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLongestConseqSubseq ( int arr [ ] , int n ) { int ans = 0 , count = 0 ; sort ( arr , arr + n ) ; vector < int > v ; v . push_back ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) v . push_back ( arr [ i ] ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( i > 0 && v [ i ] == v [ i - 1 ] + 1 ) count ++ ; else count = 1 ; ans = max ( ans , count ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = sizeof arr / sizeof arr [ 0 ] ; cout << " Length ▁ of ▁ the ▁ Longest ▁ contiguous ▁ subsequence ▁ " " is ▁ " << findLongestConseqSubseq ( arr , n ) ; return 0 ; }
Count smaller primes on the right of each array element | C ++ Program for the above approach ; Function to check if a number is prime or not ; Function to update a Binary Tree ; Function to find the sum of all the elements which are less than or equal to index ; Function to find the number of smaller primes on the right for every array element ; Iterate the array in backwards ; Calculating the required number of primes ; If current array element is prime ; Update the Fenwick tree ; Driver Code ; Function call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; const int maxn = 1e6 + 5 ; int BITree [ maxn ] ; bool is_prime ( int n ) { if ( n <= 1 ) return 0 ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return 0 ; return 1 ; } void update_bitree ( int BITree [ ] , int index , int value ) { while ( index <= maxn ) { BITree [ index ] += value ; index += ( index & ( - index ) ) ; } } int sum_bitree ( int BITree [ ] , int index ) { int s = 0 ; while ( index > 0 ) { s += BITree [ index ] ; index -= ( index & ( - index ) ) ; } return s ; } void countSmallerPrimes ( int BITree [ ] , int ar [ ] , int N ) { int ans [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { ans [ i ] = sum_bitree ( BITree , ar [ i ] ) ; if ( is_prime ( ar [ i ] ) ) update_bitree ( BITree , ar [ i ] , 1 ) ; } for ( int i = 0 ; i < N ; i ++ ) cout << ans [ i ] << " ▁ " ; } int main ( ) { int ar [ ] = { 5 , 5 , 17 , 9 , 12 , 15 , 11 , 7 , 39 , 3 } ; int N = sizeof ar / sizeof ar [ 0 ] ; countSmallerPrimes ( BITree , ar , N ) ; return 0 ; }
Longest Consecutive Subsequence | C ++ program to find longest contiguous subsequence ; Returns length of the longest contiguous subsequence ; Hash all the array elements ; check each possible sequence from the start then update optimal length ; if current element is the starting element of a sequence ; Then check for next elements in the sequence ; update optimal length if this length is more ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLongestConseqSubseq ( int arr [ ] , int n ) { unordered_set < int > S ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) S . insert ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . find ( arr [ i ] - 1 ) == S . end ( ) ) { int j = arr [ i ] ; while ( S . find ( j ) != S . end ( ) ) j ++ ; ans = max ( ans , j - arr [ i ] ) ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = sizeof arr / sizeof arr [ 0 ] ; cout << " Length ▁ of ▁ the ▁ Longest ▁ contiguous ▁ subsequence ▁ " " is ▁ " << findLongestConseqSubseq ( arr , n ) ; return 0 ; }
Longest Consecutive Subsequence | CPP program for the above approach ; return the length of the longest subsequence of consecutive integers ; adding element from array to PriorityQueue ; Storing the first element of the Priority Queue This first element is also the smallest element ; Taking a counter variable with value 1 ; Storing value of max as 1 as there will always be one element ; check if current peek element minus previous element is greater then 1 This is done because if it ' s ▁ greater ▁ than ▁ 1 ▁ ▁ then ▁ the ▁ sequence ▁ ▁ doesn ' t start or is broken here ; Store the value of counter to 1 As new sequence may begin ; Update the previous position with the current peek And remove it ; Check if the previous element and peek are same ; Update the previous position with the current peek And remove it ; If the difference between previous element and peek is 1 ; Update the counter These are consecutive elements ; Update the previous position with the current peek And remove it ; Check if current longest subsequence is the greatest ; Store the current subsequence count as max ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLongestConseqSubseq ( int arr [ ] , int N ) { priority_queue < int , vector < int > , greater < int > > pq ; for ( int i = 0 ; i < N ; i ++ ) { pq . push ( arr [ i ] ) ; } int prev = pq . top ( ) ; pq . pop ( ) ; int c = 1 ; int max = 1 ; while ( ! pq . empty ( ) ) { if ( pq . top ( ) - prev > 1 ) { c = 1 ; prev = pq . top ( ) ; pq . pop ( ) ; } else if ( pq . top ( ) - prev == 0 ) { prev = pq . top ( ) ; pq . pop ( ) ; } else { c ++ ; prev = pq . top ( ) ; pq . pop ( ) ; } if ( max < c ) { max = c ; } } return max ; } int main ( ) { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = 7 ; cout << " Length ▁ of ▁ the ▁ Longest ▁ consecutive ▁ subsequence ▁ " " is ▁ " << findLongestConseqSubseq ( arr , n ) ; return 0 ; }
Kth space | C ++ program of the above approach ; Function to extract integer at key position in the given string ; strtok ( ) : Extracts the number at key c_str ( ) : Type cast string to char * ; Driver Code ; Given string ; Given K ; Function call
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void print_kth_string ( string str , int K ) { char * s = strtok ( ( char * ) str . c_str ( ) , " ▁ " ) ; while ( K > 1 ) { s = strtok ( NULL , " ▁ " ) ; K -- ; } cout << string ( s ) << " ▁ " ; } int main ( ) { string s ( "10 ▁ 20 ▁ 30 ▁ 40" ) ; int K = 2 ; print_kth_string ( s , K ) ; return 0 ; }
Largest increasing subsequence of consecutive integers | C ++ implementation of longest continuous increasing subsequence ; Function for LIS ; Initialize result ; iterate through array and find end index of LIS and its Size ; print LIS size ; print LIS after setting start element ; driver
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLIS ( int A [ ] , int n ) { unordered_map < int , int > hash ; int LIS_size = 1 ; int LIS_index = 0 ; hash [ A [ 0 ] ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1 ; if ( LIS_size < hash [ A [ i ] ] ) { LIS_size = hash [ A [ i ] ] ; LIS_index = A [ i ] ; } } cout << " LIS _ size ▁ = ▁ " << LIS_size << " STRNEWLINE " ; cout << " LIS ▁ : ▁ " ; int start = LIS_index - LIS_size + 1 ; while ( start <= LIS_index ) { cout << start << " ▁ " ; start ++ ; } } int main ( ) { int A [ ] = { 2 , 5 , 3 , 7 , 4 , 8 , 5 , 13 , 6 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findLIS ( A , n ) ; return 0 ; }
Count subsets having distinct even numbers | C ++ implementation to count subsets having even numbers only and all are distinct ; function to count the required subsets ; inserting even numbers in the set ' us ' single copy of each number is retained ; distinct even numbers ; total count of required subsets ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubsets ( int arr [ ] , int n ) { unordered_set < int > us ; int even_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) us . insert ( arr [ i ] ) ; unordered_set < int > :: iterator itr ; even_count = us . size ( ) ; return ( pow ( 2 , even_count ) - 1 ) ; } int main ( ) { int arr [ ] = { 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Number ▁ of ▁ subsets ▁ = ▁ " << countSubsets ( arr , n ) ; return 0 ; }
Count distinct elements in every window of size k | Simple C ++ program to count distinct elements in every window of size k ; Counts distinct elements in window of size k ; Traverse the window ; Check if element arr [ i ] exists in arr [ 0. . i - 1 ] ; Counts distinct elements in all windows of size k ; Traverse through every window ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWindowDistinct ( int win [ ] , int k ) { int dist_count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( win [ i ] == win [ j ] ) break ; if ( j == i ) dist_count ++ ; } return dist_count ; } void countDistinct ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i <= n - k ; i ++ ) cout << countWindowDistinct ( arr + i , k ) << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 3 , 4 , 2 , 3 } , k = 4 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countDistinct ( arr , n , k ) ; return 0 ; }
Iterative method to check if two trees are mirror of each other | C ++ implementation to check whether the two binary tress are mirrors of each other or not ; structure of a node in binary tree ; Utility function to create and return a new node for a binary tree ; function to check whether the two binary trees are mirrors of each other or not ; iterative inorder traversal of 1 st tree and reverse inoder traversal of 2 nd tree ; if the corresponding nodes in the two traversal have different data values , then they are not mirrors of each other . ; if at any point one root becomes null and the other root is not null , then they are not mirrors . This condition verifies that structures of tree are mirrors of each other . ; we have visited the node and its left subtree . Now , it ' s ▁ right ▁ subtree ' s turn ; we have visited the node and its right subtree . Now , it ' s ▁ left ▁ subtree ' s turn ; both the trees have been completely traversed ; tress are mirrors of each other ; Driver program to test above ; 1 st binary tree formation ; 1 ; / \ ; 3 2 ; / \ ; 2 nd binary tree formation ; 1 ; / \ ; 2 3 ; / \ ; 4 5
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } string areMirrors ( Node * root1 , Node * root2 ) { stack < Node * > st1 , st2 ; while ( 1 ) { while ( root1 && root2 ) { if ( root1 -> data != root2 -> data ) return " No " ; st1 . push ( root1 ) ; st2 . push ( root2 ) ; root1 = root1 -> left ; root2 = root2 -> right ; } if ( ! ( root1 == NULL && root2 == NULL ) ) return " No " ; if ( ! st1 . empty ( ) && ! st2 . empty ( ) ) { root1 = st1 . top ( ) ; root2 = st2 . top ( ) ; st1 . pop ( ) ; st2 . pop ( ) ; root1 = root1 -> right ; root2 = root2 -> left ; } else break ; } return " Yes " ; } int main ( ) { Node * root1 = newNode ( 1 ) ; root1 -> left = newNode ( 3 ) ; root1 -> right = newNode ( 2 ) ; root1 -> right -> left = newNode ( 5 ) ; root1 -> right -> right = newNode ( 4 ) ; Node * root2 = newNode ( 1 ) ; root2 -> left = newNode ( 2 ) ; root2 -> right = newNode ( 3 ) ; root2 -> left -> left = newNode ( 4 ) ; root2 -> left -> right = newNode ( 5 ) ; cout << areMirrors ( root1 , root2 ) ; return 0 ; }
Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | C ++ program for the above approach ; Function to find the maximum score possible ; Base Case ; If previously occurred subproblem occurred ; Check if lastpicked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum score from the two possibilities ; Function to print maximum score ; DP array to store results Initialise dp with - 1 ; Function call ; Driver code ; Given arrays ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( int a [ ] , int b [ ] , int n , int index , int lastpicked , vector < vector < int > > dp ) { if ( index == n ) return 0 ; if ( dp [ index ] [ lastpicked + 1 ] != -1 ) return dp [ index ] [ lastpicked + 1 ] ; int option1 = 0 , option2 = 0 ; if ( lastpicked == -1 a [ lastpicked ] != a [ index ] ) { option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index , dp ) ; } option2 = maximumSum ( a , b , n , index + 1 , lastpicked , dp ) ; return dp [ index ] [ lastpicked + 1 ] = max ( option1 , option2 ) ; } void maximumPoints ( int arr [ ] , int brr [ ] , int n ) { int index = 0 , lastPicked = -1 ; vector < vector < int > > dp ( n + 5 , vector < int > ( n + 5 , -1 ) ) ; cout << maximumSum ( arr , brr , n , index , lastPicked , dp ) << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 3 , 3 , 1 } ; int brr [ ] = { -1 , 2 , 10 , 20 , -10 , -9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumPoints ( arr , brr , N ) ; return 0 ; }
Maximum possible sum of a window in an array such that elements of same window in other array are unique | C ++ program to find the maximum possible sum of a window in one array such that elements in same window of other array are unique . ; Function to return maximum sum of window in B [ ] according to given constraints . ; Map is used to store elements and their counts . ; Initialize result ; calculating the maximum possible sum for each subarray containing unique elements . ; Remove all duplicate instances of A [ i ] in current window . ; Add current instance of A [ i ] to map and to current sum . ; Update result if current sum is more . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int returnMaxSum ( int A [ ] , int B [ ] , int n ) { unordered_set < int > mp ; int result = 0 ; int curr_sum = 0 , curr_begin = 0 ; for ( int i = 0 ; i < n ; ++ i ) { while ( mp . find ( A [ i ] ) != mp . end ( ) ) { mp . erase ( A [ curr_begin ] ) ; curr_sum -= B [ curr_begin ] ; curr_begin ++ ; } mp . insert ( A [ i ] ) ; curr_sum += B [ i ] ; result = max ( result , curr_sum ) ; } return result ; } int main ( ) { int A [ ] = { 0 , 1 , 2 , 3 , 0 , 1 , 4 } ; int B [ ] = { 9 , 8 , 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << returnMaxSum ( A , B , n ) ; return 0 ; }
Length of second longest sequence of consecutive 1 s in a binary array | C ++ implementation of the above approach ; Function to find maximum and second maximum length ; Initialise maximum length ; Initialise second maximum length ; Initialise count ; Iterate over the array ; If sequence ends ; Reset count ; Otherwise ; Increase length of current sequence ; Update maximum ; Traverse the given array ; If sequence continues ; Increase length of current sequence ; Update second max ; Reset count when 0 is found ; Print the result ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FindMax ( int arr [ ] , int N ) { int maxi = -1 ; int maxi2 = -1 ; int count = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( arr [ i ] == 0 ) count = 0 ; else { count ++ ; maxi = max ( maxi , count ) ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { count ++ ; if ( count > maxi2 && count < maxi ) { maxi2 = count ; } } if ( arr [ i ] == 0 ) count = 0 ; } maxi = max ( maxi , 0 ) ; maxi2 = max ( maxi2 , 0 ) ; cout << maxi2 ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; FindMax ( arr , N ) ; return 0 ; }
Minimum distance between any special pair in the given array | C ++ program for the above approach ; Function that finds the minimum difference between two vectors ; Find lower bound of the index ; Find two adjacent indices to take difference ; Return the result ; Function to find the minimum distance between index of special pairs ; Stores the index of each element in the array arr [ ] ; Store the indexes ; Get the unique values in list ; Take adjacent difference of same values ; Left index array ; Right index array ; Find the minimum gap between the two adjacent different values ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mindist ( vector < int > & left , vector < int > & right ) { int res = INT_MAX ; for ( int i = 0 ; i < left . size ( ) ; ++ i ) { int num = left [ i ] ; int index = lower_bound ( right . begin ( ) , right . end ( ) , num ) - right . begin ( ) ; if ( index == 0 ) res = min ( res , abs ( num - right [ index ] ) ) ; else if ( index == right . size ( ) ) res = min ( res , abs ( num - right [ index - 1 ] ) ) ; else res = min ( res , min ( abs ( num - right [ index - 1 ] ) , abs ( num - right [ index ] ) ) ) ; } return res ; } int specialPairs ( vector < int > & nums ) { map < int , set < int > > m ; vector < int > vals ; for ( int i = 0 ; i < nums . size ( ) ; ++ i ) { m [ nums [ i ] ] . insert ( i ) ; } for ( auto p : m ) { vals . push_back ( p . first ) ; } int res = INT_MAX ; for ( int i = 0 ; i < vals . size ( ) ; ++ i ) { vector < int > vec ( m [ vals [ i ] ] . begin ( ) , m [ vals [ i ] ] . end ( ) ) ; for ( int i = 1 ; i < vec . size ( ) ; ++ i ) res = min ( res , abs ( vec [ i ] - vec [ i - 1 ] ) ) ; if ( i ) { int a = vals [ i ] ; vector < int > left ( m [ a ] . begin ( ) , m [ a ] . end ( ) ) ; int b = vals [ i - 1 ] ; vector < int > right ( m [ b ] . begin ( ) , m [ b ] . end ( ) ) ; res = min ( res , mindist ( left , right ) ) ; } } return res ; } int main ( ) { vector < int > arr { 0 , -10 , 5 , -5 , 1 } ; cout << specialPairs ( arr ) ; return 0 ; }
Substring of length K having maximum frequency in the given string | C ++ program for the above approach ; Function that generates substring of length K that occurs maximum times ; Store the frequency of substrings ; Deque to maintain substrings window size K ; Update the frequency of the first substring in the Map ; Remove the first character of the previous K length substring ; Traverse the string ; Insert the current character as last character of current substring ; Pop the first character of previous K length substring ; Find the substring that occurs maximum number of times ; Print the substring ; Driver Code ; Given string ; Given K size of substring ; Function Call
#include <bits/stdc++.h> NEW_LINE using ll = long long int ; using namespace std ; void maximumOccurringString ( string s , ll K ) { map < deque < char > , ll > M ; ll i ; deque < char > D ; for ( i = 0 ; i < K ; i ++ ) { D . push_back ( s [ i ] ) ; } M [ D ] ++ ; D . pop_front ( ) ; for ( ll j = i ; j < s . size ( ) ; j ++ ) { D . push_back ( s [ j ] ) ; M [ D ] ++ ; D . pop_front ( ) ; } ll maxi = INT_MIN ; deque < char > ans ; for ( auto it : M ) { if ( it . second > maxi ) { maxi = it . second ; ans = it . first ; } } for ( ll i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] ; } } int main ( ) { string s = " bbbbbaaaaabbabababa " ; ll K = 5 ; maximumOccurringString ( s , K ) ; return 0 ; }