codeparrot/xlcost-text-to-code huggingface dataset:
Find nth number that contains the digit k or divisible by k . | C ++ program to find nth number that contains the digit k or divisible by k . ; Function for checking if digit k is in n or not ; finding remainder ; if digit found ; Function for finding nth number ; since k is the first which satisfy the criteria , so consider it in count making count = 1 and starting from i = k + 1 ; checking that the number contain k digit or divisible by k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkdigit ( int n , int k ) { while ( n ) { int rem = n % 10 ; if ( rem == k ) return 1 ; n = n / 10 ; } return 0 ; } int findNthNumber ( int n , int k ) { for ( int i = k + 1 , count = 1 ; count < n ; i ++ ) { if ( checkdigit ( i , k ) || ( i % k == 0 ) ) count ++ ; if ( count == n ) return i ; } return -1 ; } int main ( ) { int n = 10 , k = 2 ; cout << findNthNumber ( n , k ) << endl ; return 0 ; }
Count of subarrays of size K which is a permutation of numbers from 1 to K | C ++ program to implement the above approach ; Save index of numbers of the array ; Update min and max index with the current index and check if it 's a valid permutation ; Driver code
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; int find_permutations ( vector < int > & arr ) { int cnt = 0 ; int max_ind = -1 , min_ind = 10000000 ; int n = arr . size ( ) ; unordered_map < int , int > index_of ; for ( int i = 0 ; i < n ; i ++ ) { index_of [ arr [ i ] ] = i + 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { max_ind = max ( max_ind , index_of [ i ] ) ; min_ind = min ( min_ind , index_of [ i ] ) ; if ( max_ind - min_ind + 1 == i ) cnt ++ ; } return cnt ; } int main ( ) { vector < int > nums ; nums . push_back ( 2 ) ; nums . push_back ( 3 ) ; nums . push_back ( 1 ) ; nums . push_back ( 5 ) ; nums . push_back ( 4 ) ; cout << find_permutations ( nums ) ; return 0 ; }
Count of integers that divide all the elements of the given array | C ++ implementation of the approach ; Function to return the count of the required integers ; To store the gcd of the array elements ; To store the count of factors of the found gcd ; If g is a perfect square ; Factors appear in pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int a [ ] , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = __gcd ( gcd , a [ i ] ) ; int cnt = 0 ; for ( int i = 1 ; i * i <= gcd ; i ++ ) { if ( gcd % i == 0 ) { if ( i * i == gcd ) cnt ++ ; else cnt += 2 ; } } return cnt ; } int main ( ) { int a [ ] = { 4 , 16 , 1024 , 48 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << getCount ( a , n ) ; return 0 ; }
Minimize cost of removals required to make all remaining characters of the string unique | C ++ program to implement the above approach ; Function to find the minimum cost of removing characters to make the String unique ; stores the visited character ; stores the answer ; traverse the String ; if already visited ; Stores the maximum cost of removing a particular character ; Store the total deletion cost of a particular character ; Mark the current character visited ; Traverse the indices of the String [ i , N - 1 ] ; If any duplicate is found ; Update the maximum cost and total cost ; Mark the current character visited ; Keep the character with maximum cost and delete the rest ; return the minimum cost ; Driver code ; input String ; input array ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int delCost ( string s , int cost [ ] , int l1 , int l2 ) { bool visited [ l1 ] ; memset ( visited , 0 , sizeof ( visited ) ) ; int ans = 0 ; for ( int i = 0 ; i < l1 ; i ++ ) { if ( visited [ i ] ) { continue ; } int maxDel = 0 ; int totalCost = 0 ; visited [ i ] = 1 ; for ( int j = i ; j < l1 ; j ++ ) { if ( s [ i ] == s [ j ] ) { maxDel = max ( maxDel , cost [ j ] ) ; totalCost += cost [ j ] ; visited [ j ] = 1 ; } } ans += totalCost - maxDel ; } return ans ; } int main ( ) { string s = " AAABBB " ; int l1 = s . size ( ) ; int cost [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int l2 = sizeof ( cost ) / sizeof ( cost [ 0 ] ) ; cout << delCost ( s , cost , l1 , l2 ) ; return 0 ; }
Make all array elements equal by replacing triplets with their Bitwise XOR | C ++ program to implement the above approach ; Function to find triplets such that replacing them with their XOR make all array elements equal ; If N is even ; Calculate xor of array elements ; Traverse the array ; Update xor ; If xor is not equal to 0 ; Selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; Selecting the triplets such that all array elements can be made equal to arr [ N - 1 ] ; Selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; Selecting the triplets such that all array elements can be made equal to arr [ N - 1 ] ; Driver Code ; Given array ; Size of array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkXOR ( int arr [ ] , int N ) { if ( N % 2 == 0 ) { int xro = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xro ^= arr [ i ] ; } if ( xro != 0 ) { cout << -1 << endl ; return ; } for ( int i = 0 ; i < N - 3 ; i += 2 ) { cout << i << " ▁ " << i + 1 << " ▁ " << i + 2 << endl ; } for ( int i = 0 ; i < N - 3 ; i += 2 ) { cout << i << " ▁ " << i + 1 << " ▁ " << N - 1 << endl ; } } else { for ( int i = 0 ; i < N - 2 ; i += 2 ) { cout << i << " ▁ " << i + 1 << " ▁ " << i + 2 << endl ; } for ( int i = 0 ; i < N - 2 ; i += 2 ) { cout << i << " ▁ " << i + 1 << " ▁ " << N - 1 << endl ; } } } int main ( ) { int arr [ ] = { 4 , 2 , 1 , 7 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkXOR ( arr , N ) ; }
Make all array elements even by replacing adjacent pair of array elements with their sum | C ++ program to implement the above approach ; Function to find minimum count of operations required to make all array elements even ; Stores minimum count of replacements to make all array elements even ; Stores the count of odd continuous numbers ; Traverse the array ; If arr [ i ] is an odd number ; Update odd_cont_seg ; If odd_cont_seg is even ; Update res ; Update res ; Reset odd_cont_seg = 0 ; If odd_cont_seg exceeds 0 ; If odd_cont_seg is even ; Update res ; Update res ; Print the result ; Drivers Code
#include <iostream> NEW_LINE using namespace std ; int make_array_element_even ( int arr [ ] , int N ) { int res = 0 ; int odd_cont_seg = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd_cont_seg ++ ; } else { if ( odd_cont_seg > 0 ) { if ( odd_cont_seg % 2 == 0 ) { res += odd_cont_seg / 2 ; } else { res += ( odd_cont_seg / 2 ) + 2 ; } odd_cont_seg = 0 ; } } } if ( odd_cont_seg > 0 ) { if ( odd_cont_seg % 2 == 0 ) { res += odd_cont_seg / 2 ; } else { res += odd_cont_seg / 2 + 2 ; } } return res ; } int main ( ) { int arr [ ] = { 2 , 4 , 5 , 11 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << make_array_element_even ( arr , N ) ; return 0 ; }
Find a number K such that exactly K array elements are greater than or equal to K | C ++ program for the above approach ; Function to find K for which there are exactly K array elements greater than or equal to K ; Finding the largest array element ; Possible values of K ; Traverse the array ; If current array element is greater than or equal to i ; If i array elements are greater than or equal to i ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int zvalue ( vector < int > & nums ) { int m = * max_element ( nums . begin ( ) , nums . end ( ) ) ; int cnt = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { cnt = 0 ; for ( int j = 0 ; j < nums . size ( ) ; j ++ ) { if ( nums [ j ] >= i ) cnt ++ ; } if ( cnt == i ) return i ; } return -1 ; } int main ( ) { vector < int > nums = { 7 , 8 , 9 , 0 , 0 , 1 } ; cout << zvalue ( nums ) << endl ; }
Lexicographically smallest and largest anagrams of a string containing another string as its substring | C ++ program for the above approach ; Function to find the lexicographically smallest anagram of string which contains another string ; Initializing the map and set ; Iterating over s1 ; Storing the frequency of characters present in s1 ; Storing the distinct characters present in s1 ; Decreasing the frequency of characters from M that are already present in s2 ; Traversing alphabets in sorted order ; If current character of set is not equal to current character of s2 ; If element is equal to current character of s2 ; Checking for second distinct character in s2 ; s2 [ j ] will store second distinct character ; Return the answer ; Function to find the lexicographically largest anagram of string which contains another string ; Getting the lexicographically smallest anagram ; d1 stores the prefix ; d2 stores the suffix ; Return the result ; Driver Code ; Given two strings ; Function Calls
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < string , int > lexico_smallest ( string s1 , string s2 ) { map < char , int > M ; set < char > S ; pair < string , int > pr ; for ( int i = 0 ; i <= s1 . size ( ) - 1 ; ++ i ) { M [ s1 [ i ] ] ++ ; S . insert ( s1 [ i ] ) ; } for ( int i = 0 ; i <= s2 . size ( ) - 1 ; ++ i ) { M [ s2 [ i ] ] -- ; } char c = s2 [ 0 ] ; int index = 0 ; string res = " " ; for ( auto x : S ) { if ( x != c ) { for ( int i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } } else { int j = 0 ; index = res . size ( ) ; while ( s2 [ j ] == x ) { j ++ ; } if ( s2 [ j ] < c ) { res += s2 ; for ( int i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } } else { for ( int i = 1 ; i <= M [ x ] ; ++ i ) { res += x ; } index += M [ x ] ; res += s2 ; } } } pr . first = res ; pr . second = index ; return pr ; } string lexico_largest ( string s1 , string s2 ) { pair < string , int > pr = lexico_smallest ( s1 , s2 ) ; string d1 = " " ; for ( int i = pr . second - 1 ; i >= 0 ; i -- ) { d1 += pr . first [ i ] ; } string d2 = " " ; for ( int i = pr . first . size ( ) - 1 ; i >= pr . second + s2 . size ( ) ; -- i ) { d2 += pr . first [ i ] ; } string res = d2 + s2 + d1 ; return res ; } int main ( ) { string s1 = " ethgakagmenpgs " ; string s2 = " geeks " ; cout << lexico_smallest ( s1 , s2 ) . first << " STRNEWLINE " ; cout << lexico_largest ( s1 , s2 ) ; return ( 0 ) ; }
Queries to find the count of shortest paths in a Tree that contains a given edge | C ++ implementation for the above approach ; Adjacency list to represent the tree ; Number of vertices ; Mark visited / unvisited vertices ; Stores the subtree size of the corresponding nodes ; Function to create an edge between two vertices ; Add a to b 's list ; Add b to a 's list ; Function to perform DFS ; Mark the vertex visited ; Include the node in the subtree ; Traverse all its children ; Function to print the required number of paths ; Driver Code ; Number of vertices ; Calling modified dfs function ; Count pairs of vertices in the tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sz = 1e5 ; vector < int > tree [ sz ] ; int n ; bool vis [ sz ] ; int subtreeSize [ sz ] ; void addEdge ( int a , int b ) { tree [ a ] . push_back ( b ) ; tree [ b ] . push_back ( a ) ; } void dfs ( int x ) { vis [ x ] = true ; subtreeSize [ x ] = 1 ; for ( auto i : tree [ x ] ) { if ( ! vis [ i ] ) { dfs ( i ) ; subtreeSize [ x ] += subtreeSize [ i ] ; } } } void countPairs ( int a , int b ) { int sub = min ( subtreeSize [ a ] , subtreeSize [ b ] ) ; cout << sub * ( n - sub ) << endl ; } int main ( ) { n = 6 ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 4 ) ; addEdge ( 3 , 5 ) ; dfs ( 0 ) ; countPairs ( 1 , 3 ) ; countPairs ( 0 , 2 ) ; return 0 ; }
Count of permutations of an Array having each element as a multiple or a factor of its index | C ++ Program to implement the above approach ; Function to find the count of desired permutations ; Base case ; If i has not been inserted ; Backtrack ; Insert i ; Recur to find valid permutations ; Remove i ; Return the final count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findPermutation ( unordered_set < int > & arr , int N ) { int pos = arr . size ( ) + 1 ; if ( pos > N ) return 1 ; int res = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr . find ( i ) == arr . end ( ) ) { if ( i % pos == 0 or pos % i == 0 ) { arr . insert ( i ) ; res += findPermutation ( arr , N ) ; arr . erase ( arr . find ( i ) ) ; } } } return res ; } int main ( ) { int N = 5 ; unordered_set < int > arr ; cout << findPermutation ( arr , N ) ; return 0 ; }
Check if sum Y can be obtained from the Array by the given operations | C ++ Program to implement the above approach ; Function to check if it is possible to obtain sum Y from a sequence of sum X from the array arr [ ] ; Store the difference ; Iterate over the array ; If diff reduced to 0 ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void solve ( int arr [ ] , int n , int X , int Y ) { int diff = Y - X ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != 1 ) { diff = diff % ( arr [ i ] - 1 ) ; } } if ( diff == 0 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int arr [ ] = { 1 , 2 , 7 , 9 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 11 , Y = 13 ; solve ( arr , n , X , Y ) ; return 0 ; }
Farthest distance of a Node from each Node of a Tree | C ++ Program to implement the above approach ; Adjacency List to store the graph ; Stores the height of each node ; Stores the maximum distance of a node from its ancestors ; Function to add edge between two vertices ; Insert edge from u to v ; Insert edge from v to u ; Function to calculate height of each Node ; Iterate in the adjacency list of the current node ; Dfs for child node ; Calculate height of nodes ; Increase height ; Function to calculate the maximum distance of a node from its ancestor ; Iterate in the adjacency list of the current node ; Find two children with maximum heights ; Calculate the maximum distance with ancestor for every node ; Calculating for children ; Driver Code ; Calculate height of nodes of the tree ; Calculate the maximum distance with ancestors ; Print the maximum of the two distances from each node
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxN  100001 NEW_LINE vector < int > adj [ maxN ] ; int height [ maxN ] ; int dist [ maxN ] ; void addEdge ( int u , int v ) { adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } void dfs1 ( int cur , int par ) { for ( auto u : adj [ cur ] ) { if ( u != par ) { dfs1 ( u , cur ) ; height [ cur ] = max ( height [ cur ] , height [ u ] ) ; } } height [ cur ] += 1 ; } void dfs2 ( int cur , int par ) { int max1 = 0 ; int max2 = 0 ; for ( auto u : adj [ cur ] ) { if ( u != par ) { if ( height [ u ] >= max1 ) { max2 = max1 ; max1 = height [ u ] ; } else if ( height [ u ] > max2 ) { max2 = height [ u ] ; } } } int sum = 0 ; for ( auto u : adj [ cur ] ) { if ( u != par ) { sum = ( ( max1 == height [ u ] ) ? max2 : max1 ) ; if ( max1 == height [ u ] ) dist [ u ] = 1 + max ( 1 + max2 , dist [ cur ] ) ; else dist [ u ] = 1 + max ( 1 + max1 , dist [ cur ] ) ; dfs2 ( u , cur ) ; } } } int main ( ) { int n = 6 ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 5 , 6 ) ; dfs1 ( 1 , 0 ) ; dfs2 ( 1 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) cout << ( max ( dist [ i ] , height [ i ] ) - 1 ) << " ▁ " ; return 0 ; }
Middle of three using minimum comparisons | CPP program to find middle of three distinct numbers ; Function to find the middle of three number ; Checking for b ; Checking for a ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int middleOfThree ( int a , int b , int c ) { int middleOfThree ( int a , int b , int c ) { if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ; else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ; else return c ; } int main ( ) { int a = 20 , b = 30 , c = 40 ; cout << middleOfThree ( a , b , c ) ; return 0 ; }
Difference between Insertion sort and Selection sort | C ++ program for implementation of selection sort ; Function to implement the selection sort ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; Swap the found minimum element with the first element ; Function to print an array ; Driver Code ; Function Call ; Print the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( int * xp , int * yp ) { int temp = * xp ; * xp = * yp ; * yp = temp ; } void selectionSort ( int arr [ ] , int n ) { int i , j , min_idx ; for ( i = 0 ; i < n - 1 ; i ++ ) { min_idx = i ; for ( j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] < arr [ min_idx ] ) min_idx = j ; swap ( & arr [ min_idx ] , & arr [ i ] ) ; } } void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; } int main ( ) { int arr [ ] = { 64 , 25 , 12 , 22 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; selectionSort ( arr , n ) ; cout << " Sorted ▁ array : ▁ STRNEWLINE " ; printArray ( arr , n ) ; return 0 ; }
Check if a given string can be converted to another by given possible swaps | C ++ program to implement the above approach ; Stores length of str1 ; Stores length of str2 ; Stores distinct characters of str1 ; Stores distinct characters of str2 ; Stores frequency of each character of str1 ; Traverse the string str1 ; Update frequency of str1 [ i ] ; Traverse the string str1 ; Insert str1 [ i ] into st1 ; Traverse the string str2 ; Insert str1 [ i ] into st1 ; If distinct characters in str1 and str2 are not same ; Stores frequency of each character of str2 ; Traverse the string str2 ; Update frequency of str2 [ i ] ; Sort hash1 [ ] array ; Sort hash2 [ ] array ; Traverse hash1 [ ] and hash2 [ ] ; If hash1 [ i ] not equal to hash2 [ i ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkStr1CanConStr2 ( string & str1 , string & str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; set < int > st1 ; set < int > st2 ; int hash1 [ 256 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { hash1 [ str1 [ i ] ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { st1 . insert ( str1 [ i ] ) ; } for ( int i = 0 ; i < M ; i ++ ) { st2 . insert ( str2 [ i ] ) ; } if ( st1 != st2 ) { return false ; } int hash2 [ 256 ] = { 0 } ; for ( int i = 0 ; i < M ; i ++ ) { hash2 [ str2 [ i ] ] ++ ; } sort ( hash1 , hash1 + 256 ) ; sort ( hash2 , hash2 + 256 ) ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( hash1 [ i ] != hash2 [ i ] ) { return false ; } } return true ; } int main ( ) { string str1 = " xyyzzlll " ; string str2 = " yllzzxxx " ; if ( checkStr1CanConStr2 ( str1 , str2 ) ) { cout << " True " ; } else { cout << " False " ; } }
Sort the array in a given index range | C ++ program to sort the array in a given index range ; Function to sort the elements of the array from index a to index b ; Variables to store start and end of the index range ; Sort the subarray from arr [ l ] to arr [ r ] ; Print the modified array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void partSort ( int arr [ ] , int N , int a , int b ) { int l = min ( a , b ) ; int r = max ( a , b ) ; vector < int > v ( arr , arr + N ) ; sort ( v . begin ( ) + l , v . begin ( ) + r + 1 ) ; for ( int i = 0 ; i < N ; i ++ ) cout << v [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 7 , 8 , 4 , 5 , 2 } ; int a = 1 , b = 4 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; partSort ( arr , N , a , b ) ; }
Find the minimum cost to reach destination using a train | A Dynamic Programming based solution to find min cost to reach station N - 1 from station 0. ; This function returns the smallest possible cost to reach station N - 1 from station 0. ; dist [ i ] stores minimum cost to reach station i from station 0. ; Go through every station and check if using it as an intermediate station gives better path ; Driver program to test above function
#include <iostream> NEW_LINE #include <climits> NEW_LINE using namespace std ; #define INF  INT_MAX NEW_LINE #define N  4 NEW_LINE int minCost ( int cost [ ] [ N ] ) { int dist [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dist [ i ] = INF ; dist [ 0 ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [ j ] ; return dist [ N - 1 ] ; } int main ( ) { int cost [ N ] [ N ] = { { 0 , 15 , 80 , 90 } , { INF , 0 , 40 , 50 } , { INF , INF , 0 , 70 } , { INF , INF , INF , 0 } } ; cout << " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ " << N << " ▁ is ▁ " << minCost ( cost ) ; return 0 ; }
Number of loops of size k starting from a specific node | C ++ Program to find number of cycles of length k in a graph with n nodes . ; Return the Number of ways from a node to make a loop of size K in undirected complete connected graph of N nodes ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numOfways ( int n , int k ) { int p = 1 ; if ( k % 2 ) p = -1 ; return ( pow ( n - 1 , k ) + p * ( n - 1 ) ) / n ; } int main ( ) { int n = 4 , k = 2 ; cout << numOfways ( n , k ) << endl ; return 0 ; }
Program to find the largest and smallest ASCII valued characters in a string | C ++ program to find largest and smallest characters in a string . ; function that return the largest alphabet . ; initializing max alphabet to ' a ' ; find largest alphabet ; returning largest element ; function that return the smallest alphabet ; initializing smallest alphabet to ' z ' ; find smallest alphabet ; returning smallest alphabet ; Driver Code ; Character array ; Calculating size of the string ; calling functions and print returned value
#include <iostream> NEW_LINE using namespace std ; char largest_alphabet ( char a [ ] , int n ) { char max = ' A ' ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] > max ) max = a [ i ] ; return max ; } char smallest_alphabet ( char a [ ] , int n ) { char min = ' z ' ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] < min ) min = a [ i ] ; return min ; } int main ( ) { char a [ ] = " GeEksforGeeks " ; int size = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " Largest ▁ and ▁ smallest ▁ alphabet ▁ is ▁ : ▁ " ; cout << largest_alphabet ( a , size ) << " ▁ and ▁ " ; cout << smallest_alphabet ( a , size ) << endl ; return 0 ; }
Make largest palindrome by changing at most K | C ++ program to get largest palindrome changing atmost K digits ; Returns maximum possible palindrome using k changes ; Initialize l and r by leftmost and rightmost ends ; first try to make string palindrome ; Replace left and right character by maximum of both ; If k is negative then we can 't make  string palindrome ; At mid character , if K > 0 then change it to 9 ; If character at lth ( same as rth ) is less than 9 ; If none of them is changed in the previous loop then subtract 2 from K and convert both to 9 ; If one of them is changed in the previous loop then subtract 1 from K ( 1 more is subtracted already ) and make them 9 ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; string maximumPalinUsingKChanges ( string str , int k ) { string palin = str ; int l = 0 ; int r = str . length ( ) - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { palin [ l ] = palin [ r ] = max ( str [ l ] , str [ r ] ) ; k -- ; } l ++ ; r -- ; } if ( k < 0 ) return " Not ▁ possible " ; l = 0 ; r = str . length ( ) - 1 ; while ( l <= r ) { if ( l == r ) { if ( k > 0 ) palin [ l ] = '9' ; } if ( palin [ l ] < '9' ) { if ( k >= 2 && palin [ l ] == str [ l ] && palin [ r ] == str [ r ] ) { k -= 2 ; palin [ l ] = palin [ r ] = '9' ; } else if ( k >= 1 && ( palin [ l ] != str [ l ] palin [ r ] != str [ r ] ) ) { k -- ; palin [ l ] = palin [ r ] = '9' ; } } l ++ ; r -- ; } return palin ; } int main ( ) { string str = "43435" ; int k = 3 ; cout << maximumPalinUsingKChanges ( str , k ) ; return 0 ; }