codeparrot/xlcost-text-to-code huggingface dataset:
Nth character in Concatenated Decimal String | C ++ program to get Nth character in concatenated Decimal String ; Utility method to get dth digit of number N ; Method to return Nth character in concatenated decimal string ; sum will store character escaped till now ; dist will store numbers escaped till now ; loop for number lengths ; nine * len will be incremented characters and nine will be incremented numbers ; restore variables to previous correct state ; get distance from last one digit less maximum number ; d will store dth digit of current number ; method will return dth numbered digit of ( dist + diff ) number ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; char getDigit ( int N , int d ) { string str ; stringstream ss ; ss << N ; ss >> str ; return str [ d - 1 ] ; } char getNthChar ( int N ) { int sum = 0 , nine = 9 ; int dist = 0 , len ; for ( len = 1 ; ; len ++ ) { sum += nine * len ; dist += nine ; if ( sum >= N ) { sum -= nine * len ; dist -= nine ; N -= sum ; break ; } nine *= 10 ; } int diff = ceil ( ( double ) N / len ) ; int d = N % len ; if ( d == 0 ) d = len ; return getDigit ( dist + diff , d ) ; } int main ( ) { int N = 251 ; cout << getNthChar ( N ) << endl ; return 0 ; }
Minimum characters to be added at front to make string palindrome | C ++ program for getting minimum character to be added at front to make string palindrome ; function for checking string is palindrome or not ; Driver code ; if string becomes palindrome then break ; erase the last element of the string ; print the number of insertion at front
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ispalindrome ( string s ) { int l = s . length ( ) ; int j ; for ( int i = 0 , j = l - 1 ; i <= j ; i ++ , j -- ) { if ( s [ i ] != s [ j ] ) return false ; } return true ; } int main ( ) { string s = " BABABAA " ; int cnt = 0 ; int flag = 0 ; while ( s . length ( ) > 0 ) { if ( ispalindrome ( s ) ) { flag = 1 ; break ; } else { cnt ++ ; s . erase ( s . begin ( ) + s . length ( ) - 1 ) ; } } s . erase ( s . begin ( ) + s . length ( ) - 1 ) ; if ( flag ) cout << cnt ; }
Count characters at same position as in English alphabet | C ++ program to find number of characters at same position as in English alphabets ; Traverse input string ; Check that index of characters of string is same as of English alphabets by using ASCII values and the fact that all lower case alphabetic characters come together in same order in ASCII table . And same is true for upper case . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( string str ) { int result = 0 ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) if ( i == ( str [ i ] - ' a ' ) || i == ( str [ i ] - ' A ' ) ) result ++ ; return result ; } int main ( ) { string str = " AbgdeF " ; cout << findCount ( str ) ; return 0 ; }
Remove a character from a string to make it a palindrome | C / C ++ program to check whether it is possible to make string palindrome by removing one character ; Utility method to check if substring from low to high is palindrome or not . ; This method returns - 1 if it is not possible to make string a palindrome . It returns - 2 if string is already a palindrome . Otherwise it returns index of character whose removal can make the whole string palindrome . ; Initialize low and right by both the ends of the string ; loop until low and high cross each other ; If both characters are equal then move both pointer towards end ; If removing str [ low ] makes the whole string palindrome . We basically check if substring str [ low + 1. . high ] is palindrome or not . ; If removing str [ high ] makes the whole string palindrome We basically check if substring str [ low + 1. . high ] is palindrome or not . ; We reach here when complete string will be palindrome if complete string is palindrome then return mid character ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string :: iterator low , string :: iterator high ) { while ( low < high ) { if ( * low != * high ) return false ; low ++ ; high -- ; } return true ; } int possiblePalinByRemovingOneChar ( string str ) { int low = 0 , high = str . length ( ) - 1 ; while ( low < high ) { if ( str [ low ] == str [ high ] ) { low ++ ; high -- ; } else { if ( isPalindrome ( str . begin ( ) + low + 1 , str . begin ( ) + high ) ) return low ; if ( isPalindrome ( str . begin ( ) + low , str . begin ( ) + high - 1 ) ) return high ; return -1 ; } } return -2 ; } int main ( ) { string str = " abecbea " ; int idx = possiblePalinByRemovingOneChar ( str ) ; if ( idx == -1 ) cout << " Not ▁ Possible ▁ STRNEWLINE " ; else if ( idx == -2 ) cout << " Possible ▁ without ▁ removing ▁ any ▁ character " ; else cout << " Possible ▁ by ▁ removing ▁ character " << " ▁ at ▁ index ▁ " << idx << " STRNEWLINE " ; return 0 ; }
Print all words matching a pattern in CamelCase Notation Dictionary | C ++ program to print all words in the CamelCase dictionary that matches with a given pattern ; Alphabet size ( # of upper - Case characters ) ; A Trie node ; isLeaf is true if the node represents end of a word ; vector to store list of complete words in leaf node ; Function to insert word into the Trie ; consider only uppercase characters ; get current character position ; mark last node as leaf ; push word into vector associated with leaf node ; Function to print all children of Trie node root ; if current node is leaf ; recurse for all children of root node ; search for pattern in Trie and print all words matching that pattern ; Invalid pattern ; print all words matching that pattern ; Main function to print all words in the CamelCase dictionary that matches with a given pattern ; construct Trie root node ; Construct Trie from given dict ; search for pattern in Trie ; Driver function ; dictionary of words where each word follows CamelCase notation ; pattern consisting of uppercase characters only
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ALPHABET_SIZE  26 NEW_LINE struct TrieNode { TrieNode * children [ ALPHABET_SIZE ] ; bool isLeaf ; list < string > word ; } ; TrieNode * getNewTrieNode ( void ) { TrieNode * pNode = new TrieNode ; if ( pNode ) { pNode -> isLeaf = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) pNode -> children [ i ] = NULL ; } return pNode ; } void insert ( TrieNode * root , string word ) { int index ; TrieNode * pCrawl = root ; for ( int level = 0 ; level < word . length ( ) ; level ++ ) { if ( islower ( word [ level ] ) ) continue ; index = int ( word [ level ] ) - ' A ' ; if ( ! pCrawl -> children [ index ] ) pCrawl -> children [ index ] = getNewTrieNode ( ) ; pCrawl = pCrawl -> children [ index ] ; } pCrawl -> isLeaf = true ; ( pCrawl -> word ) . push_back ( word ) ; } void printAllWords ( TrieNode * root ) { if ( root -> isLeaf ) { for ( string str : root -> word ) cout << str << endl ; } for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) { TrieNode * child = root -> children [ i ] ; if ( child ) printAllWords ( child ) ; } } bool search ( TrieNode * root , string pattern ) { int index ; TrieNode * pCrawl = root ; for ( int level = 0 ; level < pattern . length ( ) ; level ++ ) { index = int ( pattern [ level ] ) - ' A ' ; if ( ! pCrawl -> children [ index ] ) return false ; pCrawl = pCrawl -> children [ index ] ; } printAllWords ( pCrawl ) ; return true ; } void findAllWords ( vector < string > dict , string pattern ) { TrieNode * root = getNewTrieNode ( ) ; for ( string word : dict ) insert ( root , word ) ; if ( ! search ( root , pattern ) ) cout << " No ▁ match ▁ found " ; } int main ( ) { vector < string > dict = { " Hi " , " Hello " , " HelloWorld " , " HiTech " , " HiGeek " , " HiTechWorld " , " HiTechCity " , " HiTechLab " } ; string pattern = " HT " ; findAllWords ( dict , pattern ) ; return 0 ; }
Generate all binary strings from given pattern | Recursive C ++ program to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; Recursive function to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; replace ' ? ' by '0' and recurse ; replace ' ? ' by '1' and recurse ; No need to backtrack as string is passed by value to the function ; Driver code to test above function
#include <iostream> NEW_LINE using namespace std ; void print ( string str , int index ) { if ( index == str . size ( ) ) { cout << str << endl ; return ; } if ( str [ index ] == ' ? ' ) { str [ index ] = '0' ; print ( str , index + 1 ) ; str [ index ] = '1' ; print ( str , index + 1 ) ; } else print ( str , index + 1 ) ; } int main ( ) { string str = "1 ? ? 0?101" ; print ( str , 0 ) ; return 0 ; }
Find the most frequent digit without using array / string | Finds maximum occurring digit without using any array / string ; Simple function to count occurrences of digit d in x ; int count = 0 ; Initialize count of digit d ; Increment count if current digit is same as d ; Returns the max occurring digit in x ; Handle negative number ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOccurrences ( long int x , int d ) { while ( x ) { if ( x % 10 == d ) count ++ ; x = x / 10 ; } return count ; } int maxOccurring ( long int x ) { if ( x < 0 ) x = - x ; for ( int d = 0 ; d <= 9 ; d ++ ) { int count = countOccurrences ( x , d ) ; if ( count >= max_count ) { max_count = count ; result = d ; } } return result ; } int main ( ) { long int x = 1223355 ; cout << " Max ▁ occurring ▁ digit ▁ is ▁ " << maxOccurring ( x ) ; return 0 ; }
Remove recurring digits in a given number | C ++ program to remove recurring digits from a given number ; Removes recurring digits in num [ ] ; Traverse digits of given number one by one ; Copy the first occurrence of new digit ; Remove repeating occurrences of digit ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeRecurringDigits ( char num [ ] ) { int len = strlen ( num ) ; for ( int i = 0 ; i < len ; i ++ ) { num [ j ++ ] = num [ i ] ; while ( i + 1 < len && num [ i ] == num [ i + 1 ] ) i ++ ; } int main ( ) { char num [ ] = "1299888833" ; removeRecurringDigits ( num ) ; cout << " Modified ▁ number ▁ is ▁ " << num ; return 0 ; }
Find the maximum subarray XOR in a given array | A simple C ++ program to find max subarray XOR ; Initialize result ; Pick starting points of subarrays ; Pick ending points of subarrays starting with i ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubarrayXOR ( int arr [ ] , int n ) { int ans = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { curr_xor = curr_xor ^ arr [ j ] ; ans = max ( ans , curr_xor ) ; } } return ans ; } int main ( ) { int arr [ ] = { 8 , 1 , 2 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Max ▁ subarray ▁ XOR ▁ is ▁ " << maxSubarrayXOR ( arr , n ) ; return 0 ; }
Recursive Implementation of atoi ( ) | Recursive C program to compute atoi ( ) ; Recursive function to compute atoi ( ) ; Base case ( Only one digit ) ; If more than 1 digits , recur for ( n - 1 ) , multiplu result with 10 and add last digit ; Driver Program
#include <stdio.h> NEW_LINE #include <string.h> NEW_LINE int myAtoiRecursive ( char * str , int n ) { if ( n == 1 ) return * str - '0' ; return ( 10 * myAtoiRecursive ( str , n - 1 ) + str [ n - 1 ] - '0' ) ; } int main ( void ) { char str [ ] = "112" ; int n = strlen ( str ) ; printf ( " % d " , myAtoiRecursive ( str , n ) ) ; return 0 ; }
Print string of odd length in ' X ' format | C ++ program to print the given pattern ; Function to print the given string in respective pattern ; Print characters at corresponding places satisfying the two conditions ; Print blank space at rest of places ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPattern ( string str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { for ( int j = 0 ; j < len ; j ++ ) { if ( ( i == j ) || ( i + j == len - 1 ) ) cout << str [ j ] ; else cout << " ▁ " ; } cout << endl ; } } int main ( ) { string str = " geeksforgeeks " ; int len = str . size ( ) ; printPattern ( str , len ) ; return 0 ; }
Find the longest substring with k unique characters in a given string | C ++ program to find the longest substring with k unique characters in a given string ; This function calculates number of unique characters using a associative array count [ ] . Returns true if no . of characters are less than required else returns false . ; Return true if k is greater than or equal to val ; Finds the maximum substring with exactly k unique chars ; Associative array to store the count of characters ; Traverse the string , Fills the associative array count [ ] and count number of unique characters ; If there are not enough unique characters , show an error message . ; Otherwise take a window with first element in it . start and end variables . ; Also initialize values for result longest window ; Initialize associative array count [ ] with zero ; put the first character ; Start from the second character and add characters in window according to above explanation ; Add the character ' s [ i ] ' to current window ; If there are more than k unique characters in current window , remove from left side ; Update the max window size if required ; Driver function
#include <iostream> NEW_LINE #include <cstring> NEW_LINE #define MAX_CHARS  26 NEW_LINE using namespace std ; bool isValid ( int count [ ] , int k ) { int val = 0 ; for ( int i = 0 ; i < MAX_CHARS ; i ++ ) if ( count [ i ] > 0 ) val ++ ; return ( k >= val ) ; } void kUniques ( string s , int k ) { int count [ MAX_CHARS ] ; memset ( count , 0 , sizeof ( count ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count [ s [ i ] - ' a ' ] == 0 ) u ++ ; count [ s [ i ] - ' a ' ] ++ ; } if ( u < k ) { cout << " Not ▁ enough ▁ unique ▁ characters " ; return ; } int curr_start = 0 , curr_end = 0 ; int max_window_size = 1 , max_window_start = 0 ; memset ( count , 0 , sizeof ( count ) ) ; count [ s [ 0 ] - ' a ' ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { count [ s [ i ] - ' a ' ] ++ ; curr_end ++ ; while ( ! isValid ( count , k ) ) { count [ s [ curr_start ] - ' a ' ] -- ; curr_start ++ ; } if ( curr_end - curr_start + 1 > max_window_size ) { max_window_size = curr_end - curr_start + 1 ; max_window_start = curr_start ; } } cout << " Max ▁ substring ▁ is ▁ : ▁ " << s . substr ( max_window_start , max_window_size ) << " ▁ with ▁ length ▁ " << max_window_size << endl ; } int main ( ) { string s = " aabacbebebe " ; int k = 3 ; kUniques ( s , k ) ; return 0 ; }
Function to find Number of customers who could not get a computer | C ++ program to find number of customers who couldn 't get a resource. ; n is number of computers in cafe . ' seq ' is given sequence of customer entry , exit events ; seen [ i ] = 0 , indicates that customer ' i ' is not in cafe seen [ 1 ] = 1 , indicates that customer ' i ' is in cafe but computer is not assigned yet . seen [ 2 ] = 2 , indicates that customer ' i ' is in cafe and has occupied a computer . ; Initialize result which is number of customers who could not get any computer . ; Traverse the input sequence ; Find index of current character in seen [ 0. . .25 ] ; If First occurrence of ' seq [ i ] ' ; set the current character as seen ; If number of occupied computers is less than n , then assign a computer to new customer ; Set the current character as occupying a computer ; Else this customer cannot get a computer , increment result ; If this is second occurrence of ' seq [ i ] ' ; Decrement occupied only if this customer was using a computer ; Driver program
#include <iostream> NEW_LINE #include <cstring> NEW_LINE using namespace std ; #define MAX_CHAR  26 NEW_LINE int runCustomerSimulation ( int n , const char * seq ) { char seen [ MAX_CHAR ] = { 0 } ; int res = 0 ; for ( int i = 0 ; seq [ i ] ; i ++ ) { int ind = seq [ i ] - ' A ' ; if ( seen [ ind ] == 0 ) { seen [ ind ] = 1 ; if ( occupied < n ) { occupied ++ ; seen [ ind ] = 2 ; } else res ++ ; } else { if ( seen [ ind ] == 2 ) occupied -- ; seen [ ind ] = 0 ; } } return res ; } int main ( ) { cout << runCustomerSimulation ( 2 , " ABBAJJKZKZ " ) << endl ; cout << runCustomerSimulation ( 3 , " GACCBDDBAGEE " ) << endl ; cout << runCustomerSimulation ( 3 , " GACCBGDDBAEE " ) << endl ; cout << runCustomerSimulation ( 1 , " ABCBCA " ) << endl ; cout << runCustomerSimulation ( 1 , " ABCBCADEED " ) << endl ; return 0 ; }
Check if characters of a given string can be rearranged to form a palindrome | C ++ Implementation of the above approach ; bitvector to store the record of which character appear odd and even number of times ; Driver Code
# include <bits/stdc++.h> NEW_LINE using namespace std ; bool canFormPalindrome ( string a ) { int bitvector = 0 , mask = 0 ; for ( int i = 0 ; a [ i ] != ' \0' ; i ++ ) { int x = a [ i ] - ' a ' ; mask = 1 << x ; bitvector = bitvector ^ mask ; } return ( bitvector & ( bitvector - 1 ) ) == 0 ; } int main ( ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) cout << ( " Yes " ) << endl ; else cout << ( " No " ) << endl ; return 0 ; }
Print all pairs of anagrams in a given array of strings | C ++ program to Print all pairs of anagrams in a given array of strings ; function to check whether two strings are anagram of each other ; Create two count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array ; This function prints all anagram pairs in a given array of strings ; Driver program to test to pront printDups
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define NO_OF_CHARS  256 NEW_LINE bool areAnagram ( string str1 , string str2 ) { int count [ NO_OF_CHARS ] = { 0 } ; int i ; for ( i = 0 ; str1 [ i ] && str2 [ i ] ; i ++ ) { count [ str1 [ i ] ] ++ ; count [ str2 [ i ] ] -- ; } if ( str1 [ i ] str2 [ i ] ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] ) return false ; return true ; } void findAllAnagrams ( string arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( areAnagram ( arr [ i ] , arr [ j ] ) ) cout << arr [ i ] << " ▁ is ▁ anagram ▁ of ▁ " << arr [ j ] << endl ; } int main ( ) { string arr [ ] = { " geeksquiz " , " geeksforgeeks " , " abcd " , " forgeeksgeeks " , " zuiqkeegs " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findAllAnagrams ( arr , n ) ; return 0 ; }
Program to print all palindromes in a given range | A function to check if n is palindrome ; Find reverse of n ; If n and rev are same , then n is palindrome ; prints palindrome between min and max ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int isPalindrome ( int n ) { int rev = 0 ; for ( int i = n ; i > 0 ; i /= 10 ) rev = rev * 10 + i % 10 ; return ( n == rev ) ; } void countPal ( int min , int max ) { for ( int i = min ; i <= max ; i ++ ) if ( isPalindrome ( i ) ) cout << i << " ▁ " ; } int main ( ) { countPal ( 100 , 2000 ) ; return 0 ; }
Length of the longest substring without repeating characters | C ++ program to find the length of the longest substring without repeating characters ; result ; Note : Default values in visited are false ; If current character is visited Break the loop ; Else update the result if this window is larger , and mark current character as visited . ; Remove the first character of previous window ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestUniqueSubsttr ( string str ) { int n = str . size ( ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { vector < bool > visited ( 256 ) ; for ( int j = i ; j < n ; j ++ ) { if ( visited [ str [ j ] ] == true ) break ; else { res = max ( res , j - i + 1 ) ; visited [ str [ j ] ] = true ; } } visited [ str [ i ] ] = false ; } return res ; } int main ( ) { string str = " geeksforgeeks " ; cout << " The ▁ input ▁ string ▁ is ▁ " << str << endl ; int len = longestUniqueSubsttr ( str ) ; cout << " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ " " character ▁ substring ▁ is ▁ " << len ; return 0 ; }
Length of the longest substring without repeating characters | Creating a set to store the last positions of occurrence ; Starting the initial point of window to index 0 ; Checking if we have already seen the element or not ; If we have seen the number , move the start pointer to position after the last occurrence ; Updating the last seen value of the character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestUniqueSubsttr ( string s ) { map < char , int > seen ; int maximum_length = 0 ; int start = 0 ; for ( int end = 0 ; end < s . length ( ) ; end ++ ) { if ( seen . find ( s [ end ] ) != seen . end ( ) ) { start = max ( start , seen [ s [ end ] ] + 1 ) ; } seen [ s [ end ] ] = end ; maximum_length = max ( maximum_length , end - start + 1 ) ; } return maximum_length ; } int main ( ) { string s = " geeksforgeeks " ; cout << " The ▁ input ▁ String ▁ is ▁ " << s << endl ; int length = longestUniqueSubsttr ( s ) ; cout << " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ character ▁ " << " substring ▁ is ▁ " << length ; }
Find the smallest window in a string containing all characters of another string | Function ; Answer length of ans ; starting index of ans ; creating map ; References of Window ; Traversing the window ; Calculations ; Condition matching ; Sorting ans ; Sliding I Calculation for removing I ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string Minimum_Window ( string s , string t ) { int m [ 256 ] = { 0 } ; int ans = INT_MAX ; int start = 0 ; int count = 0 ; for ( int i = 0 ; i < t . length ( ) ; i ++ ) { if ( m [ t [ i ] ] == 0 ) count ++ ; m [ t [ i ] ] ++ ; } int i = 0 ; int j = 0 ; while ( j < s . length ( ) ) { m [ s [ j ] ] -- ; if ( m [ s [ j ] ] == 0 ) count -- ; if ( count == 0 ) { while ( count == 0 ) { if ( ans > j - i + 1 ) { ans = min ( ans , j - i + 1 ) ; start = i ; } m [ s [ i ] ] ++ ; if ( m [ s [ i ] ] > 0 ) count ++ ; i ++ ; } } j ++ ; } if ( ans != INT_MAX ) return s . substr ( start , ans ) ; else return " - 1" ; } main ( ) { string s = " ADOBECODEBANC " ; string t = " ABC " ; cout << " - - > Smallest ▁ window ▁ that ▁ contain ▁ all ▁ character ▁ : ▁ " << endl ; cout << Minimum_Window ( s , t ) ; }
Run Length Encoding | CPP program to implement run length encoding ; Count occurrences of current character ; Print character and its count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRLE ( string str ) { int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; while ( i < n - 1 && str [ i ] == str [ i + 1 ] ) { count ++ ; i ++ ; } cout << str [ i ] << count ; } } int main ( ) { string str = " wwwwaaadexxxxxxywww " ; printRLE ( str ) ; return 0 ; }