codeparrot/xlcost-text-to-code huggingface dataset:
Longest Common Prefix using Binary Search | A C ++ Program to find the longest common prefix ; A Function to find the string having the minimum length and returns that length ; A Function that returns the longest common prefix from the array of strings ; We will do an in - place binary search on the first string of the array in the range 0 to index ; Same as ( low + high ) / 2 , but avoids overflow for large low and high ; If all the strings in the input array contains this prefix then append this substring to our answer ; And then go for the right part ; else Go for the left part ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinLength ( string arr [ ] , int n ) { int min = INT_MAX ; for ( int i = 0 ; i <= n - 1 ; i ++ ) if ( arr [ i ] . length ( ) < min ) min = arr [ i ] . length ( ) ; return ( min ) ; } bool allContainsPrefix ( string arr [ ] , int n , string str , int start , int end ) { for ( int i = 0 ; i <= n - 1 ; i ++ ) for ( int j = start ; j <= end ; j ++ ) if ( arr [ i ] [ j ] != str [ j ] ) return ( false ) ; return ( true ) ; } string commonPrefix ( string arr [ ] , int n ) { int index = findMinLength ( arr , n ) ; int low = 0 , high = index ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( allContainsPrefix ( arr , n , arr [ 0 ] , low , mid ) ) { prefix = prefix + arr [ 0 ] . substr ( low , mid - low + 1 ) ; low = mid + 1 ; } high = mid - 1 ; } return ( prefix ) ; } int main ( ) { string arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string ans = commonPrefix ( arr , n ) ; if ( ans . length ( ) ) cout << " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " << ans ; else cout << " There ▁ is ▁ no ▁ common ▁ prefix " ; return ( 0 ) ; }
Lower case to upper case | C ++ program to convert a string to uppercase ; Converts a string to uppercase ; Driver code
#include <iostream> NEW_LINE using namespace std ; string to_upper ( string & in ) { for ( int i = 0 ; i < in . length ( ) ; i ++ ) if ( ' a ' <= in [ i ] <= ' z ' ) in [ i ] &= ~ ( 1 << 5 ) ; return in ; } int main ( ) { string str = " geeksforgeeks " ; cout << to_upper ( str ) ; return 0 ; }
Longest Common Prefix using Divide and Conquer Algorithm | A C ++ Program to find the longest common prefix ; A Utility Function to find the common prefix between strings - str1 and str2 ; A Divide and Conquer based function to find the longest common prefix . This is similar to the merge sort technique ; Same as ( low + high ) / 2 , but avoids overflow for large low and high ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string commonPrefixUtil ( string str1 , string str2 ) { string result ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 [ i ] != str2 [ j ] ) break ; result . push_back ( str1 [ i ] ) ; } return ( result ) ; } string commonPrefix ( string arr [ ] , int low , int high ) { if ( low == high ) return ( arr [ low ] ) ; if ( high > low ) { int mid = low + ( high - low ) / 2 ; string str1 = commonPrefix ( arr , low , mid ) ; string str2 = commonPrefix ( arr , mid + 1 , high ) ; return ( commonPrefixUtil ( str1 , str2 ) ) ; } } int main ( ) { string arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string ans = commonPrefix ( arr , 0 , n - 1 ) ; if ( ans . length ( ) ) cout << " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " << ans ; else cout << " There ▁ is ▁ no ▁ common ▁ prefix " ; return ( 0 ) ; }
Longest Common Prefix using Word by Word Matching | A C ++ Program to find the longest common prefix ; A Utility Function to find the common prefix between strings - str1 and str2 ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string commonPrefixUtil ( string str1 , string str2 ) { string result ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 [ i ] != str2 [ j ] ) break ; result . push_back ( str1 [ i ] ) ; } return ( result ) ; } string commonPrefix ( string arr [ ] , int n ) { string prefix = arr [ 0 ] ; for ( int i = 1 ; i <= n - 1 ; i ++ ) prefix = commonPrefixUtil ( prefix , arr [ i ] ) ; return ( prefix ) ; } int main ( ) { string arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string ans = commonPrefix ( arr , n ) ; if ( ans . length ( ) ) printf ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ - ▁ % s " , ans . c_str ( ) ) ; else printf ( " There ▁ is ▁ no ▁ common ▁ prefix " ) ; return ( 0 ) ; }
Find all strings formed from characters mapped to digits of a number | C ++ program to find all strings formed from a given number where each digit maps to given characters . ; Function to find all strings formed from a given number where each digit maps to given characters . ; vector of strings to store output ; stores index of first occurrence of the digits in input ; maintains index of current digit considered ; for each digit ; store index of first occurrence of the digit in the map ; clear vector contents for future use ; do for each character thats maps to the digit ; for first digit , simply push all its mapped characters in the output list ; from second digit onwards ; for each string in output list append current character to it . ; convert current character to string ; Imp - If this is not the first occurrence of the digit , use same character as used in its first occurrence ; store strings formed by current digit ; nothing more needed to be done if this is not the first occurrence of the digit ; replace contents of output list with temp list ; Driver program ; vector to store the mappings ; vector to store input number ; print all possible strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > findCombinations ( vector < int > input , vector < char > table [ ] ) { vector < string > out , temp ; unordered_map < int , int > mp ; int index = 0 ; for ( int d : input ) { if ( mp . find ( d ) == mp . end ( ) ) mp [ d ] = index ; temp . clear ( ) ; for ( int i = 0 ; i < table [ d - 1 ] . size ( ) ; i ++ ) { if ( index == 0 ) { string s ( 1 , table [ d - 1 ] . at ( i ) ) ; out . push_back ( s ) ; } if ( index > 0 ) { for ( string str : out ) { string s ( 1 , table [ d - 1 ] . at ( i ) ) ; if ( mp [ d ] != index ) s = str [ mp [ d ] ] ; str = str + s ; temp . push_back ( str ) ; } if ( mp [ d ] != index ) break ; } } if ( index > 0 ) out = temp ; index ++ ; } return out ; } int main ( ) { vector < char > table [ ] = { { ' A ' , ' B ' , ' C ' } , { ' D ' , ' E ' , ' F ' } , { ' G ' , ' H ' , ' I ' } , { ' J ' , ' K ' , ' L ' } , { ' M ' , ' N ' , ' O ' } , { ' P ' , ' Q ' , ' R ' } , { ' S ' , ' T ' , ' U ' } , { ' V ' , ' W ' , ' X ' } , { ' Y ' , ' Z ' } } ; vector < int > input = { 1 , 2 , 1 } ; vector < string > out = findCombinations ( input , table ) ; for ( string it : out ) cout << it << " ▁ " ; return 0 ; }
1 ' s ▁ and ▁ 2' s complement of a Binary Number | C ++ program to print 1 ' s ▁ and ▁ 2' s complement of a binary number ; Returns '0' for '1' and '1' for '0' ; Print 1 ' s ▁ and ▁ 2' s complement of binary number represented by " bin " ; for ones complement flip every bit ; for two 's complement go from right to left in  ones complement and if we get 1 make, we make  them 0 and keep going left when we get first  0, make that 1 and go out of loop ; If No break : all are 1 as in 111 or 11111 ; in such case , add extra 1 at beginning ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } void printOneAndTwosComplement ( string bin ) { int n = bin . length ( ) ; int i ; string ones , twos ; ones = twos = " " ; for ( i = 0 ; i < n ; i ++ ) ones += flip ( bin [ i ] ) ; twos = ones ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( ones [ i ] == '1' ) twos [ i ] = '0' ; else { twos [ i ] = '1' ; break ; } } if ( i == -1 ) twos = '1' + twos ; cout << "1 ' s ▁ complement : ▁ " << ones << endl ; cout << "2 ' s ▁ complement : ▁ " << twos << endl ; } int main ( ) { string bin = "1100" ; printOneAndTwosComplement ( bin ) ; return 0 ; }
Print Concatenation of Zig | C ++ Program for above approach ; Function for zig - zag Concatenation ; Check is n is less or equal to 1 ; Iterate rowNum from 0 to n - 1 ; Iterate i till s . length ( ) - 1 ; Check is rowNum is 0 or n - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string zigZagConcat ( string s , int n ) { if ( n <= 1 ) { return s ; } string result = " " ; for ( int rowNum = 0 ; rowNum < n ; rowNum ++ ) { int i = rowNum ; bool up = true ; while ( i < s . length ( ) ) { result += s [ i ] ; if ( rowNum == 0 rowNum == n - 1 ) { i += ( 2 * n - 2 ) ; } else { if ( up ) { i += ( 2 * ( n - rowNum ) - 2 ) ; } else { i += rowNum * 2 ; } up ^= true ; } } } return result ; } int main ( ) { string str = " GEEKSFORGEEKS " ; int n = 3 ; cout << zigZagConcat ( str , n ) ; }
Print string of odd length in ' X ' format | CPP program to print cross pattern ; Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pattern ( string str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { int j = len - 1 - i ; for ( int k = 0 ; k < len ; k ++ ) { if ( k == i k == j ) cout << str [ k ] ; else cout << " ▁ " ; } cout << endl ; } } int main ( ) { string str = " geeksforgeeks " ; int len = str . size ( ) ; pattern ( str , len ) ; return 0 ; }
Recursively print all sentences that can be formed from list of word lists | C ++ program to print all possible sentences from a list of word list ; A recursive function to print all possible sentences that can be formed from a list of word list ; Add current word to output array ; If this is last word of current output sentence , then print the output sentence ; Recur for next row ; A wrapper over printUtil ( ) ; Create an array to store sentence ; Consider all words for first row as starting points and print all sentences ; Driver program to test above functions
#include <iostream> NEW_LINE #include <string> NEW_LINE #define R  3 NEW_LINE #define C  3 NEW_LINE using namespace std ; void printUtil ( string arr [ R ] [ C ] , int m , int n , string output [ R ] ) { output [ m ] = arr [ m ] [ n ] ; if ( m == R - 1 ) { for ( int i = 0 ; i < R ; i ++ ) cout << output [ i ] << " ▁ " ; cout << endl ; return ; } for ( int i = 0 ; i < C ; i ++ ) if ( arr [ m + 1 ] [ i ] != " " ) printUtil ( arr , m + 1 , i , output ) ; } void print ( string arr [ R ] [ C ] ) { string output [ R ] ; for ( int i = 0 ; i < C ; i ++ ) if ( arr [ 0 ] [ i ] != " " ) printUtil ( arr , 0 , i , output ) ; } int main ( ) { string arr [ R ] [ C ] = { { " you " , " we " } , { " have " , " are " } , { " sleep " , " eat " , " drink " } } ; print ( arr ) ; return 0 ; }
Check if characters of a given string can be rearranged to form a palindrome | C ++ implementation to check if characters of a given string can be rearranged to form a palindrome ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define NO_OF_CHARS  256 NEW_LINE bool canFormPalindrome ( string str ) { int count [ NO_OF_CHARS ] = { 0 } ; for ( int i = 0 ; str [ i ] ; i ++ ) count [ str [ i ] ] ++ ; int odd = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] & 1 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } int main ( ) { canFormPalindrome ( " geeksforgeeks " ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; canFormPalindrome ( " geeksogeeks " ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Recursively remove all adjacent duplicates | C ++ Program for above approach ; Recursively removes adjacent duplicates from str and returns new string . las_removed is a pointer to last_removed character ; If length of string is 1 or 0 ; Recursively remove all the adjacent characters formed by removing the adjacent characters ; Have to check whether this is the repeated character that matches the last char of the parent String ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeDuplicates ( string s , char ch ) { if ( s . length ( ) <= 1 ) { return s ; } int i = 0 ; while ( i < s . length ( ) ) { if ( i + 1 < s . length ( ) && s [ i ] == s [ i + 1 ] ) { int j = i ; while ( j + 1 < s . length ( ) && s [ j ] == s [ j + 1 ] ) { j ++ ; } char lastChar = i > 0 ? s [ i - 1 ] : ch ; string remStr = removeDuplicates ( s . substr ( j + 1 , s . length ( ) ) , lastChar ) ; s = s . substr ( 0 , i ) ; int k = s . length ( ) , l = 0 ; while ( remStr . length ( ) > 0 && s . length ( ) > 0 && remStr [ 0 ] == s [ s . length ( ) - 1 ] ) { while ( remStr . length ( ) > 0 && remStr [ 0 ] != ch && remStr [ 0 ] == s [ s . length ( ) - 1 ] ) { remStr = remStr . substr ( 1 , remStr . length ( ) ) ; } s = s . substr ( 0 , s . length ( ) - 1 ) ; } s = s + remStr ; i = j ; } else { i ++ ; } } return s ; } int main ( ) { string str1 = " mississipie " ; cout << removeDuplicates ( str1 , ' ▁ ' ) << endl ; string str2 = " ocvvcolop " ; cout << removeDuplicates ( str2 , ' ▁ ' ) << endl ; }
Write your own atoi ( ) |
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int val ; char strn1 [ ] = "12546" ; val = atoi ( strn1 ) ; cout << " String ▁ value ▁ = ▁ " << strn1 << endl ; cout << " Integer ▁ value ▁ = ▁ " << val << endl ; char strn2 [ ] = " GeeksforGeeks " ; val = atoi ( strn2 ) ; cout << " String ▁ value ▁ = ▁ " << strn2 << endl ; cout << " Integer ▁ value ▁ = ▁ " << val << endl ; return ( 0 ) ; }
Generate n | C ++ program to generate n - bit Gray codes ; This function generates all n bit Gray codes and prints the generated codes ; Base case ; Recursive case ; Append 0 to the first half ; Append 1 to the second half ; Function to generate the Gray code of N bits ; print contents of arr ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > generateGray ( int n ) { if ( n <= 0 ) return { "0" } ; if ( n == 1 ) { return { "0" , "1" } ; } vector < string > recAns = generateGray ( n - 1 ) ; vector < string > mainAns ; for ( int i = 0 ; i < recAns . size ( ) ; i ++ ) { string s = recAns [ i ] ; mainAns . push_back ( "0" + s ) ; } for ( int i = recAns . size ( ) - 1 ; i >= 0 ; i -- ) { string s = recAns [ i ] ; mainAns . push_back ( "1" + s ) ; } return mainAns ; } void generateGrayarr ( int n ) { vector < string > arr ; arr = generateGray ( n ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) cout << arr [ i ] << endl ; } int main ( ) { generateGrayarr ( 3 ) ; return 0 ; }
Write your own strcmp that ignores cases |  ; implementation of strcmp that ignores cases ; If characters are same or inverting the 6 th bit makes them same ; Compare the last ( or first mismatching in case of not same ) characters ; Set the 6 th bit in both , then compare ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ic_strcmp ( string s1 , string s2 ) { int i ; for ( i = 0 ; s1 [ i ] && s2 [ i ] ; ++ i ) { if ( s1 [ i ] == s2 [ i ] || ( s1 [ i ] ^ 32 ) == s2 [ i ] ) continue ; else break ; } if ( s1 [ i ] == s2 [ i ] ) return 0 ; if ( ( s1 [ i ] 32 ) < ( s2 [ i ] 32 ) ) return -1 ; return 1 ; } int main ( ) { cout << " ret : ▁ " << ic_strcmp ( " Geeks " , " apple " ) << endl ; cout << " ret : ▁ " << ic_strcmp ( " " , " ABCD " ) << endl ; cout << " ret : ▁ " << ic_strcmp ( " ABCD " , " z " ) << endl ; cout << " ret : ▁ " << ic_strcmp ( " ABCD " , " abcdEghe " ) << endl ; cout << " ret : ▁ " << ic_strcmp ( " GeeksForGeeks " , " gEEksFORGeEKs " ) << endl ; cout << " ret : ▁ " << ic_strcmp ( " GeeksForGeeks " , " geeksForGeeks " ) << endl ; return 0 ; }
Check whether two strings are anagram of each other | C ++ program to check if two strings are anagrams of each other ; function to check whether two strings are anagram of each other ; Create 2 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 " ; Compare count arrays ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define NO_OF_CHARS  256 NEW_LINE bool areAnagram ( char * str1 , char * str2 ) { int count1 [ NO_OF_CHARS ] = { 0 } ; int count2 [ NO_OF_CHARS ] = { 0 } ; int i ; for ( i = 0 ; str1 [ i ] && str2 [ i ] ; i ++ ) { count1 [ str1 [ i ] ] ++ ; count2 [ str2 [ i ] ] ++ ; } if ( str1 [ i ] str2 [ i ] ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } int main ( ) { char str1 [ ] = " geeksforgeeks " ; char str2 [ ] = " forgeeksgeeks " ; if ( areAnagram ( str1 , str2 ) ) cout << " The ▁ two ▁ strings ▁ are ▁ anagram ▁ of ▁ each ▁ other " ; else cout << " The ▁ two ▁ strings ▁ are ▁ not ▁ anagram ▁ of ▁ each ▁ " " other " ; return 0 ; }
Find the smallest window in a string containing all characters of another string | C ++ program to find smallest window containing all characters of a pattern . ; Function to find smallest window containing all characters of ' pat ' ; Check if string ' s ▁ length ▁ ▁ is ▁ less ▁ than ▁ pattern ' s length . If yes then no such window can exist ; Store occurrence ofs characters of pattern ; Start traversing the string Count of characters ; Count occurrence of characters of string ; If string ' s ▁ char ▁ matches ▁ with ▁ ▁ pattern ' s char then increment count ; if all the characters are matched ; Try to minimize the window ; update window size ; If no window found ; Return substring starting from start_index and length min_len ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int no_of_chars = 256 ; string findSubString ( string str , string pat ) { int len1 = str . length ( ) ; int len2 = pat . length ( ) ; if ( len1 < len2 ) { cout << " No ▁ such ▁ window ▁ exists " ; return " " ; } int hash_pat [ no_of_chars ] = { 0 } ; int hash_str [ no_of_chars ] = { 0 } ; for ( int i = 0 ; i < len2 ; i ++ ) hash_pat [ pat [ i ] ] ++ ; int start = 0 , start_index = -1 , min_len = INT_MAX ; int count = 0 ; for ( int j = 0 ; j < len1 ; j ++ ) { hash_str [ str [ j ] ] ++ ; if ( hash_str [ str [ j ] ] <= hash_pat [ str [ j ] ] ) count ++ ; if ( count == len2 ) { while ( hash_str [ str [ start ] ] > hash_pat [ str [ start ] ] hash_pat [ str [ start ] ] == 0 ) { if ( hash_str [ str [ start ] ] > hash_pat [ str [ start ] ] ) hash_str [ str [ start ] ] -- ; start ++ ; } int len_window = j - start + 1 ; if ( min_len > len_window ) { min_len = len_window ; start_index = start ; } } } if ( start_index == -1 ) { cout << " No ▁ such ▁ window ▁ exists " ; return " " ; } return str . substr ( start_index , min_len ) ; } int main ( ) { string str = " this ▁ is ▁ a ▁ test ▁ string " ; string pat = " tist " ; cout << " Smallest ▁ window ▁ is ▁ : ▁ STRNEWLINE " << findSubString ( str , pat ) ; return 0 ; }
Remove characters from the first string which are present in the second string | C ++ program to remove duplicates ; we extract every character of string string 2 ; we find char exit or not ; if char exit we simply remove that char ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeChars ( string string1 , string string2 ) { for ( auto i : string2 ) { while ( find ( string1 . begin ( ) , string1 . end ( ) , i ) != string1 . end ( ) ) { auto itr = find ( string1 . begin ( ) , string1 . end ( ) , i ) ; string1 . erase ( itr ) ; } } return string1 ; } int main ( ) { string string1 , string2 ; string1 = " geeksforgeeks " ; string2 = " mask " ; cout << removeChars ( string1 , string2 ) << endl ; ; return 0 ; }
Remove duplicates from a given string | CPP program to remove duplicate character from character array and print in sorted order ; Used as index in the modified string ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char * removeDuplicate ( char str [ ] , int n ) { int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( str [ i ] == str [ j ] ) break ; if ( j == i ) str [ index ++ ] = str [ i ] ; } return str ; } int main ( ) { char str [ ] = " geeksforgeeks " ; int n = sizeof ( str ) / sizeof ( str [ 0 ] ) ; cout << removeDuplicate ( str , n ) ; return 0 ; }
Remove duplicates from a given string | access time in unordered_map on is O ( 1 ) generally if no collisions occur and therefore it helps us check if an element exists in a string in O ( 1 ) time complexity with constant space . ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char * removeDuplicates ( char * s , int n ) { unordered_map < char , int > exists ; int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( exists [ s [ i ] ] == 0 ) { s [ index ++ ] = s [ i ] ; exists [ s [ i ] ] ++ ; } } return s ; } int main ( ) { char s [ ] = " geeksforgeeks " ; int n = sizeof ( s ) / sizeof ( s [ 0 ] ) ; cout << removeDuplicates ( s , n ) << endl ; return 0 ; }
Print all Knight 's tour possible from a starting point on NxN chessboard | C ++ program of the above approach ; Stores the 8 possible combinations of moves that the knight can follow ; Function to find if ( i , j ) is a valid cell for the knight to move and it exists within the chessboard ; Stores whether there exist any valid path ; Recursive function to iterate through all the paths that the knight can follow ; Mark the current square of the chessboard ; If the number of visited squares are equal to the total number of sqares ; Print the current state of ChessBoard ; Backtrack to the previous move ; Iterate through all the eight possible moves for a knight ; Stores the new position of the knight after a move ; If the new position is a valid position recursively call for the next move ; Backtrack to the previous move ; Driver Code ; If no valid sequence of moves exist
#include <bits/stdc++.h> NEW_LINE using namespace std ; int DirX [ ] = { 2 , 1 , -1 , -2 , -2 , -1 , 1 , 2 } ; int DirY [ ] = { 1 , 2 , 2 , 1 , -1 , -2 , -2 , -1 } ; bool isSafe ( int i , int j , int n , vector < vector < int > > & Board ) { return ( i >= 0 and j >= 0 and i < n and j < n and Board [ i ] [ j ] == 0 ) ; } bool isPossible = false ; void knightTour ( vector < vector < int > > & ChessBoard , int N , int x , int y , int visited = 1 ) { ChessBoard [ x ] [ y ] = visited ; if ( visited == N * N ) { isPossible = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { cout << ChessBoard [ i ] [ j ] << " ▁ " ; } cout << endl ; } cout << endl ; ChessBoard [ x ] [ y ] = 0 ; return ; } for ( int i = 0 ; i < 8 ; i ++ ) { int newX = x + DirX [ i ] ; int newY = y + DirY [ i ] ; if ( isSafe ( newX , newY , N , ChessBoard ) && ! ChessBoard [ newX ] [ newY ] ) { knightTour ( ChessBoard , N , newX , newY , visited + 1 ) ; } } ChessBoard [ x ] [ y ] = 0 ; } int main ( ) { vector < vector < int > > ChessBoard ( 5 , vector < int > ( 5 , 0 ) ) ; int N = ChessBoard . size ( ) ; int X = 1 ; int Y = 1 ; knightTour ( ChessBoard , N , X - 1 , Y - 1 ) ; if ( ! isPossible ) { cout << -1 ; } return 0 ; }