codeparrot/xlcost-text-to-code huggingface dataset:
Count number of substrings having at least K distinct characters | C ++ program for the above approach ; Function to count number of substrings having atleast k distinct characters ; Stores the size of the string ; Initialize a HashMap ; Stores the start and end indices of sliding window ; Stores the required result ; Iterate while the end pointer is less than n ; Include the character at the end of the window ; Increment end pointer by 1 ; Iterate until count of distinct characters becomes less than K ; Remove the character from the beginning of window ; If its frequency is 0 , remove it from the map ; Update the answer ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void atleastkDistinctChars ( string s , int k ) { int n = s . size ( ) ; unordered_map < char , int > mp ; int begin = 0 , end = 0 ; int ans = 0 ; while ( end < n ) { char c = s [ end ] ; mp ++ ; end ++ ; while ( mp . size ( ) >= k ) { char pre = s [ begin ] ; mp [ pre ] -- ; if ( mp [ pre ] == 0 ) { mp . erase ( pre ) ; } ans += s . length ( ) - end + 1 ; begin ++ ; } } cout << ans ; } int main ( ) { string S = " abcca " ; int K = 3 ; atleastkDistinctChars ( S , K ) ; return 0 ; }
Rearrange characters of a string to make it a concatenation of palindromic substrings | C ++ program for the above approach ; Function to check if a string can be modified such that it can be split into palindromic substrings of length >= 2 ; Stores frequencies of characters ; Traverse the string ; Update frequency of each character ; Traverse the frequency array ; Update values of odd and eve ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canSplit ( string & S ) { vector < int > frequency ( 26 , 0 ) ; int cnt_singles = 0 ; int k = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) frequency [ S [ i ] - ' a ' ] ++ ; int odd = 0 , eve = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( frequency [ i ] ) { odd += ( frequency [ i ] & 1 ) ; eve += frequency [ i ] / 2 ; } } if ( eve >= odd ) cout << " Yes " ; else cout << " No " ; } int main ( ) { string S = " aaabbbccc " ; canSplit ( S ) ; return 0 ; }
Maximize count of occurrences of S2 in S1 as a subsequence by concatenating N1 and N2 times respectively | C ++ program for the above approach ; Function to count maximum number of occurrences of s2 as subsequence in s1 by concatenating s1 , n1 times and s2 , n2 times ; Stores number of times s1 is traversed ; Stores number of times s2 is traversed ; Mapping index of s2 to number of times s1 and s2 are traversed ; Stores index of s1 circularly ; Stores index of s2 circularly ; Traverse the string s1 , n1 times ; If current character of both the string are equal ; Update j ; Update i ; If j is length of s2 ; Update j for circular traversal ; Update s2_reps ; If i is length of s1 ; Update i for circular traversal ; Update s1_reps ; If already mapped j to ( s1_reps , s2_reps ) ; Mapping j to ( s1_reps , s2_reps ) ; If s1 already traversed n1 times ; Otherwis , traverse string s1 by multiple of s1_reps and update both s1_reps and s2_reps ; Update s2_reps ; Update s1_reps ; If s1 is traversed less than n1 times ; If current character in both the string are equal ; Update j ; Update i ; If i is length of s1 ; Update i for circular traversal ; Update s1_reps ; If j is length of ss ; Update j for circular traversal ; Update s2_reps ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaxRepetitions ( string s1 , int n1 , string s2 , int n2 ) { int temp1 [ 26 ] = { 0 } , temp2 [ 26 ] = { 0 } ; for ( char i : s1 ) temp1 [ i - ' a ' ] ++ ; for ( char i : s2 ) temp2 [ i - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( temp2 [ i ] > temp1 [ i ] ) return 0 ; } int s1_reps = 0 ; int s2_reps = 0 ; map < int , pair < int , int > > s2_index_to_reps ; s2_index_to_reps [ 0 ] = { 0 , 0 } ; int i = 0 ; int j = 0 ; while ( s1_reps < n1 ) { if ( s1 [ i ] == s2 [ j ] ) j += 1 ; i += 1 ; if ( j == s2 . size ( ) ) { j = 0 ; s2_reps += 1 ; } if ( i == s1 . size ( ) ) { i = 0 ; s1_reps += 1 ; if ( s2_index_to_reps . find ( j ) != s2_index_to_reps . end ( ) ) break ; s2_index_to_reps [ j ] = { s1_reps , s2_reps } ; } } if ( s1_reps == n1 ) return s2_reps / n2 ; int initial_s1_reps = s2_index_to_reps [ j ] . first , initial_s2_reps = s2_index_to_reps [ j ] . second ; int loop_s1_reps = s1_reps - initial_s1_reps ; int loop_s2_reps = s2_reps - initial_s2_reps ; int loops = ( n1 - initial_s1_reps ) ; s2_reps = initial_s2_reps + loops * loop_s2_reps ; s1_reps = initial_s1_reps + loops * loop_s1_reps ; while ( s1_reps < n1 ) { if ( s1 [ i ] == s2 [ j ] ) j += 1 ; i += 1 ; if ( i == s1 . size ( ) ) { i = 0 ; s1_reps += 1 ; } if ( j == s2 . size ( ) ) { j = 0 ; s2_reps += 1 ; } } return s2_reps / n2 ; } int main ( ) { string s1 = " acb " ; int n1 = 4 ; string s2 = " ab " ; int n2 = 2 ; cout << ( getMaxRepetitions ( s1 , n1 , s2 , n2 ) ) ; return 0 ; }
Shortest string possible after removal of all pairs of similar adjacent characters | C ++ program for the above approach ; Function to delete pair of adjacent characters which are equal ; Base Case ; Stores the update string ; Traverse the string s ; If two unequal pair of adjacent characters are found ; If two equal pair of adjacent characters are found ; Append the remaining string after removing the pair ; Return the final String ; Function to find the shortest string after repeatedly removing pairs of adjacent characters which are equal ; Stores the resultant String ; Keeps track of previously iterated string ; Update the result after deleting adjacent pair of characters which are similar ; Termination Conditions ; Update pre variable with the value of result ; Case for " Empty ▁ String " ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeAdjacent ( string s ) { if ( s . length ( ) == 1 ) return s ; string sb = " " ; for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) { char c = s [ i ] ; char d = s [ i + 1 ] ; if ( c != d ) { sb = sb + c ; if ( i == s . length ( ) - 2 ) { sb = sb + d ; } } else { for ( int j = i + 2 ; j < s . length ( ) ; j ++ ) sb = sb + s [ j ] ; return sb ; } } return sb ; } void reduceString ( string s ) { string result = " " ; string pre = s ; while ( true ) { result = removeAdjacent ( pre ) ; if ( result == pre ) break ; pre = result ; } if ( result . length ( ) != 0 ) cout << ( result ) << endl ; else cout << " Empty ▁ String " << endl ; } int main ( ) { string S = " aaabccddd " ; reduceString ( S ) ; return 0 ; }
Minimum removals required such that a string can be rearranged to form a palindrome | C ++ program for the above approach ; Function to find the number of deletions required such that characters of the string can be rearranged to form a palindrome ; Stores frequency of characters ; Store the frequency of each character in frequency array ; Count number of characters with odd frequency ; If count is 1 or 0 , return 0 ; Otherwise , return count - 1 ; Driver Code ; Function call to find minimum number of deletions required
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDeletions ( string str ) { int fre [ 26 ] ; memset ( fre , 0 , sizeof ( fre ) ) ; int n = str . size ( ) ; cout << n ; for ( int i = 0 ; i < n ; i ++ ) { fre [ str [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < n ; i ++ ) { cout << fre [ i ] ; } int count = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % 2 ) { count += 1 ; } } if ( count == 0 count == 1 ) { return 0 ; } else { return count - 1 ; } } int main ( ) { string str = " ababbccca " ; cout << minDeletions ( str ) << endl ; }
Program to print an array in Pendulum Arrangement | C ++ program for pendulum arrangement of numbers ; Prints pendulum arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; Driver function ; input Array ; calculating the length of array A ; calling pendulum function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pendulumArrangement ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int op [ n ] ; int mid = ( n - 1 ) / 2 ; int j = 1 , i = 1 ; op [ mid ] = arr [ 0 ] ; for ( i = 1 ; i <= mid ; i ++ ) { op [ mid + i ] = arr [ j ++ ] ; op [ mid - i ] = arr [ j ++ ] ; } if ( n % 2 == 0 ) op [ mid + i ] = arr [ j ] ; cout << " Pendulum ▁ arrangement : " << endl ; for ( i = 0 ; i < n ; i ++ ) cout << op [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 14 , 6 , 19 , 21 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pendulumArrangement ( arr , n ) ; return 0 ; }
Calculate score of parentheses from a given string | C ++ program for the above approach ; Function to calculate the score of the parentheses using stack ; To keep track of the score ; Initially , push 0 to stack ; Traverse the string s ; If ' ( ' is encountered , then push 0 to stack ; Otherwise ; Balance the last ' ( ' , and store the score of inner parentheses ; If tmp is not zero , it means inner parentheses exists ; Otherwise , it means no inner parentheses exists ; Pass the score of this level to parent parentheses ; Print the score ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void scoreOfParentheses ( string s ) { stack < int > stack ; stack . push ( 0 ) ; for ( char c : s ) { if ( c == ' ( ' ) stack . push ( 0 ) ; else { int tmp = stack . top ( ) ; stack . pop ( ) ; int val = 0 ; if ( tmp > 0 ) val = tmp * 2 ; else val = 1 ; stack . top ( ) += val ; } } cout << stack . top ( ) ; } int main ( ) { string S = " ( ( ) ( ( ) ) ) " ; scoreOfParentheses ( S ) ; return 0 ; }
Count unique substrings of a string S present in a wraparound string | C ++ program for the above approach ; Function to find the count of non - empty substrings of p present in s ; Stores the required answer ; Stores the length of substring present in p ; Stores the current length of substring that is present in string s starting from each character of p ; Iterate over the characters of the string ; Check if the current character can be added with previous substring to form the required substring ; Increment current length ; To avoid repetition ; Update arr [ cur ] ; Print the answer ; Driver Code ; Function call to find the count of non - empty substrings of p present in s
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSubstringInWraproundString ( string p ) { int ans = 0 ; int curLen = 0 ; int arr [ 26 ] = { 0 } ; for ( int i = 0 ; i < ( int ) p . length ( ) ; i ++ ) { int curr = p [ i ] - ' a ' ; if ( i > 0 && ( p [ i - 1 ] != ( ( curr + 26 - 1 ) % 26 + ' a ' ) ) ) { curLen = 0 ; } curLen ++ ; if ( curLen > arr [ curr ] ) { ans += ( curLen - arr [ curr ] ) ; arr [ curr ] = curLen ; } } cout << ans ; } int main ( ) { string p = " zab " ; findSubstringInWraproundString ( p ) ; return 0 ; }
Minimize sum of given array by removing all occurrences of a single digit | C ++ program for super ugly number ; Function to remove each digit from the given integer ; Convert into string ; Stores final string ; Traverse the string ; Append it to the final string ; Return integer value ; Function to find the minimum sum by removing occurences of each digit ; Iterate in range [ 0 , 9 ] ; Traverse the array ; Update the minimum sum ; Print the minimized sum ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int remove ( int N , int digit ) { string strN = to_string ( N ) ; string ans = " " ; for ( char i : strN ) { if ( ( i - '0' ) == digit ) { continue ; } ans += i ; } return stoi ( ans ) ; } void getMin ( vector < int > arr ) { int minSum = INT_MAX ; for ( int i = 0 ; i < 10 ; i ++ ) { int curSum = 0 ; for ( int num : arr ) curSum += remove ( num , i ) ; minSum = min ( minSum , curSum ) ; } cout << minSum ; } int main ( ) { vector < int > arr = { 34 , 23 , 85 , 93 } ; getMin ( arr ) ; return 0 ; }
Split a string into maximum number of unique substrings | CPP program for the above approach ; Utility function to find maximum count of unique substrings by splitting the string ; Stores maximum count of unique substring by splitting the string into substrings ; Iterate over the characters of the string ; Stores prefix substring ; Check if the current substring already exists ; Insert tmp into set ; Recursively call for remaining characters of string ; Remove from the set ; Return answer ; Function to find the maximum count of unique substrings by splitting a string into maximum number of unique substrings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxUnique ( string S , set < string > st ) { int mx = 0 ; for ( int i = 1 ; i <= S . length ( ) ; i ++ ) { string tmp = S . substr ( 0 , i ) ; if ( st . find ( tmp ) == st . end ( ) ) { st . insert ( tmp ) ; mx = max ( mx , maxUnique ( S . substr ( i ) , st ) + 1 ) ; st . erase ( tmp ) ; } } return mx ; } int maxUniqueSplit ( string S ) { set < string > st ; return maxUnique ( S , st ) ; } int main ( ) { string S = " ababccc " ; int res = maxUniqueSplit ( S ) ; cout << res ; }
Minimize length of a given string by removing subsequences forming valid parenthesis | C ++ program of the above approach ; Function to remove all possible valid bracket subsequences ; Stores indexes of ' ( ' in valid subsequences ; Stores indexes of ' { ' in valid subsequences ; Stores indexes of ' [ ' in valid subsequences ; vis [ i ] : Check if character at i - th index is removed or not ; Mark vis [ i ] as not removed ; Iterate over the characters of string ; If current character is ' ( ' ; If current character is ' { ' ; If current character is ' [ ' ; If current character is ' ) ' and top element of A is ' ( ' ; Mark the top element of A as removed ; Mark current chacracter as removed ; If current character is ' } ' and top element of B is ' { ' ; Mark the top element of B as removed ; Mark current chacracter as removed ; If current character is ' ] ' and top element of B is ' [ ' ; Mark the top element of C as removed ; Mark current chacracter as removed ; Print the remaining characters which is not removed from S ; Driver Code ; Given string ; Size of the string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeValidBracketSequences ( string & str , int N ) { stack < int > A ; stack < int > B ; stack < int > C ; bool vis [ N ] ; memset ( vis , true , sizeof ( vis ) ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ( ' ) { A . push ( i ) ; } else if ( str [ i ] == ' { ' ) { B . push ( i ) ; } else if ( str [ i ] == ' [ ' ) { C . push ( i ) ; } else if ( str [ i ] == ' ) ' && ! A . empty ( ) ) { vis [ A . top ( ) ] = false ; A . pop ( ) ; vis [ i ] = false ; } else if ( str [ i ] == ' } ' && ! B . empty ( ) ) { vis [ B . top ( ) ] = false ; B . pop ( ) ; vis [ i ] = false ; } else if ( str [ i ] == ' ] ' && ! C . empty ( ) ) { vis [ C . top ( ) ] = false ; C . pop ( ) ; vis [ i ] = false ; } } for ( int i = 0 ; i < N ; ++ i ) { if ( vis [ i ] ) cout << str [ i ] ; } } int main ( ) { string str = " ( ( ) { ( ) ( { } ) " ; int N = str . length ( ) ; removeValidBracketSequences ( str , N ) ; return 0 ; }
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | C ++ program for the above approach ; Function to update frequency of a character in Fenwick tree ; Update frequency of ( idx + ' a ' ) ; Update i ; Function to find the frequency of a character ( idx + ' a ' ) in range [ 1 , i ] ; Stores frequency of character , ( idx + ' a ' ) in range [ 1 , i ] ; Update ans ; Update i ; Function to find difference between maximum and minimum frequency of a character in given range ; BIT [ i ] [ j ] : Stores frequency of ( i + ' a ' ) If j is a power of 2 , then it stores the frequency ( i + ' a ' ) of from [ 1 , j ] ; Stores length of string ; Iterate over the characters of the string ; Update the frequency of s [ i ] in fenwick tree ; Stores count of queries ; Iterate over all the queries ; Stores maximum frequency of a character in range [ l , r ] ; Stores minimum frequency of a character in range [ l , r ] ; Iterate over all possible characters ; Stores frequency of ( j + ' a ' ) in range [ 1 , r ] ; Stores frequency of ( j + ' a ' ) in range [ 1 , l - 1 ] ; Update mx ; If a character ( i + ' a ' ) present in range [ l , r ] ; Update mn ; Print the difference between max and min freq ; Driver Code ; Given string ; Given queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void update ( int BIT [ 26 ] [ 10005 ] , int idx , int i , int val ) { while ( i < 10005 ) { BIT [ idx ] [ i ] += val ; i = i + ( i & ( - i ) ) ; } } int query ( int BIT [ 26 ] [ 10005 ] , int idx , int i ) { int ans = 0 ; while ( i > 0 ) { ans += BIT [ idx ] [ i ] ; i = i - ( i & ( - i ) ) ; } return ans ; } void maxDiffFreq ( string s , vector < pair < int , int > > queries ) { int BIT [ 26 ] [ 10005 ] ; int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { update ( BIT , s [ i ] - ' a ' , i + 1 , 1 ) ; } int Q = queries . size ( ) ; for ( int i = 0 ; i < Q ; ++ i ) { int mx = 0 ; int mn = INT_MAX ; int l = queries [ i ] . first ; int r = queries [ i ] . second ; for ( int j = 0 ; j < 26 ; j ++ ) { int p = query ( BIT , j , r ) ; int q = query ( BIT , j , l - 1 ) ; mx = max ( mx , p - q ) ; if ( p > 0 ) { mn = min ( mn , p - q ) ; } } cout << mx - mn << endl ; } } int main ( ) { string S = " abaabac " ; vector < pair < int , int > > queries = { { 2 , 6 } , { 1 , 7 } } ; maxDiffFreq ( S , queries ) ; }
Make all strings from a given array equal by replacing minimum number of characters | C ++ program to implement the above approach ; Function to find the minimum count of operations required to make all strings equal by replacing characters of strings ; Stores minimum count of operations required to make all strings equal ; Stores length of the string ; hash [ i ] [ j ] : Stores frequency of character i present at j - th index of all strings ; Traverse the array arr [ ] ; Iterate over characters of current string ; Update frequency of arr [ i ] [ j ] ; Traverse hash [ ] [ ] array ; Stores sum of i - th column ; Stores the largest element of i - th column ; Iterate over all possible characters ; Update Sum ; Update Max ; Update cntMinOP ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( string arr [ ] , int N ) { int cntMinOP = 0 ; int M = arr [ 0 ] . length ( ) ; int hash [ 256 ] [ M ] ; memset ( hash , 0 , sizeof ( hash ) ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] [ j ] ] [ j ] ++ ; } } for ( int i = 0 ; i < M ; i ++ ) { int Sum = 0 ; int Max = 0 ; for ( int j = 0 ; j < 256 ; j ++ ) { Sum += hash [ j ] [ i ] ; Max = max ( Max , hash [ j ] [ i ] ) ; } cntMinOP += ( Sum - Max ) ; } return cntMinOP ; } int main ( ) { string arr [ ] = { " abcd " , " bcde " , " cdef " } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperation ( arr , N ) << " STRNEWLINE " ; }
Maximize count of distinct strings generated by replacing similar adjacent digits having sum K with K | C ++ program for the above approach ; Function to find the desired number of strings ; Store the count of strings ; Store the length of the string ; Initialize variable to indicate the start of the substring ; Store the starting index of the substring ; Traverse the string ; If sum of adjacent characters is K mark the starting index and set flag to 1 ; If sum of adjacent characters is not K and the flag variable is set , end the substring here ; Set flag to 0 denoting the end of substring ; Check if the length of the substring formed is odd ; Update the answer ; If flag is set and end of string is reached , mark the end of substring ; Update the answer ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countStrings ( string s , int k ) { int ans = 1 ; int len = s . size ( ) ; int flag = 0 ; int start_ind ; for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( s [ i ] - '0' + s [ i + 1 ] - '0' == k && flag == 0 ) { flag = 1 ; start_ind = i ; } if ( flag == 1 && s [ i ] - '0' + s [ i + 1 ] - '0' != k ) { flag = 0 ; if ( ( i - start_ind + 1 ) % 2 != 0 ) ans *= ( i - start_ind + 1 - 1 ) / 2 + 1 ; } } if ( flag == 1 && ( len - start_ind ) % 2 != 0 ) ans *= ( len - start_ind ) / 2 + 1 ; cout << ans ; } int main ( ) { string S = "313" ; int K = 4 ; countStrings ( S , K ) ; }
Smallest number whose product with N has sum of digits equal to that of N | C ++ program for the above approach ; Function to find the minimum integer having sum of digits of a number multiplied by n equal to sum of digits of n ; Initialize answer ; Find sum of digits of N ; Multiply N with x ; Sum of digits of the new number ; If condition satisfies ; Print answer ; Driver Code ; Function call
#include <iostream> NEW_LINE using namespace std ; void find_num ( string n ) { int ans = 0 ; int sumOfDigitsN = 0 ; for ( int c = 0 ; c < n . length ( ) ; c ++ ) { sumOfDigitsN += n - '0' ; } int x = 11 ; while ( true ) { int newNum = x * stoi ( n ) ; int tempSumDigits = 0 ; string temp = to_string ( newNum ) ; for ( int c = 0 ; c < temp . length ( ) ; c ++ ) { tempSumDigits += temp - '0' ; } if ( tempSumDigits == sumOfDigitsN ) { ans = x ; break ; } x ++ ; } cout << ans << endl ; } int main ( ) { string N = "3029" ; find_num ( N ) ; return 0 ; }
Print all distinct strings from a given array | C ++ program to implement the above approach ; Function to find the distinct strings from the given array ; Stores distinct strings from the given array ; Traverse the array ; If current string not present into the set ; Insert current string into the set ; Traverse the set DistString ; Print distinct string ; Driver Code ; Stores length of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findDisStr ( vector < string > & arr , int N ) { unordered_set < string > DistString ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! DistString . count ( arr [ i ] ) ) { DistString . insert ( arr [ i ] ) ; } } for ( auto String : DistString ) { cout << String << " ▁ " ; } } int main ( ) { vector < string > arr = { " Geeks " , " For " , " Geeks " , " Code " , " Coder " } ; int N = arr . size ( ) ; findDisStr ( arr , N ) ; return 0 ; }
Reverse substrings of given string according to specified array indices | C ++ program for the above approach ; Function to perform the reversal operation on the given string ; Size of string ; Stores the count of indices ; Count the positions where reversals will begin ; Store the count of reversals beginning at position i ; Check if the count [ i ] is odd the swap the character ; Return the updated string ; Driver Code ; Given string str ; Given array of reversing index ; Function Call
#include <iostream> NEW_LINE using namespace std ; string modifyString ( int A [ ] , string str , int K ) { int N = str . size ( ) ; int count [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < K ; i ++ ) { count [ A [ i ] ] ++ ; } for ( int i = 1 ; i <= N / 2 ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; if ( count [ i ] & 1 ) { swap ( str [ i - 1 ] , str [ N - i ] ) ; } } return str ; } int main ( ) { string str = " abcdef " ; int arr [ ] = { 1 , 2 , 3 } ; int K = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << modifyString ( arr , str , K ) ; return 0 ; }