codeparrot/xlcost-text-to-code huggingface dataset:
Program for credit card number validation | C ++ program to check if a given credit card is valid or not . ; Return this number if it is a single digit , otherwise , return the sum of the two digits ; Return the number of digits in d ; Return the first k number of digits from number . If the number of digits in number is less than k , return number . ; Return true if the digit d is a prefix for number ; Get the result from Step 2 ; Return sum of odd - place digits in number ; Return true if the card number is valid ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int getDigit ( int number ) { if ( number < 9 ) return number ; return number / 10 + number % 10 ; } int getSize ( long d ) { string num = to_string ( d ) ; return num . length ( ) ; } long getPrefix ( long number , int k ) { if ( getSize ( number ) > k ) { string num = to_string ( number ) ; return stol ( num . substr ( 0 , k ) ) ; } return number ; } bool prefixMatched ( long number , int d ) { return getPrefix ( number , getSize ( d ) ) == d ; } int sumOfDoubleEvenPlace ( long int number ) { int sum = 0 ; string num = to_string ( number ) ; for ( int i = getSize ( number ) - 2 ; i >= 0 ; i -= 2 ) sum += getDigit ( int ( num [ i ] - '0' ) * 2 ) ; return sum ; } int sumOfOddPlace ( long number ) { int sum = 0 ; string num = to_string ( number ) ; for ( int i = getSize ( number ) - 1 ; i >= 0 ; i -= 2 ) sum += num [ i ] - '0' ; return sum ; } bool isValid ( long int number ) { return ( getSize ( number ) >= 13 && getSize ( number ) <= 16 ) && ( prefixMatched ( number , 4 ) || prefixMatched ( number , 5 ) || prefixMatched ( number , 37 ) || prefixMatched ( number , 6 ) ) && ( ( sumOfDoubleEvenPlace ( number ) + sumOfOddPlace ( number ) ) % 10 == 0 ) ; } int main ( ) { long int number = 5196081888500645L ; cout << number << " ▁ is ▁ " << ( isValid ( number ) ? " valid " : " invalid " ) ; return 0 ; }
Find substrings that contain all vowels | C ++ program to find all substring that contain all vowels ; Returns true if x is vowel . ; Function to check whether a character is vowel or not ; Function to FindSubstrings of string ; set < char > hash ; To store vowels ; If current character is vowel then insert into hash , ; If all vowels are present in current substring ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } void FindSubstring ( string str ) { int start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( isVowel ( str [ i ] ) == true ) { hash . insert ( str [ i ] ) ; if ( hash . size ( ) == 5 ) cout << str . substr ( start , i - start + 1 ) << " ▁ " ; } else { start = i + 1 ; hash . clear ( ) ; } } } int main ( ) { string str = " aeoibsddaeiouudb " ; FindSubstring ( str ) ; return 0 ; }
Concatenated string with uncommon characters of two strings | C ++ program Find concatenated string with uncommon characters of given strings ; store all characters of s2 in map ; Find characters of s1 that are not present in s2 and append to result ; Find characters of s2 that are not present in s1 . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string concatenetedString ( string s1 , string s2 ) { unordered_map < char , int > m ; for ( int i = 0 ; i < s2 . size ( ) ; i ++ ) m [ s2 [ i ] ] = 1 ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( m . find ( s1 [ i ] ) == m . end ( ) ) res += s1 [ i ] ; else m [ s1 [ i ] ] = 2 ; } for ( int i = 0 ; i < s2 . size ( ) ; i ++ ) if ( m [ s2 [ i ] ] == 1 ) res += s2 [ i ] ; return res ; } int main ( ) { string s1 = " abcs " ; string s2 = " cxzca " ; cout << concatenetedString ( s1 , s2 ) ; return 0 ; }
Print all Subsequences of String which Start with Vowel and End with Consonant . | C ++ program to generate all the subse - quence starting with vowel and ending with consonant . ; Set to store all the subsequences ; Utility method to check vowel ; Utility method to check consonant ; It computes all the possible substring that starts with vowel and end with consonent ; iterate over the entire string ; test ith character for vowel ; if the ith character is vowel iterate from end of the string and check for consonant . ; test jth character for consonant . ; once we get a consonant add it to the hashset ; drop each character of the substring and recur to generate all subsequence of the substring ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; set < string > st ; bool isVowel ( char c ) { return ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) ; } bool isConsonant ( char c ) { return ! isVowel ( c ) ; } void subsequence ( string str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( isVowel ( str [ i ] ) ) { for ( int j = str . length ( ) - 1 ; j >= i ; j -- ) { if ( isConsonant ( str [ j ] ) ) { string str_sub = str . substr ( i , j + 1 ) ; st . insert ( str_sub ) ; for ( int k = 1 ; k < str_sub . length ( ) - 1 ; k ++ ) { string sb = str_sub ; sb . erase ( sb . begin ( ) + k ) ; subsequence ( sb ) ; } } } } } } int main ( ) { string s = " xabcef " ; subsequence ( s ) ; for ( auto i : st ) cout << i << " ▁ " ; cout << endl ; return 0 ; }
Null Cipher | CPP program to decode NULL CIPHER ; Store the decoded string . ; found variable is used to tell that the encoded encoded character is found in that particular word . ; Set found variable to false whenever we find whitespace , meaning that encoded character for new word is not found ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decode ( string str ) { string res = " " ; bool found = false ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { if ( str [ i ] == ' ▁ ' ) { found = false ; continue ; } if ( ! found ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { res += str [ i ] + 32 ; found = true ; } else if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) { res += str [ i ] ; found = true ; } } } return res ; } int main ( ) { string in ; in = " A ▁ Step ▁ by ▁ Step ▁ Guide ▁ for ▁ Placement ▁ Preparation ▁ by ▁ GeeksforGeeks " ; cout << " Enciphered ▁ Message : ▁ " ; cout << decode ( in ) << endl ; return 0 ; }
Program to count vowels in a string ( Iterative and Recursive ) | Recursive C ++ program to count the total number of vowels using recursion ; Function to check the Vowel ; to count total number of vowel from 0 to n ; Main Calling Function ; string object ; Total numbers of Vowel
#include <iostream> NEW_LINE using namespace std ; bool isVowel ( char ch ) { ch = toupper ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) ; } int countVovels ( string str , int n ) { if ( n == 1 ) return isVowel ( str [ n - 1 ] ) ; return countVovels ( str , n - 1 ) + isVowel ( str [ n - 1 ] ) ; } int main ( ) { string str = " abc ▁ de " ; cout << countVovels ( str , str . length ( ) ) << endl ; return 0 ; }
Smallest window that contains all characters of string itself | C ++ program to find the smallest window containing all characters of a pattern . ; Function to find smallest window containing all distinct characters ; Count all distinct characters . ; Now follow the algorithm discussed in below ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHARS = 256 ; string findSubString ( string str ) { int n = str . length ( ) ; int dist_count = 0 ; unordered_map < int , int > hash_map ; for ( int i = 0 ; i < n ; i ++ ) { hash_map [ str [ i ] ] ++ ; } dist_count = hash_map . size ( ) ; int size = INT_MAX ; string res ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; int visited [ 256 ] = { 0 } ; string sub_str = " " ; for ( int j = i ; j < n ; j ++ ) { if ( visited [ str [ j ] ] == 0 ) { count ++ ; visited [ str [ j ] ] = 1 ; } sub_str += str [ j ] ; if ( count == dist_count ) break ; } if ( sub_str . length ( ) < size && count == dist_count ) { res = sub_str ; size = res . length ( ) ; } } return res ; } int main ( ) { string str = " aabcbcdbca " ; cout << " Smallest ▁ window ▁ containing ▁ all ▁ distinct " " ▁ characters ▁ is : ▁ " << findSubString ( str ) ; return 0 ; }
Generate all rotations of a given string | An efficient C ++ program to print all rotations of a string . ; Print all the rotated string . ; Concatenate str with itself ; Print all substrings of size n . Note that size of temp is 2 n ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRotatedString ( char str [ ] ) { int n = strlen ( str ) ; char temp [ 2 * n + 1 ] ; strcpy ( temp , str ) ; strcat ( temp , str ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j != n ; j ++ ) printf ( " % c " , temp [ i + j ] ) ; printf ( " STRNEWLINE " ) ; } } int main ( ) { char str [ ] = " geeks " ; printRotatedString ( str ) ; return 0 ; }
Check if frequency of all characters can become same by one removal | C ++ program to get same frequency character string by removal of at most one char ; Utility method to get index of character ch in lower alphabet characters ; Returns true if all non - zero elements values are same ; get first non - zero element ; check equality of each element with variable same ; Returns true if we can make all character frequencies same ; fill frequency array ; if all frequencies are same , then return true ; Try decreasing frequency of all character by one and then check all equality of all non - zero frequencies ; Check character only if it occurs in str ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  26 NEW_LINE int getIdx ( char ch ) { return ( ch - ' a ' ) ; } bool allSame ( int freq [ ] , int N ) { int same ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( freq [ i ] > 0 ) { same = freq [ i ] ; break ; } } for ( int j = i + 1 ; j < N ; j ++ ) if ( freq [ j ] > 0 && freq [ j ] != same ) return false ; return true ; } bool possibleSameCharFreqByOneRemoval ( string str ) { int l = str . length ( ) ; int freq [ M ] = { 0 } ; for ( int i = 0 ; i < l ; i ++ ) freq [ getIdx ( str [ i ] ) ] ++ ; if ( allSame ( freq , M ) ) return true ; for ( char c = ' a ' ; c <= ' z ' ; c ++ ) { int i = getIdx ( c ) ; if ( freq [ i ] > 0 ) { freq [ i ] -- ; if ( allSame ( freq , M ) ) return true ; freq [ i ] ++ ; } } return false ; } int main ( ) { string str = " xyyzz " ; if ( possibleSameCharFreqByOneRemoval ( str ) ) cout << " Yes " ; else cout << " No " ; }
Check if a large number is divisible by 11 or not | C ++ program to find if a number is divisible by 11 or not ; Function to find that number divisible by 11 or not ; Compute sum of even and odd digit sums ; When i is even , position of digit is odd ; Check its difference is divisible by 11 or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( string str ) { int n = str . length ( ) ; int oddDigSum = 0 , evenDigSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) oddDigSum += ( str [ i ] - '0' ) ; else evenDigSum += ( str [ i ] - '0' ) ; } return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) ; } int main ( ) { string str = "76945" ; check ( str ) ? cout << " Yes " : cout << " No ▁ " ; return 0 ; }
Hamming Distance between two strings | C ++ program to find hamming distance b / w two string ; function to calculate Hamming distance ; driver code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int hammingDist ( char * str1 , char * str2 ) { int i = 0 , count = 0 ; while ( str1 [ i ] != ' \0' ) { if ( str1 [ i ] != str2 [ i ] ) count ++ ; i ++ ; } return count ; } int main ( ) { char str1 [ ] = " geekspractice " ; char str2 [ ] = " nerdspractise " ; cout << hammingDist ( str1 , str2 ) ; return 0 ; }
Check if two strings are k | C ++ program to check if two strings are k anagram or not . ; Function to check that string is k - anagram or not ; If both strings are not of equal length then return false ; Store the occurrence of all characters in a hash_array ; Count number of characters that are different in both strings ; Return true if count is less than or equal to k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; bool arekAnagrams ( string str1 , string str2 , int k ) { int n = str1 . length ( ) ; if ( str2 . length ( ) != n ) return false ; int count1 [ MAX_CHAR ] = { 0 } ; int count2 [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count1 [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) count2 [ str2 [ i ] - ' a ' ] ++ ; int count = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count1 [ i ] > count2 [ i ] ) count = count + abs ( count1 [ i ] - count2 [ i ] ) ; return ( count <= k ) ; } int main ( ) { string str1 = " anagram " ; string str2 = " grammar " ; int k = 2 ; if ( arekAnagrams ( str1 , str2 , k ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Remove spaces from a given string | An efficient C ++ program to remove all spaces from a string ; Function to remove all spaces from a given string ; To keep track of non - space character count ; Traverse the given string . If current character is not space , then place it at index ' count + + ' ; str [ count ++ ] = str [ i ] ; here count is incremented ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void removeSpaces ( char * str ) { int count = 0 ; for ( int i = 0 ; str [ i ] ; i ++ ) if ( str [ i ] != ' ▁ ' ) str [ count ] = ' \0' ; } int main ( ) { char str [ ] = " g ▁ eeks ▁ for ▁ ge ▁ eeks ▁ " ; removeSpaces ( str ) ; cout << str ; return 0 ; }
Given a binary string , count number of substrings that start and end with 1. | A O ( n ) C ++ program to count number of substrings starting and ending with 1 ; Traverse input string and count of 1 's in it ; Return count of possible pairs among m 1 's ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int countSubStr ( char str [ ] ) { for ( int i = 0 ; str [ i ] != ' \0' ; i ++ ) { if ( str [ i ] == '1' ) m ++ ; } return m * ( m - 1 ) / 2 ; } int main ( ) { char str [ ] = "00100101" ; cout << countSubStr ( str ) ; return 0 ; }
Given a number as a string , find the number of contiguous subsequences which recursively add up to 9 | C ++ program to count substrings with recursive sum equal to 9 ; int count = 0 ; To store result ; Consider every character as beginning of substring ; int sum = number [ i ] - '0' ; sum of digits in current substring ; One by one choose every character as an ending character ; Add current digit to sum , if sum becomes multiple of 5 then increment count . Let us do modular arithmetic to avoid overflow for big strings ; driver program to test above function
#include <iostream> NEW_LINE #include <cstring> NEW_LINE using namespace std ; int count9s ( char number [ ] ) { int n = strlen ( number ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( number [ i ] == '9' ) count ++ ; for ( int j = i + 1 ; j < n ; j ++ ) { sum = ( sum + number [ j ] - '0' ) % 9 ; if ( sum == 0 ) count ++ ; } } return count ; } int main ( ) { cout << count9s ( "4189" ) << endl ; cout << count9s ( "1809" ) ; 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 ; ' arr ' will store all generated codes ; start with one - bit pattern ; Every iteration of this loop generates 2 * i codes from previously generated i codes . ; Enter the prviously generated codes again in arr [ ] in reverse order . Nor arr [ ] has double number of codes . ; append 0 to the first half ; append 1 to the second half ; print contents of arr [ ] ; Driver program to test above function
#include <iostream> NEW_LINE #include <string> NEW_LINE #include <vector> NEW_LINE using namespace std ; void generateGrayarr ( int n ) { if ( n <= 0 ) return ; vector < string > arr ; arr . push_back ( "0" ) ; arr . push_back ( "1" ) ; int i , j ; for ( i = 2 ; i < ( 1 << n ) ; i = i << 1 ) { for ( j = i - 1 ; j >= 0 ; j -- ) arr . push_back ( arr [ j ] ) ; for ( j = 0 ; j < i ; j ++ ) arr [ j ] = "0" + arr [ j ] ; for ( j = i ; j < 2 * i ; j ++ ) arr [ j ] = "1" + arr [ j ] ; } for ( i = 0 ; i < arr . size ( ) ; i ++ ) cout << arr [ i ] << endl ; } int main ( ) { generateGrayarr ( 3 ) ; return 0 ; }
Divide a string in N equal parts | if ( start < str . length ( ) ) cout << endl ; to ignore final new line
#include <iostream> NEW_LINE using namespace std ; void divide ( string str , int n ) { if ( str . length ( ) % n != 0 ) { cout << " Invalid ▁ Input : ▁ String ▁ size " ; cout << " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ; return ; } int parts = str . length ( ) / n ; int start = 0 ; while ( start < str . length ( ) ) { cout << str . substr ( start , parts ) << endl ; start += parts ; } } int main ( ) { string str = " a _ simple _ divide _ string _ quest " ; divide ( str , 4 ) ; }
Least count of words required to construct a target String | C ++ program of the above approach ; countMap [ ] [ ] to store count of characters ; Function to get minimum number of stickers for a particular state ; If an optimal solution is already there , return ; If Target has been constructed update cnt and return ; Update charAvailable [ ] ; Recursizevely function call for ( pos + 1 ) ; Update charAvailable [ ] ; Update charAvailable [ ] ; Recursizeve Call ; Update charAvailable [ ] ; Function to find the minimum number of stickers ; Base Case ; Fill the countMap Array ; Recusizeve function call to get minimum number of stickers ; Driver Code ; Given Input ; Function Call ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > countMap ; int cnt = INT_MAX ; void count ( int curCnt , int pos , vector < int > charAvailable , string target , vector < string > stickers ) { if ( curCnt >= cnt ) return ; int m = stickers . size ( ) ; int n = target . size ( ) ; if ( pos == n ) { cnt = min ( cnt , curCnt ) ; return ; } char c = target [ pos ] ; if ( charAvailable > 0 ) { charAvailable -- ; count ( curCnt , pos + 1 , charAvailable , target , stickers ) ; charAvailable ++ ; } else { for ( int i = 0 ; i < m ; i ++ ) { if ( countMap [ i ] == 0 ) continue ; for ( int j = 0 ; j < 26 ; j ++ ) { charAvailable [ j ] += countMap [ i ] [ j ] ; } count ( curCnt + 1 , pos , charAvailable , target , stickers ) ; for ( int j = 0 ; j < 26 ; j ++ ) { charAvailable [ j ] -= countMap [ i ] [ j ] ; } } } } int minStickers ( vector < string > stickers , string target ) { if ( target == " " ) return -1 ; if ( target . size ( ) == 0 ) return 0 ; if ( stickers . size ( ) == 0 ) return -1 ; int m = stickers . size ( ) ; countMap . resize ( m , vector < int > ( 26 , 0 ) ) ; for ( int i = 0 ; i < stickers . size ( ) ; i ++ ) { string s = stickers [ i ] ; for ( char c : s ) { countMap [ i ] ++ ; } } vector < int > temp ( 26 ) ; count ( 0 , 0 , temp , target , stickers ) ; return cnt == INT_MAX ? -1 : cnt ; } int main ( ) { vector < string > str = { " with " , " example " , " science " } ; string target = " thehat " ; int Result = minStickers ( str , target ) ; cout << Result ; }
Rat in a Maze Problem when movement in all possible directions is allowed | C ++ implementation of the above approach ; Function returns true if the move taken is valid else it will return false . ; Function to print all the possible paths from ( 0 , 0 ) to ( n - 1 , n - 1 ) . ; This will check the initial point ( i . e . ( 0 , 0 ) ) to start the paths . ; If reach the last cell ( n - 1 , n - 1 ) then store the path and return ; Mark the cell as visited ; Check if downward move is valid ; Check if the left move is valid ; Check if the right move is valid ; Check if the upper move is valid ; Mark the cell as unvisited for other possible paths ; Function to store and print all the valid paths ; vector to store all the possible paths ; Call the utility function to find the valid paths ; Print all possible paths ; Driver code
#include <bits/stdc++.h> NEW_LINE #define MAX  5 NEW_LINE using namespace std ; bool isSafe ( int row , int col , int m [ ] [ MAX ] , int n , bool visited [ ] [ MAX ] ) { if ( row == -1 row == n col == -1 col == n visited [ row ] [ col ] m [ row ] [ col ] == 0 ) return false ; return true ; } void printPathUtil ( int row , int col , int m [ ] [ MAX ] , int n , string & path , vector < string > & possiblePaths , bool visited [ ] [ MAX ] ) { if ( row == -1 row == n col == -1 col == n visited [ row ] [ col ] m [ row ] [ col ] == 0 ) return ; if ( row == n - 1 && col == n - 1 ) { possiblePaths . push_back ( path ) ; return ; } visited [ row ] [ col ] = true ; if ( isSafe ( row + 1 , col , m , n , visited ) ) { path . push_back ( ' D ' ) ; printPathUtil ( row + 1 , col , m , n , path , possiblePaths , visited ) ; path . pop_back ( ) ; } if ( isSafe ( row , col - 1 , m , n , visited ) ) { path . push_back ( ' L ' ) ; printPathUtil ( row , col - 1 , m , n , path , possiblePaths , visited ) ; path . pop_back ( ) ; } if ( isSafe ( row , col + 1 , m , n , visited ) ) { path . push_back ( ' R ' ) ; printPathUtil ( row , col + 1 , m , n , path , possiblePaths , visited ) ; path . pop_back ( ) ; } if ( isSafe ( row - 1 , col , m , n , visited ) ) { path . push_back ( ' U ' ) ; printPathUtil ( row - 1 , col , m , n , path , possiblePaths , visited ) ; path . pop_back ( ) ; } visited [ row ] [ col ] = false ; } void printPath ( int m [ MAX ] [ MAX ] , int n ) { vector < string > possiblePaths ; string path ; bool visited [ n ] [ MAX ] ; memset ( visited , false , sizeof ( visited ) ) ; printPathUtil ( 0 , 0 , m , n , path , possiblePaths , visited ) ; for ( int i = 0 ; i < possiblePaths . size ( ) ; i ++ ) cout << possiblePaths [ i ] << " ▁ " ; } int main ( ) { int m [ MAX ] [ MAX ] = { { 1 , 0 , 0 , 0 , 0 } , { 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 , 1 } } ; int n = sizeof ( m ) / sizeof ( m [ 0 ] ) ; printPath ( m , n ) ; return 0 ; }
N Queen in O ( n ) space | CPP code to for n Queen placement ; Function to check queens placement ; Helper Function to check if queen can be placed ; Function to display placed queen ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define breakLine  cout << " NEW_LINE -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - " #define MAX 10 using namespace std ; int arr [ MAX ] , no ; void nQueens ( int k , int n ) ; bool canPlace ( int k , int i ) ; void display ( int n ) ; void nQueens ( int k , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( canPlace ( k , i ) ) { arr [ k ] = i ; if ( k == n ) display ( n ) ; else nQueens ( k + 1 , n ) ; } } } bool canPlace ( int k , int i ) { for ( int j = 1 ; j <= k - 1 ; j ++ ) { if ( arr [ j ] == i || ( abs ( arr [ j ] - i ) == abs ( j - k ) ) ) return false ; } return true ; } void display ( int n ) { breakLine cout < < " Arrangement ▁ No . ▁ " << ++ no ; breakLine for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( arr [ i ] != j ) cout << " TABSYMBOL _ " ; else cout << " TABSYMBOL Q " ; } cout << endl ; } breakLine } int main ( ) { int n = 4 ; nQueens ( 1 , n ) ; return 0 ; }
Numbers that are bitwise AND of at least one non | C ++ implementation of the approach ; Set to store all possible AND values . ; Starting index of the sub - array . ; Ending index of the sub - array . ; AND value is added to the set . ; The set contains all possible AND values .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int A [ ] = { 11 , 15 , 7 , 19 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; unordered_set < int > s ; int i , j , res ; for ( i = 0 ; i < N ; ++ i ) for ( j = i , res = INT_MAX ; j < N ; ++ j ) { res &= A [ j ] ; s . insert ( res ) ; } for ( int i : s ) cout << i << " ▁ " ; return 0 ; }