codeparrot/xlcost-text-to-code huggingface dataset:
Count of values chosen for X such that N is reduced to 0 after given operations | C ++ program for the above approach ; Function to check if the value of X reduces N to 0 or not ; Update the value of N as N - x ; Check if x is a single digit integer ; Function to find the number of values X such that N can be reduced to 0 after performing the given operations ; Number of digits in N ; Stores the count of value of X ; Iterate over all possible value of X ; Check if x follow the conditions ; Return total count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int x , int N ) { while ( 1 ) { N -= x ; if ( x < 10 ) break ; int temp2 = 0 ; while ( x ) { temp2 *= 10 ; temp2 += ( x % 10 ) ; x /= 10 ; } x = temp2 ; } if ( ( x < 10 ) && ( N == 0 ) ) { return 1 ; } return 0 ; } int countNoOfsuchX ( int N ) { int k = ceil ( log10 ( N ) ) ; int count = 1 ; for ( int x = ( N - ( k * ( k + 1 ) * 5 ) ) ; x < N ; x ++ ) { if ( check ( x , N ) ) { count += 1 ; } } return count ; } int main ( ) { int N = 9399 ; cout << countNoOfsuchX ( N ) ; return 0 ; }
Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element | C ++ program for the above approach ; Returns the count of subarrays which contains both the maximum and minimum elements in the given vector ; Initialize the low and high of array ; If current element is less than least element ; If current element is more than highest element ; If current element is equal to low or high then update the pointers ; Update number of subarrays ; Return the result ; Function to find the maximum count of subarrays ; Iterate the array to find the maximum and minimum element ; Vector after removing the minimum element ; Using assignment operator to copy one vector to other ; Vector after removing the maximum element ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long proc ( vector < int > & v ) { long long int n = v . size ( ) ; int low = v [ n - 1 ] , high = v [ n - 1 ] ; long long int p1 = n , p2 = n ; long long ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int x = v [ i ] ; if ( x < low ) { low = x ; ans = 0 ; } else if ( x > high ) { high = x ; ans = 0 ; } if ( x == low ) p1 = i ; if ( x == high ) p2 = i ; ans += n - max ( p1 , p2 ) ; } return ans ; } long long subarray ( vector < int > & v ) { long long int n = v . size ( ) ; if ( n <= 1 ) return n ; long long ans = proc ( v ) ; int low = v [ 0 ] , pos_low = 0 , high = v [ 0 ] , pos_high = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int x = v [ i ] ; if ( x < low ) { low = x ; pos_low = i ; } else if ( x > high ) { high = x ; pos_high = i ; } } vector < int > u ; u = v ; u . erase ( u . begin ( ) + pos_low ) ; ans = max ( ans , proc ( u ) ) ; vector < int > w ; w = v ; w . erase ( w . begin ( ) + pos_high ) ; return max ( ans , proc ( w ) ) ; } int main ( ) { vector < int > v ; v . push_back ( 7 ) ; v . push_back ( 2 ) ; v . push_back ( 5 ) ; v . push_back ( 4 ) ; v . push_back ( 3 ) ; v . push_back ( 1 ) ; cout << subarray ( v ) << endl ; return 0 ; }
Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2 | Function to find the minimum number of increment and decrement of pairs required to make all array elements equal ; Stores the sum of the array ; If sum is not divisible by N ; Update sum ; Store the minimum number of operations ; Iterate while i is less than N ; Add absolute difference of current element with k to ans ; Increase i bye 1 ; Return the value in ans2 ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( vector < int > arr , int N ) { int Sum = 0 ; for ( auto i : arr ) Sum += i ; if ( Sum % N ) return -1 ; int k = Sum / N ; int ans = 0 ; int i = 0 ; while ( i < N ) { ans = ans + abs ( k - arr [ i ] ) ; i += 1 ; } return ans / 2 ; } int main ( ) { vector < int > arr = { 5 , 4 , 1 , 10 } ; int N = arr . size ( ) ; cout << ( find ( arr , N ) ) ; }
Convert A into B by incrementing or decrementing 1 , 2 , or 5 any number of times | C ++ program for the above approach ; Function to find minimum number of moves required to convert A into B ; Stores the minimum number of moves required ; Stores the absolute difference ; FInd the number of moves ; Return cnt ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSteps ( int a , int b ) { int cnt = 0 ; a = abs ( a - b ) ; cnt = ( a / 5 ) + ( a % 5 ) / 2 + ( a % 5 ) % 2 ; return cnt ; } int main ( ) { int A = 3 , B = 9 ; cout << minimumSteps ( A , B ) ; return 0 ; }
Minimum distance between duplicates in a String | C ++ Program to find the minimum distance between two repeating characters in a string using two pointers technique ; This function is used to find minimum distance between any two repeating characters using two - pointers and hashing technique ; hash array to store character 's last index ; Traverse through the string ; If the character is present in visited array find if its forming minimum distance ; update current character 's last index ; Return minimum distance found , else - 1 ; Driver code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int shortestDistance ( string s , int n ) { vector < int > visited ( 128 , -1 ) ; int ans = INT_MAX ; for ( int right = 0 ; right < n ; right ++ ) { char c = s [ right ] ; int left = visited ; if ( left != -1 ) ans = min ( ans , right - left - 1 ) ; visited = right ; } return ans == INT_MAX ? -1 : ans ; } int main ( ) { string s = " geeksforgeeks " ; int n = 13 ; cout << ( shortestDistance ( s , n ) ) ; }
Lexicographically smallest string which is not a subsequence of given string | C ++ program for the above approach ; Function to find lexicographically smallest string that is not a subsequence of S ; variable to store frequency of ' a ' ; calculate frequency of ' a ' ; Initialize string consisting of freq number of ' a ' ; change the last digit to ' b ' ; add another ' a ' to the string ; return tha answer string ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string smallestNonSubsequence ( string S , int N ) { int freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( S [ i ] == ' a ' ) freq ++ ; string ans ( freq , ' a ' ) ; if ( freq == N ) ans [ freq - 1 ] = ' b ' ; else ans += ' a ' ; return ans ; } int main ( ) { string S = " abcdefghijklmnopqrstuvwxyz " ; int N = S . length ( ) ; cout << smallestNonSubsequence ( S , N ) << endl ; return 0 ; }
Print all numbers that can be obtained by adding A or B to N exactly M times | C ++ program for the above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be obtained ; If A and B are equal , then only one number can be obtained , i . e . N + M * A ; For finding others numbers , subtract A from number once and add B to number once ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void possibleNumbers ( int N , int M , int A , int B ) { if ( A > B ) { swap ( A , B ) ; } int number = N + M * A ; cout << number << " ▁ " ; if ( A != B ) { for ( int i = 0 ; i < M ; i ++ ) { number = number - A + B ; cout << number << " ▁ " ; } } } int main ( ) { int N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ; return 0 ; }
Maximize number of circular buildings that can be covered by L length wire | C ++ program for the above approach ; Function to find the maximum number of buildings covered ; Store the current sum ; Traverse the array ; Add the length of wire required for current building to cur_sum ; Add extra unit distance 1 ; If curr_sum <= length of wire increment count by 1 ; If curr_sum > length of wire increment start by 1 and decrement count by 1 and update the new curr_sum ; Update the max_count ; Return the max_count ; Driver Code ; Given Input ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const double Pi = 3.141592 ; int MaxBuildingsCovered ( int arr [ ] , int N , int L ) { double curr_sum = 0 ; int start = 0 , curr_count = 0 , max_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { curr_sum = curr_sum + ( ( double ) arr [ i ] * Pi ) ; if ( i != 0 ) curr_sum += 1 ; if ( curr_sum <= L ) { curr_count ++ ; } else if ( curr_sum > L ) { curr_sum = curr_sum - ( ( double ) arr [ start ] * Pi ) ; curr_sum -= 1 ; start ++ ; curr_count -- ; } max_count = max ( curr_count , max_count ) ; } return max_count ; } int main ( ) { int arr [ ] = { 4 , 1 , 6 , 2 } ; int L = 24 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MaxBuildingsCovered ( arr , N , L ) ; return 0 ; }
Count numbers less than N whose Bitwise AND with N is zero | C ++ program for the above approach ; Function to count number of unset bits in the integer N ; Stores the number of unset bits in N ; Check if N is even ; Increment the value of c ; Right shift N by 1 ; Return the value of count of unset bits ; Function to count numbers whose Bitwise AND with N equal to 0 ; Stores the number of unset bits in N ; Print the value of 2 to the power of unsetBits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countUnsetBits ( int N ) { int c = 0 ; while ( N ) { if ( N % 2 == 0 ) { c += 1 ; } N = N >> 1 ; } return c ; } void countBitwiseZero ( int N ) { int unsetBits = countUnsetBits ( N ) ; cout << ( 1 << unsetBits ) ; } int main ( ) { int N = 9 ; countBitwiseZero ( N ) ; return 0 ; }
Check if a Binary String can be split into disjoint subsequences which are equal to "010" | C ++ program for the above approach ; Function to check if the given string can be partitioned into a number of subsequences all of which are equal to "010" ; Store the size of the string ; Store the count of 0 s and 1 s ; Traverse the given string in the forward direction ; If the character is '0' , increment count_0 by 1 ; If the character is '1' increment count_1 by 1 ; If at any point , count_1 > count_0 , return false ; If count_0 is not equal to twice count_1 , return false ; Reset the value of count_0 and count_1 ; Traverse the string in the reverse direction ; If the character is '0' increment count_0 ; If the character is '1' increment count_1 ; If count_1 > count_0 , return false ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( string s ) { int n = s . size ( ) ; int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } if ( count_0 != ( 2 * count_1 ) ) return false ; count_0 = 0 , count_1 = 0 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } return true ; } int main ( ) { string s = "010100" ; if ( isPossible ( s ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum number of bricks that can be intersected | C ++ program for the above approach ; Function to find a line across a wall such that it intersects minimum number of bricks ; Declare a hashmap ; Store the maximum number of brick ending a point on x - axis ; Iterate over all the rows ; Initialize width as 0 ; Iterate over individual bricks ; Add the width of the current brick to the total width ; Increment number of bricks ending at this width position ; Update the variable , res ; Print the answer ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void leastBricks ( vector < vector < int > > wall ) { unordered_map < int , int > map ; int res = 0 ; for ( vector < int > list : wall ) { int width = 0 ; for ( int i = 0 ; i < list . size ( ) - 1 ; i ++ ) { width += list [ i ] ; map [ width ] ++ ; res = max ( res , map [ width ] ) ; } } cout << wall . size ( ) - res ; } int main ( ) { vector < vector < int > > arr { { 1 , 2 , 2 , 1 } , { 3 , 1 , 2 } , { 1 , 3 , 2 } , { 2 , 4 } , { 3 , 1 , 2 } , { 1 , 3 , 1 , 1 } } ; leastBricks ( arr ) ; return 0 ; }
Find an N | C ++ implementation of the above approach ; Function to print the required permutation ; Driver code
#include <iostream> NEW_LINE using namespace std ; void findPermutation ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) cout << i << " ▁ " ; cout << endl ; } int main ( ) { int N = 5 ; findPermutation ( N ) ; return 0 ; }
Maximize count of distinct profits possible by N transactions | C ++ program for the above approach ; Function to count distinct profits possible ; Stores the minimum total profit ; Stores the maximum total profit ; Return count of distinct profits ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfWays ( int N , int X , int Y ) { int S1 = ( N - 1 ) * X + Y ; int S2 = ( N - 1 ) * Y + X ; return ( S2 - S1 + 1 ) ; } int main ( ) { int N = 3 ; int X = 13 ; int Y = 15 ; cout << numberOfWays ( N , X , Y ) ; return 0 ; }
Minimum number of operations required to make a permutation of first N natural numbers equal | C ++ program for the above approach ; Function to find the minimum number of operations required to make all array elements equal ; Store the count of operations required ; Increment by K - 1 , as the last element will be used again for the next K consecutive elements ; Increment count by 1 ; Return the result ; Driver Code ; Given Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumOperations ( int A [ ] , int N , int K ) { int Count = 0 ; int i = 0 ; while ( i < N - 1 ) { i = i + K - 1 ; Count ++ ; } return Count ; } int main ( ) { int A [ ] = { 5 , 4 , 3 , 1 , 2 } ; int K = 3 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << MinimumOperations ( A , N , K ) << endl ; return 0 ; }
Consecutive Prime numbers greater than equal to given number . | C ++ program for the above approach ; Function to check prime . ; It means it is not a prime ; No factor other than 1 therefore prime number ; Function to find out the required consecutive primes . ; Finding first prime just less than sqrt ( n ) . ; Finding prime just greater than sqrt ( n ) . ; Product of both prime is greater than n then print it ; Finding prime greater than second ; Driver Program
#include <bits/stdc++.h> NEW_LINE #define endl  " NEW_LINE " #define ll  long long NEW_LINE using namespace std ; bool is_prime ( ll n ) { if ( n == 1 ) { return false ; } for ( ll i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } void consecutive_primes ( int n ) { ll first = -1 , second = -1 ; for ( ll i = sqrt ( n ) ; i >= 2 ; i -- ) { if ( is_prime ( i ) ) { first = i ; break ; } } for ( ll i = sqrt ( n ) + 1 ; i <= n / 2 ; i ++ ) { if ( is_prime ( i ) ) { second = i ; break ; } } if ( first * second >= n ) { cout << first << " ▁ " << second << endl ; } else { for ( ll i = second + 1 ; i <= n ; i ++ ) { if ( is_prime ( i ) ) { cout << second << " ▁ " << i << endl ; return ; } } } } int main ( ) { ll n = 14 ; consecutive_primes ( n ) ; return 0 ; }
Minimum decrements required to make all pairs of adjacent matrix elements distinct | C ++ program for the above approach ; Function to count minimum number of operations required ; Case 1 : ; Case 2 : ; Print the minimum number of operations required ; Driver Code ; The given matrix ; Function Call to count the minimum number of decrements required
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int n = 3 ; const int m = 3 ; void countDecrements ( long long arr [ ] [ m ] ) { int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) count_1 ++ ; if ( 1 - ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) count_2 ++ ; } } cout << min ( count_1 , count_2 ) ; } int main ( ) { long long arr [ ] [ m ] = { { 1 , 2 , 3 } , { 1 , 2 , 3 } , { 1 , 2 , 3 } } ; countDecrements ( arr ) ; return 0 ; }
Check if X can be converted to Y by converting to 3 * ( X / 2 ) or X | C ++ program for the above approach ; Function to check if X can be made equal to Y by converting X to ( 3 * X / 2 ) or ( X - 1 ) ; Conditions for possible conversion ; Otherwise , conversion is not possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int X , int Y ) { if ( X > 3 ) { cout << " Yes " ; } else if ( X == 1 and Y == 1 ) { cout << " Yes " ; } else if ( X == 2 and Y <= 3 ) { cout << " Yes " ; } else if ( X == 3 and Y <= 3 ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int X = 6 , Y = 8 ; check ( X , Y ) ; return 0 ; }
Count distinct sum of pairs possible from a given range | C ++ program for the above approach ; Function to count distinct sum of pairs possible from the range [ L , R ] ; Return the count of distinct sum of pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distIntegers ( int L , int R ) { return 2 * R - 2 * L + 1 ; } int main ( ) { int L = 3 , R = 8 ; cout << distIntegers ( L , R ) ; return 0 ; }
Count subarrays having even Bitwise XOR | C ++ program for the above approach ; Function to count subarrays having even Bitwise XOR ; Store the required result ; Stores count of subarrays with even and odd XOR values ; Stores Bitwise XOR of current subarray ; Traverse the array ; Update current Xor ; If XOR is even ; Update ans ; Increment count of subarrays with even XOR ; Otherwise , increment count of subarrays with odd XOR ; Print the result ; Driver Code ; Given array ; Stores the size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void evenXorSubarray ( int arr [ ] , int n ) { int ans = 0 ; int freq [ ] = { 0 , 0 } ; int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { XOR = XOR ^ arr [ i ] ; if ( XOR % 2 == 0 ) { ans += freq [ 0 ] + 1 ; freq [ 0 ] ++ ; } else { ans += freq [ 1 ] ; freq [ 1 ] ++ ; } } cout << ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; evenXorSubarray ( arr , N ) ; return 0 ; }
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | C ++ program for the above approach ; Function to count the occurrences of X in the generated square matrix ; Stores the required result ; Iterate upto square root of X ; Check if i divides X ; Store the quotient obtained on dividing X by i ; If both the numbers fall in the range , update count ; Return the result ; Driver code ; Given N and X ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOccurrences ( int N , int X ) { int count = 0 ; for ( int i = 1 ; i < sqrt ( X ) ; i ++ ) { if ( X % i == 0 ) { int a = i ; int b = X / i ; if ( a <= N && b <= N ) { if ( a == b ) count += 1 ; else count += 2 ; } } } return count ; } int main ( ) { int N = 7 ; int X = 12 ; cout << countOccurrences ( N , X ) ; return 0 ; }
Maximize array product by changing any array element arr [ i ] to ( | C ++ program for the above approach ; Function to find array with maximum product by changing array elements to ( - 1 ) arr [ i ] - 1 ; Traverse the array ; Applying the operation on all the positive elements ; Stores maximum element in array ; Stores index of maximum element ; Check if current element is greater than the maximum element ; Find index of the maximum element ; Perform the operation on the maximum element ; Print the elements of the array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findArrayWithMaxProduct ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] >= 0 ) { arr [ i ] = - arr [ i ] - 1 ; } if ( N % 2 == 1 ) { int max_element = -1 ; int index = -1 ; for ( int i = 0 ; i < N ; i ++ ) if ( abs ( arr [ i ] ) > max_element ) { max_element = abs ( arr [ i ] ) ; index = i ; } arr [ index ] = - arr [ index ] - 1 ; } for ( int i = 0 ; i < N ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; } int main ( ) { int arr [ ] = { -3 , 0 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findArrayWithMaxProduct ( arr , N ) ; return 0 ; }
Lexicographically smallest permutation of first N natural numbers having K perfect indices | C ++ program for the above approach ; Function to print the lexicographically smallest permutation with K perfect indices ; Iterator to traverse the array ; Traverse first K array indices ; Traverse remaining indices ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void findPerfectIndex ( int N , int K ) { int i = 0 ; for ( ; i < K ; i ++ ) { cout << ( N - K + 1 ) + i << " ▁ " ; } for ( ; i < N ; i ++ ) { cout << i - K + 1 << " ▁ " ; } } int main ( ) { int N = 10 , K = 3 ; findPerfectIndex ( N , K ) ; return 0 ; }
Count pairs having distinct sum from a given range | C ++ program for the above approach ; Function to count pairs made up of elements from the range [ L , R ] having distinct sum ; Stores the least sum which can be formed by the pairs ; Stores the highest sum which can be formed by the pairs ; Stores the count of pairs having distinct sum ; Print the count of pairs ; Driver Code ; Function call to count the number of pairs having distinct sum in the range [ L , R ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; long countPairs ( long L , long R ) { long firstNum = 2 * L ; long lastNum = 2 * R ; long Cntpairs = lastNum - firstNum + 1 ; cout << Cntpairs ; } int main ( ) { long L = 2 , R = 3 ; countPairs ( L , R ) ; return 0 ; }
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | C ++ program for the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Total number of pairs possible from the array ; Stores frequency of each array element ; Traverse the array A [ ] ; Increment ump [ A [ i ] ] by 1 ; Traverse the Hashmap ump ; Subtract those pairs ( i , j ) from count which has the same element on index i and j ( i < j ) ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int A [ ] , int n ) { long long count = ( n * ( n - 1 ) ) / 2 ; unordered_map < long long , long long > ump ; for ( int i = 0 ; i < n ; i ++ ) { ump [ A [ i ] ] ++ ; } for ( auto it = ump . begin ( ) ; it != ump . end ( ) ; ++ it ) { long long c = it -> second ; count = count - ( c * ( c - 1 ) ) / 2 ; } cout << count ; } int main ( ) { int A [ ] = { 1 , 4 , 7 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; countPairs ( A , N ) ; return 0 ; }
Construct a K | C ++ program for the above approach ; To construct the required binary string ; Function to construct binary string according to the given conditions ; Initialize with 1 ; Traverse the array ; To check if the i - th integer needs to be considered or not ; Print the binary string ; Driver Code ; Given array ; Size of the array ; Given K
#include <bits/stdc++.h> NEW_LINE using namespace std ; bitset < 100003 > bit ; void constructBinaryString ( int arr [ ] , int N , int K ) { bit [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { bit |= bit << arr [ i ] ; } for ( int i = 1 ; i <= K ; i ++ ) { cout << bit [ i ] ; } } int main ( ) { int arr [ ] = { 1 , 6 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 8 ; constructBinaryString ( arr , N , K ) ; }
Check if a Rook can reach the given destination in a single move | C ++ program to implement for the above approach ; Function to check if it is possible to reach destination in a single move by a rook ; Driver Code ; Given arrays
#include <bits/stdc++.h> NEW_LINE using namespace std ; string check ( int current_row , int current_col , int destination_row , int destination_col ) { if ( current_row == destination_row ) return " POSSIBLE " ; else if ( current_col == destination_col ) return " POSSIBLE " ; else return " NOT ▁ POSSIBLE " ; } int main ( ) { int current_row = 8 ; int current_col = 8 ; int destination_row = 8 ; int destination_col = 4 ; string output = check ( current_row , current_col , destination_row , destination_col ) ; cout << output ; return 0 ; }