codeparrot/xlcost-text-to-code huggingface dataset:
Ford | C ++ program for implementation of Ford Fulkerson algorithm ; Number of vertices in given graph ; Returns true if there is a path from source ' s ' to sink ' t ' in residual graph . Also fills parent [ ] to store the path ; Create a visited array and mark all vertices as not visited ; Create a queue , enqueue source vertex and mark source vertex as visited ; Standard BFS Loop ; If we find a connection to the sink node , then there is no point in BFS anymore We just have to set its parent and can return true ; We didn 't reach sink in BFS starting from source, so  return false ; Returns the maximum flow from s to t in the given graph ; Create a residual graph and fill the residual graph with given capacities in the original graph as residual capacities in residual graph ; This array is filled by BFS and to store path ; There is no flow initially ; Augment the flow while tere is path from source to sink ; Find minimum residual capacity of the edges along the path filled by BFS . Or we can say find the maximum flow through the path found . ; update residual capacities of the edges and reverse edges along the path ; Add path flow to overall flow ; Return the overall flow ; Driver program to test above functions ; Let us create a graph shown in the above example
#include <iostream> NEW_LINE #include <limits.h> NEW_LINE #include <queue> NEW_LINE #include <string.h> NEW_LINE using namespace std ; #define V  6 NEW_LINE bool bfs ( int rGraph [ V ] [ V ] , int s , int t , int parent [ ] ) { bool visited [ V ] ; memset ( visited , 0 , sizeof ( visited ) ) ; queue < int > q ; q . push ( s ) ; visited [ s ] = true ; parent [ s ] = -1 ; while ( ! q . empty ( ) ) { int u = q . front ( ) ; q . pop ( ) ; for ( int v = 0 ; v < V ; v ++ ) { if ( visited [ v ] == false && rGraph [ u ] [ v ] > 0 ) { if ( v == t ) { parent [ v ] = u ; return true ; } q . push ( v ) ; parent [ v ] = u ; visited [ v ] = true ; } } } return false ; } int fordFulkerson ( int graph [ V ] [ V ] , int s , int t ) { int u , v ; int rGraph [ V ] [ V ] ; for ( u = 0 ; u < V ; u ++ ) for ( v = 0 ; v < V ; v ++ ) rGraph [ u ] [ v ] = graph [ u ] [ v ] ; int parent [ V ] ; int max_flow = 0 ; while ( bfs ( rGraph , s , t , parent ) ) { int path_flow = INT_MAX ; for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; path_flow = min ( path_flow , rGraph [ u ] [ v ] ) ; } for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; rGraph [ u ] [ v ] -= path_flow ; rGraph [ v ] [ u ] += path_flow ; } max_flow += path_flow ; } return max_flow ; } int main ( ) { int graph [ V ] [ V ] = { { 0 , 16 , 13 , 0 , 0 , 0 } , { 0 , 0 , 10 , 12 , 0 , 0 } , { 0 , 4 , 0 , 0 , 14 , 0 } , { 0 , 0 , 9 , 0 , 0 , 20 } , { 0 , 0 , 0 , 7 , 0 , 4 } , { 0 , 0 , 0 , 0 , 0 , 0 } } ; cout << " The ▁ maximum ▁ possible ▁ flow ▁ is ▁ " << fordFulkerson ( graph , 0 , 5 ) ; return 0 ; }
Perfect Binary Tree Specific Level Order Traversal | Set 2 | C ++ program for special order traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Function body ; Create a queue and enqueue left and right children of root ; We process two nodes at a time , so we need two variables to store two front items of queue ; traversal loop ; Pop two items from queue ; Push first and second node 's chilren  in reverse order ; If first and second have grandchildren , enqueue them in specific order ; Given a perfect binary tree , print its nodes in specific level order ; create a stack and push root ; Push level 1 and level 2 nodes in stack ; Since it is perfect Binary Tree , right is not checked ; Do anything more if there are nodes at next level in given perfect Binary Tree ; Finally pop all Nodes from stack and prints them . ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> right = node -> left = NULL ; return node ; } void printSpecificLevelOrderUtil ( Node * root , stack < Node * > & s ) { if ( root == NULL ) return ; queue < Node * > q ; q . push ( root -> left ) ; q . push ( root -> right ) ; Node * first = NULL , * second = NULL ; while ( ! q . empty ( ) ) { first = q . front ( ) ; q . pop ( ) ; second = q . front ( ) ; q . pop ( ) ; s . push ( second -> left ) ; s . push ( first -> right ) ; s . push ( second -> right ) ; s . push ( first -> left ) ; if ( first -> left -> left != NULL ) { q . push ( first -> right ) ; q . push ( second -> left ) ; q . push ( first -> left ) ; q . push ( second -> right ) ; } } } void printSpecificLevelOrder ( Node * root ) { stack < Node * > s ; s . push ( root ) ; if ( root -> left != NULL ) { s . push ( root -> right ) ; s . push ( root -> left ) ; } if ( root -> left -> left != NULL ) printSpecificLevelOrderUtil ( root , s ) ; while ( ! s . empty ( ) ) { cout << s . top ( ) -> data << " ▁ " ; s . pop ( ) ; } } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; cout << " Specific ▁ Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ " " tree ▁ is ▁ STRNEWLINE " ; printSpecificLevelOrder ( root ) ; return 0 ; }
Prim 's algorithm using priority_queue in STL | If v is not in MST and weight of ( u , v ) is smaller than current key of v ; Updating key of v
if ( inMST [ v ] == false && key [ v ] > weight ) { key [ v ] = weight ; pq . push ( make_pair ( key [ v ] , v ) ) ; parent [ v ] = u ; }
Graph implementation using STL for competitive programming | Set 2 ( Weighted graph ) | C ++ program to represent undirected and weighted graph using STL . The program basically prints adjacency list representation of graph ; To add an edge ; Print adjacency list representaion ot graph ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addEdge ( vector < pair < int , int > > adj [ ] , int u , int v , int wt ) { adj [ u ] . push_back ( make_pair ( v , wt ) ) ; adj [ v ] . push_back ( make_pair ( u , wt ) ) ; } void printGraph ( vector < pair < int , int > > adj [ ] , int V ) { int v , w ; for ( int u = 0 ; u < V ; u ++ ) { cout << " Node ▁ " << u << " ▁ makes ▁ an ▁ edge ▁ with ▁ STRNEWLINE " ; for ( auto it = adj [ u ] . begin ( ) ; it != adj [ u ] . end ( ) ; it ++ ) { v = it -> first ; w = it -> second ; cout << " TABSYMBOL Node ▁ " << v << " ▁ with ▁ edge ▁ weight ▁ = " << w << " STRNEWLINE " ; } cout << " STRNEWLINE " ; } } int main ( ) { int V = 5 ; vector < pair < int , int > > adj [ V ] ; addEdge ( adj , 0 , 1 , 10 ) ; addEdge ( adj , 0 , 4 , 20 ) ; addEdge ( adj , 1 , 2 , 30 ) ; addEdge ( adj , 1 , 3 , 40 ) ; addEdge ( adj , 1 , 4 , 50 ) ; addEdge ( adj , 2 , 3 , 60 ) ; addEdge ( adj , 3 , 4 , 70 ) ; printGraph ( adj , V ) ; return 0 ; }
K Centers Problem | Set 1 ( Greedy Approximate Algorithm ) | C ++ program for the above approach ; index of city having the maximum distance to it 's  closest center ; updating the distance of the cities to their closest centers ; updating the index of the city with the maximum distance to it 's closest center ; Printing the maximum distance of a city to a center that is our answer ; Printing the cities that were chosen to be made centers ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxindex ( int * dist , int n ) { int mi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dist [ i ] > dist [ mi ] ) mi = i ; } return mi ; } void selectKcities ( int n , int weights [ 4 ] [ 4 ] , int k ) { int * dist = new int [ n ] ; vector < int > centers ; for ( int i = 0 ; i < n ; i ++ ) { dist [ i ] = INT_MAX ; } int max = 0 ; for ( int i = 0 ; i < k ; i ++ ) { centers . push_back ( max ) ; for ( int j = 0 ; j < n ; j ++ ) { dist [ j ] = min ( dist [ j ] , weights [ max ] [ j ] ) ; } max = maxindex ( dist , n ) ; } cout << endl << dist [ max ] << endl ; for ( int i = 0 ; i < centers . size ( ) ; i ++ ) { cout << centers [ i ] << " ▁ " ; } cout << endl ; } int main ( ) { int n = 4 ; int weights [ 4 ] [ 4 ] = { { 0 , 4 , 8 , 5 } , { 4 , 0 , 10 , 7 } , { 8 , 10 , 0 , 9 } , { 5 , 7 , 9 , 0 } } ; int k = 2 ; selectKcities ( n , weights , k ) ; }
Hierholzer 's Algorithm for directed graph | A C ++ program to print Eulerian circuit in given directed graph using Hierholzer algorithm ; adj represents the adjacency list of the directed graph edge_count represents the number of edges emerging from a vertex ; find the count of edges to keep track of unused edges ; Maintain a stack to keep vertices ; vector to store final circuit ; start from any vertex ; Current vertex ; If there 's remaining edge ; Push the vertex ; Find the next vertex using an edge ; and remove that edge ; Move to next vertex ; back - track to find remaining circuit ; Back - tracking ; we 've got the circuit, now print it in reverse ; Driver program to check the above function ; Input Graph 1 ; Build the edges ; Input Graph 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCircuit ( vector < vector < int > > adj ) { unordered_map < int , int > edge_count ; for ( int i = 0 ; i < adj . size ( ) ; i ++ ) { edge_count [ i ] = adj [ i ] . size ( ) ; } if ( ! adj . size ( ) ) return ; stack < int > curr_path ; vector < int > circuit ; curr_path . push ( 0 ) ; int curr_v = 0 ; while ( ! curr_path . empty ( ) ) { if ( edge_count [ curr_v ] ) { curr_path . push ( curr_v ) ; int next_v = adj [ curr_v ] . back ( ) ; edge_count [ curr_v ] -- ; adj [ curr_v ] . pop_back ( ) ; curr_v = next_v ; } else { circuit . push_back ( curr_v ) ; curr_v = curr_path . top ( ) ; curr_path . pop ( ) ; } } for ( int i = circuit . size ( ) - 1 ; i >= 0 ; i -- ) { cout << circuit [ i ] ; if ( i ) cout << " ▁ - > ▁ " ; } } int main ( ) { vector < vector < int > > adj1 , adj2 ; adj1 . resize ( 3 ) ; adj1 [ 0 ] . push_back ( 1 ) ; adj1 [ 1 ] . push_back ( 2 ) ; adj1 [ 2 ] . push_back ( 0 ) ; printCircuit ( adj1 ) ; cout << endl ; adj2 . resize ( 7 ) ; adj2 [ 0 ] . push_back ( 1 ) ; adj2 [ 0 ] . push_back ( 6 ) ; adj2 [ 1 ] . push_back ( 2 ) ; adj2 [ 2 ] . push_back ( 0 ) ; adj2 [ 2 ] . push_back ( 3 ) ; adj2 [ 3 ] . push_back ( 4 ) ; adj2 [ 4 ] . push_back ( 2 ) ; adj2 [ 4 ] . push_back ( 5 ) ; adj2 [ 5 ] . push_back ( 0 ) ; adj2 [ 6 ] . push_back ( 4 ) ; printCircuit ( adj2 ) ; return 0 ; }
Perfect Binary Tree Specific Level Order Traversal | Set 2 | C ++ program for special order traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Given a perfect binary tree , print its nodes in specific level order ; for level order traversal ; stack to print reverse ; vector to store the level ; considering size of the level ; push data of the node of a particular level to vector ; push vector containing a level in stack ; print the stack ; Finally pop all Nodes from stack and prints them . ; finally print root ; ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * left ; Node * right ; Node ( int value ) { data = value ; left = NULL ; right = NULL ; } } ; void specific_level_order_traversal ( Node * root ) { queue < Node * > q ; stack < vector < int > > s ; q . push ( root ) ; int sz ; while ( ! q . empty ( ) ) { vector < int > v ; sz = q . size ( ) ; for ( int i = 0 ; i < sz ; ++ i ) { Node * temp = q . front ( ) ; q . pop ( ) ; v . push_back ( temp -> data ) ; if ( temp -> left != NULL ) q . push ( temp -> left ) ; if ( temp -> right != NULL ) q . push ( temp -> right ) ; } s . push ( v ) ; } while ( ! s . empty ( ) ) { vector < int > v = s . top ( ) ; s . pop ( ) ; for ( int i = 0 , j = v . size ( ) - 1 ; i < j ; ++ i ) { cout << v [ i ] << " ▁ " << v [ j ] << " ▁ " ; j -- ; } } cout << root -> data ; } int main ( ) { Node * root = new Node ( 1 ) ; root -> left = new Node ( 2 ) ; root -> right = new Node ( 3 ) ; cout << " Specific ▁ Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ " " tree ▁ is ▁ STRNEWLINE " ; specific_level_order_traversal ( root ) ; return 0 ; }
Number of Triangles in an Undirected Graph | A C ++ program for finding number of triangles in an Undirected Graph . The program is for adjacency matrix representation of the graph ; Number of vertices in the graph ; Utility function for matrix multiplication ; Utility function to calculate trace of a matrix ( sum ofdiagnonal elements ) ; Utility function for calculating number of triangles in graph ; To Store graph ^ 2 ; To Store graph ^ 3 ; Initialising aux matrices with 0 ; aux2 is graph ^ 2 now printMatrix ( aux2 ) ; ; after this multiplication aux3 is graph ^ 3 printMatrix ( aux3 ) ; ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define V  4 NEW_LINE void multiply ( int A [ ] [ V ] , int B [ ] [ V ] , int C [ ] [ V ] ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { C [ i ] [ j ] = 0 ; for ( int k = 0 ; k < V ; k ++ ) C [ i ] [ j ] += A [ i ] [ k ] * B [ k ] [ j ] ; } } } int getTrace ( int graph [ ] [ V ] ) { int trace = 0 ; for ( int i = 0 ; i < V ; i ++ ) trace += graph [ i ] [ i ] ; return trace ; } int triangleInGraph ( int graph [ ] [ V ] ) { int aux2 [ V ] [ V ] ; int aux3 [ V ] [ V ] ; for ( int i = 0 ; i < V ; ++ i ) for ( int j = 0 ; j < V ; ++ j ) aux2 [ i ] [ j ] = aux3 [ i ] [ j ] = 0 ; multiply ( graph , graph , aux2 ) ; multiply ( graph , aux2 , aux3 ) ; int trace = getTrace ( aux3 ) ; return trace / 6 ; } int main ( ) { int graph [ V ] [ V ] = { { 0 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 0 } } ; printf ( " Total ▁ number ▁ of ▁ Triangle ▁ in ▁ Graph ▁ : ▁ % d STRNEWLINE " , triangleInGraph ( graph ) ) ; return 0 ; }
Count subsequences in first string which are anagrams of the second string | C ++ implementation to count subsequences in first string which are anagrams of the second string ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; function to count subsequences in first string which are anagrams of the second string ; hash tables to store frequencies of each character ; store frequency of each character of ' str1' ; store frequency of each character of ' str2' ; to store the total count of subsequences ; if character ( i + ' a ' ) exists in ' str2' ; if this character ' s ▁ frequency ▁ ▁ in ▁ ' str2 ' ▁ in ▁ less ▁ than ▁ or ▁ ▁ equal ▁ to ▁ its ▁ frequency ▁ in ▁ ▁ ' str1 ' ▁ then ▁ accumulate ▁ its ▁ ▁ contribution ▁ to ▁ the ▁ count ▁ ▁ of ▁ subsequences . ▁ If ▁ its ▁ ▁ frequency ▁ in ▁ ' str1 ' ▁ is ▁ ' n ' ▁ ▁ and ▁ in ▁ ' str2 ' ▁ is ▁ ' r ', then  its contribution will be nCr,  where C is the binomial  coefficient. ; else return 0 as there could be no subsequence which is an anagram of ' str2' ; required count of subsequences ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  26 NEW_LINE int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } int countSubsequences ( string str1 , string str2 ) { int freq1 [ SIZE ] , freq2 [ SIZE ] ; int n1 = str1 . size ( ) ; int n2 = str2 . size ( ) ; memset ( freq1 , 0 , sizeof ( freq1 ) ) ; memset ( freq2 , 0 , sizeof ( freq2 ) ) ; for ( int i = 0 ; i < n1 ; i ++ ) freq1 [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] ++ ; int count = 1 ; for ( int i = 0 ; i < SIZE ; i ++ ) if ( freq2 [ i ] != 0 ) { if ( freq2 [ i ] <= freq1 [ i ] ) count = count * binomialCoeff ( freq1 [ i ] , freq2 [ i ] ) ; else return 0 ; } return count ; } int main ( ) { string str1 = " abacd " ; string str2 = " abc " ; cout << " Count ▁ = ▁ " << countSubsequences ( str1 , str2 ) ; return 0 ; }
Subsequence queries after removing substrings | CPP program for answering queries to check whether a string subsequence or not after removing a substring . ; arrays to store results of preprocessing ; function to preprocess the strings ; initialize it as 0. ; store subsequence count in forward direction ; store number of matches till now ; store subsequence count in backward direction ; store number of matches till now ; function that gives the output ; length of remaining string A is less than B 's length ; driver function ; two queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; int * fwd , * bwd ; void preProcess ( string a , string b ) { int n = a . size ( ) ; fwd = new int [ n ] ( ) ; bwd = new int [ n ] ( ) ; int j = 0 ; for ( int i = 1 ; i <= a . size ( ) ; i ++ ) { if ( j < b . size ( ) && a [ i - 1 ] == b [ j ] ) j ++ ; fwd [ i ] = j ; } j = 0 ; for ( int i = a . size ( ) ; i >= 1 ; i -- ) { if ( j < b . size ( ) && a [ i - 1 ] == b [ b . size ( ) - j - 1 ] ) j ++ ; bwd [ i ] = j ; } } void query ( string a , string b , int x , int y ) { if ( ( x - 1 + a . size ( ) - y ) < b . size ( ) ) { cout << " No STRNEWLINE " ; return ; } if ( fwd [ x - 1 ] + bwd [ y + 1 ] >= b . size ( ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; } int main ( ) { string a = " abcabcxy " , b = " acy " ; preProcess ( a , b ) ; int x = 2 , y = 5 ; query ( a , b , x , y ) ; x = 3 , y = 6 ; query ( a , b , x , y ) ; return 0 ; }
Count subsequence of length three in a given string | C ++ program to find number of occurrences of a subsequence of length 3 ; Function to find number of occurrences of a subsequence of length three in a string ; variable to store no of occurrences ; loop to find first character ; loop to find 2 nd character ; loop to find 3 rd character ; increment count if subsequence is found ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findOccurrences ( string str , string substr ) { int counter = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == substr [ 0 ] ) { for ( int j = i + 1 ; j < str . length ( ) ; j ++ ) { if ( str [ j ] == substr [ 1 ] ) { for ( int k = j + 1 ; k < str . length ( ) ; k ++ ) { if ( str [ k ] == substr [ 2 ] ) counter ++ ; } } } } } return counter ; } int main ( ) { string str = " GFGFGYSYIOIWIN " ; string substr = " GFG " ; cout << findOccurrences ( str , substr ) ; return 0 ; }
Count subsequence of length three in a given string | C ++ program to find number of occurrences of a subsequence of length 3 ; Function to find number of occurrences of a subsequence of length three in a string ; calculate length of string ; auxiliary array to store occurrences of first character ; auxiliary array to store occurrences of third character ; calculate occurrences of first character upto ith index from left ; calculate occurrences of third character upto ith index from right ; variable to store total number of occurrences ; loop to find the occurrences of middle element ; if middle character of subsequence is found in the string ; multiply the total occurrences of first character before middle character with the total occurrences of third character after middle character ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findOccurrences ( string str , string substr ) { int n = str . length ( ) ; int preLeft [ n ] = { 0 } ; int preRight [ n ] = { 0 } ; if ( str [ 0 ] == substr [ 0 ] ) preLeft [ 0 ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == substr [ 0 ] ) preLeft [ i ] = preLeft [ i - 1 ] + 1 ; else preLeft [ i ] = preLeft [ i - 1 ] ; } if ( str [ n - 1 ] == substr [ 2 ] ) preRight [ n - 1 ] ++ ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( str [ i ] == substr [ 2 ] ) preRight [ i ] = preRight [ i + 1 ] + 1 ; else preRight [ i ] = preRight [ i + 1 ] ; } int counter = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ 1 ] ) { int total = preLeft [ i - 1 ] * preRight [ i + 1 ] ; counter += total ; } } return counter ; } int main ( ) { string str = " GFGFGYSYIOIWIN " ; string substr = " GFG " ; cout << findOccurrences ( str , substr ) ; return 0 ; }
Form the largest palindromic number using atmost two swaps | C ++ implementation to form the largest palindromic number using atmost two swaps ; function to form the largest palindromic number using atmost two swaps ; if length of number is less than '3' then no higher palindromic number can be formed ; find the index of last digit in the 1 st half of ' num ' ; as only the first half of ' num [ ] ' is being considered , therefore for the rightmost digit in the first half of ' num [ ] ' , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element in the first half of ' num [ ] ' up to the left element ; if ' num [ i ] ' is less than the greatest digit encountered so far ; there is no greater right digit for ' num [ i ] ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and also perform the required swap operation in the right halft of ' num [ ] ' to maintain palindromic property , then break ; performing the required swap operations ; Driver program to test above ; required largest palindromic number
#include <bits/stdc++.h> NEW_LINE using namespace std ; void largestPalin ( char num [ ] , int n ) { if ( n <= 3 ) return ; int mid = n / 2 - 1 ; int rightMax [ mid + 1 ] , right ; rightMax [ mid ] = -1 ; right = mid ; for ( int i = mid - 1 ; i >= 0 ; i -- ) { if ( num [ i ] < num [ right ] ) rightMax [ i ] = right ; else { rightMax [ i ] = -1 ; right = i ; } } for ( int i = 0 ; i <= mid ; i ++ ) { if ( rightMax [ i ] != -1 ) { swap ( num [ i ] , num [ rightMax [ i ] ] ) ; swap ( num [ n - i - 1 ] , num [ n - rightMax [ i ] - 1 ] ) ; break ; } } } int main ( ) { char num [ ] = "4697557964" ; int n = strlen ( num ) ; largestPalin ( num , n ) ; cout << " Largest ▁ Palindrome : ▁ " << num ; return 0 ; }
Mirror characters of a string | C ++ code to find the reverse alphabetical order from a given position ; Function which take the given string and the position from which the reversing shall be done and returns the modified string ; Creating a string having reversed alphabetical order ; The string up to the point specified in the question , the string remains unchanged and from the point up to the length of the string , we reverse the alphabetical order ; Driver function
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; string compute ( string str , int n ) { string reverseAlphabet = " zyxwvutsrqponmlkjihgfedcba " ; int l = str . length ( ) ; for ( int i = n ; i < l ; i ++ ) str [ i ] = reverseAlphabet [ str [ i ] - ' a ' ] ; return str ; } int main ( ) { string str = " pneumonia " ; int n = 4 ; string answer = compute ( str , n - 1 ) ; cout << answer ; return 0 ; }