codeparrot/xlcost-text-to-code huggingface dataset:
Maximize absolute displacement from origin by moving on X | C ++ program for the above approach ; Recursive function to find the maximum absolute displacement from origin by performing the given set of moves ; If i is equal to N ; If S [ i ] is equal to ' L ' ; If S [ i ] is equal to ' R ' ; If S [ i ] is equal to ' ? ' ; Function to find the maximum absolute displacement from the origin ; Return the maximum absolute displacement ; Driver Code ; Input ; Function call
#include <iostream> NEW_LINE using namespace std ; int DistRecursion ( string S , int i , int dist ) { if ( i == S . length ( ) ) return abs ( dist ) ; if ( S [ i ] == ' L ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( S [ i ] == ' R ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } int maxDistance ( string S ) { return DistRecursion ( S , 0 , 0 ) ; } int main ( ) { string S = " ? RRR ? " ; cout << maxDistance ( S ) ; return 0 ; }
Estimating the value of Pi using Monte Carlo | Parallel Computing Method | C ++ program for the above approach ; Function to find estimated value of PI using Monte Carlo algorithm ; Stores X and Y coordinates of a random point ; Stores distance of a random point from origin ; Stores number of points lying inside circle ; Stores number of points lying inside square ; Parallel calculation of random points lying inside a circle ; Initializes random points with a seed ; Finds random X co - ordinate ; Finds random X co - ordinate ; Finds the square of distance of point ( x , y ) from origin ; If d is less than or equal to 1 ; Increment pCircle by 1 ; Increment pSquare by 1 ; Stores the estimated value of PI ; Prints the value in pi ; Driver Code ; Input ; Function call
#include <iostream> NEW_LINE using namespace std ; void monteCarlo ( int N , int K ) { double x , y ; double d ; int pCircle = 0 ; int pSquare = 0 ; int i = 0 ; #pragma  omp parallel firstprivate(x, y, d, i) reduction(+ : pCircle, pSquare) num_threads(K) NEW_LINE { srand48 ( ( int ) time ( NULL ) ) ; for ( i = 0 ; i < N ; i ++ ) { x = ( double ) drand48 ( ) ; y = ( double ) drand48 ( ) ; d = ( ( x * x ) + ( y * y ) ) ; if ( d <= 1 ) { pCircle ++ ; } pSquare ++ ; } } double pi = 4.0 * ( ( double ) pCircle / ( double ) ( pSquare ) ) ; cout << " Final ▁ Estimation ▁ of ▁ Pi ▁ = ▁ " << pi ; } int main ( ) { int N = 100000 ; int K = 8 ; monteCarlo ( N , K ) ; }
Count of pairs in range [ P , Q ] with numbers as multiple of R and their product lie in range [ P * Q / 4 , P * Q ] | C ++ program for the above approach ; Function to find the number of pairs such that both the elements are in the range [ P , Q ] and the numbers should be multiple of R , and the product of numbers should lie in the range [ P * Q / 4 , P * Q ] ; Store multiple of r in range of [ P , Q ] ; Itearte in the range [ p , q ] ; Vector to store pair of answer ; Iterate through the vector v ; Iterate in the range [ i + 1 , v . size ( ) - 1 ] ; If pair follow this condition insert the pair in vector ans ; If no pair satisfy the conditions , print - 1 ; Print the pairs which satisfy the given condition ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPairs ( int p , int q , int r ) { vector < int > v ; for ( int i = p ; i <= q ; i ++ ) { if ( i % r == 0 ) { v . push_back ( i ) ; } } vector < pair < int , int > > ans ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) { if ( v [ i ] * v [ j ] >= p * q / 4 && v [ i ] * v [ j ] <= p * q ) { ans . push_back ( { v [ i ] , v [ j ] } ) ; } } } if ( ans . size ( ) == 0 ) { cout << -1 << endl ; } else { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] . first << " ▁ " << ans [ i ] . second << endl ; } } } int main ( ) { int p = 14 , q = 30 , r = 5 ; findPairs ( p , q , r ) ; return 0 ; }
Print rectangular pattern with given center | C ++ program for the above approach ; Function to print the matrix filled with rectangle pattern having center coordinates are c1 , c2 ; Iterate in the range [ 0 , n - 1 ] ; Iterate in the range [ 0 , n - 1 ] ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printRectPattern ( int c1 , int c2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { cout << ( max ( abs ( c1 - i ) , abs ( c2 - j ) ) ) << " ▁ " ; } cout << endl ; } } int main ( ) { int c1 = 2 ; int c2 = 2 ; int n = 5 ; printRectPattern ( c1 , c2 , n ) ; return 0 ; }
Count of numbers in range [ L , R ] with LSB as 0 in their Binary representation | C ++ implementation of the approach ; Function to return the count of required numbers ; If rightmost bit is 0 ; Return the required count ; Driver code ; Call function countNumbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int l , int r ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( ( i & 1 ) == 0 ) { count ++ ; } } return count ; } int main ( ) { int l = 10 , r = 20 ; cout << countNumbers ( l , r ) ; return 0 ; }
Count of numbers in range [ L , R ] with LSB as 0 in their Binary representation | C ++ implementation of the approach ; Function to return the count of required numbers ; Count of numbers in range which are divisible by 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int l , int r ) { return ( ( r / 2 ) - ( l - 1 ) / 2 ) ; } int main ( ) { int l = 10 , r = 20 ; cout << countNumbers ( l , r ) ; return 0 ; }
Length of longest Fibonacci subarray | C ++ program for the above approach ; A utility function that returns true if x is perfect square ; Returns true if n is a Fibinacci Number , else false ; Here n is Fibinac ci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perferct square ; Function to find the length of the largest sub - array of an array every element of whose is a Fibonacci number ; Traverse the array arr [ ] ; Check if arr [ i ] is a Fibonacci number ; Stores the maximum length of the Fibonacci number subarray ; Finally , return the maximum length ; Driver code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int x ) { int s = sqrt ( x ) ; return ( s * s == x ) ; } bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } int contiguousFibonacciNumber ( int arr [ ] , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isFibonacci ( arr [ i ] ) ) { current_length ++ ; } else { current_length = 0 ; } max_length = max ( max_length , current_length ) ; } return max_length ; } int main ( ) { int arr [ ] = { 11 , 8 , 21 , 5 , 3 , 28 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << contiguousFibonacciNumber ( arr , n ) ; return 0 ; }
Minimum number of flips required such that a Binary Matrix doesn 't contain any path from the top left to the bottom right consisting only of 0s | C ++ program for the above approach ; The four direction coordinates changes from the current cell ; Function that returns true if there exists any path from the top - left to the bottom - right cell of 0 s ; If the bottom - right cell is reached ; Update the cell to 1 ; Traverse in all four directions ; Find the new coordinates ; If the new cell is valid ; Recursively call DFS ; If path exists , then return true ; Return false , if there doesn 't  exists any such path ; Function to flip the minimum number of cells such that there doesn 't exists any such path from (0, 0) to (N - 1, M - 1) cell consisting of 0s ; Case 1 : If no such path exists already ; Case 2 : If there exists only one path ; Case 3 : If there exists two - path ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int direction [ ] [ 2 ] = { { -1 , 0 } , { 0 , 1 } , { 0 , -1 } , { 1 , 0 } } ; bool dfs ( vector < vector < int > > & matrix , int i , int j , int N , int M ) { if ( i == N - 1 and j == M - 1 ) { return true ; } matrix [ i ] [ j ] = 1 ; for ( int k = 0 ; k < 4 ; k ++ ) { int newX = i + direction [ k ] [ 0 ] ; int newY = j + direction [ k ] [ 1 ] ; if ( newX >= 0 and newX < N and newY > = 0 and newY < M and matrix [ newX ] [ newY ] == 0 ) { if ( dfs ( matrix , newX , newY , N , M ) ) { return true ; } } } return false ; } int solve ( vector < vector < int > > & matrix ) { int N = matrix . size ( ) ; int M = matrix [ 0 ] . size ( ) ; if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 0 ; } if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 1 ; } return 2 ; } int main ( ) { vector < vector < int > > mat = { { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; cout << solve ( mat ) ; return 0 ; }
Maximize product of subarray sum with its maximum element | C ++ program for the above approach ; Function to find the maximum product of the sum of the subarray with its maximum element ; Traverse the array arr [ ] ; Increment currSum by a [ i ] ; Maximize the value of currMax ; Maximize the value of largestSum ; If currSum goes less than 0 then update currSum = 0 ; Return the resultant value ; Function to maximize the product of the sum of the subarray with its maximum element ; Find the largest sum of the subarray ; Multiply each array element with - 1 ; Find the largest sum of the subarray with negation of all array element ; Return the resultant maximum value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Kadane ( int arr [ ] , int n ) { int largestSum = 0 , currMax = 0 ; int currSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currSum += arr [ i ] ; currMax = max ( currMax , arr [ i ] ) ; largestSum = max ( largestSum , currMax * currSum ) ; if ( currSum < 0 ) { currMax = 0 ; currSum = 0 ; } } return largestSum ; } int maximumWeight ( int arr [ ] , int n ) { int largestSum = Kadane ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = - arr [ i ] ; } largestSum = max ( largestSum , Kadane ( arr , n ) ) ; return largestSum ; } int main ( ) { int arr [ ] = { 2 , -3 , 8 , -2 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumWeight ( arr , N ) ; return 0 ; }
Partition an array into two subsets with equal count of unique elements | C ++ program for the above approach ; Function to partition the array into two subsets such that count of unique elements in both subsets is the same ; Stores the subset number for each array elements ; Stores the count of unique array elements ; Stores the frequency of elements ; Traverse the array ; Count of elements having a frequency of 1 ; Check if there exists any element with frequency > 2 ; Count of elements needed to have frequency exactly 1 in each subset ; Initialize all values in the / array ans [ ] as 1 ; Traverse the array ans [ ] ; This array element is a part of first subset ; Half array elements with frequency 1 are part of the second subset ; If count of elements is exactly 1 are odd and has no element with frequency > 2 ; If count of elements that occurs exactly once are even ; Print the result ; If the count of elements has exactly 1 frequency are odd and there is an element with frequency greater than 2 ; Print the result ; Driver Codea
#include <bits/stdc++.h> NEW_LINE using namespace std ; void arrayPartition ( int a [ ] , int n ) { int ans [ n ] ; int cnt = 0 ; int ind , flag = 0 ; map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ a [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mp [ a [ i ] ] == 1 ) cnt ++ ; if ( mp [ a [ i ] ] > 2 && flag == 0 ) { flag = 1 ; ind = i ; } } int p = ( cnt + 1 ) / 2 ; int ans1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp [ a [ i ] ] == 1 && ans1 < p ) { ans [ i ] = 1 ; ans1 ++ ; } else if ( mp [ a [ i ] ] == 1 ) { ans [ i ] = 2 ; } } if ( cnt % 2 == 1 && flag == 0 ) { cout << -1 << endl ; return ; } if ( cnt % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } else { for ( int i = 0 ; i < n ; i ++ ) { if ( ind == i ) cout << 2 << " ▁ " ; else cout << ans [ i ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 3 , 4 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; arrayPartition ( arr , N ) ; return 0 ; }
Modify a given array by replacing each element with the sum or product of their digits based on a given condition | C ++ program for the above approach ; Function to modify the given array as per the given conditions ; Traverse the given array arr [ ] ; Initialize the count of even and odd digits ; Initialize temp with the current array element ; For count the number of even digits ; Increment the odd count ; Otherwise ; Divide temp by 10 ; Performe addition ; Performe multiplication ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void evenOdd ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int even_digits = 0 ; int odd_digits = 0 ; int temp = arr [ i ] ; while ( temp ) { if ( ( temp % 10 ) & 1 ) odd_digits ++ ; else even_digits ++ ; temp /= 10 ; } if ( even_digits > odd_digits ) { int res = 0 ; while ( arr [ i ] ) { res += arr [ i ] % 10 ; arr [ i ] /= 10 ; } cout << res << " ▁ " ; } else if ( odd_digits > even_digits ) { int res = 1 ; while ( arr [ i ] ) { res *= arr [ i ] % 10 ; arr [ i ] /= 10 ; } cout << res << " ▁ " ; } else cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 113 , 141 , 214 , 3186 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; evenOdd ( arr , N ) ; return 0 ; }
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers | C ++ implementation for the above approach ; Function to print the prime factorization of the product of all numbers <= N that are even and can be expressed as a product of two distinct prime numbers ; sieve of Eratosthenese ; Store prime numbers in the range [ 3 , N / 2 ] ; print the coefficient of 2 in the prime factorization ; print the coefficients of other primes ; Driver code ; Input ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; void primeFactorization ( int N ) { int sieve [ N / 2 + 1 ] = { 0 } ; for ( int i = 2 ; i <= N / 2 ; i ++ ) { if ( sieve [ i ] == 0 ) { for ( int j = i * i ; j <= N / 2 ; j += i ) { sieve [ j ] = 1 ; } } } vector < int > prime ; for ( int i = 3 ; i <= N / 2 ; i ++ ) if ( sieve [ i ] == 0 ) prime . push_back ( i ) ; int x = prime . size ( ) ; cout << "2 - > " << x << endl ; for ( int i : prime ) cout << i << " - > 1" << endl ; } int main ( ) { int N = 18 ; primeFactorization ( N ) ; return 0 ; }
Sum of the first M elements of Array formed by infinitely concatenating given array | C ++ program for the above approach ; Function to find the sum of first M numbers formed by the infinite concatenation of the array A [ ] ; Stores the resultant sum ; Iterate over the range [ 0 , M - 1 ] ; Add the value A [ i % N ] to sum ; Return the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfFirstM ( int A [ ] , int N , int M ) { int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { sum = sum + A [ i % N ] ; } return sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int M = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumOfFirstM ( arr , N , M ) ; return 0 ; }
Check if it is possible to reach M from 0 by given paths | C ++ program for the above approach ; Function to check if it is possible to reach M from 0 ; Stores the farther point that can reach from 1 point ; Stores the farthest point it can go for each index i ; Initialize rightMost [ i ] with 0 ; Traverse the array ; Update the rightMost position reached from a1 ; Find the farthest point it can reach from i ; If point < can be reached ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canReach0toM ( int a [ ] [ 2 ] , int n , int m ) { int rightMost [ m + 1 ] ; int dp [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { rightMost [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int a1 = a [ i ] [ 0 ] ; int b1 = a [ i ] [ 1 ] ; rightMost [ a1 ] = max ( rightMost [ a1 ] , b1 ) ; } for ( int i = m ; i >= 0 ; i -- ) { dp [ i ] = i ; for ( int j = min ( m , rightMost [ i ] ) ; j > i ; j -- ) { dp [ i ] = max ( dp [ i ] , dp [ j ] ) ; } } if ( dp [ 0 ] >= m ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int arr [ ] [ 2 ] = { { 0 , 2 } , { 2 , 2 } , { 2 , 5 } , { 4 , 5 } } ; int M = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; canReach0toM ( arr , N , M ) ; return 0 ; }
Count of non co | C ++ program for the above approach ; Recursive function to return gcd of two numbers ; Function to count the number of non co - prime pairs for each query ; Traverse the array arr [ ] ; Stores the count of non co - prime pairs ; Iterate over the range [ 1 , x ] ; Iterate over the range [ x , y ] ; If gcd of current pair is greater than 1 ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void countPairs ( int * arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int x = 1 ; x <= arr [ i ] ; x ++ ) { for ( int y = x ; y <= arr [ i ] ; y ++ ) { if ( gcd ( x , y ) > 1 ) count ++ ; } } cout << count << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , 10 , 20 } ; int N = 3 ; countPairs ( arr , N ) ; return 0 ; }
Count of non co | C ++ program for the above approach ; Auxiliary function to pre - compute the answer for each array ; Iterate over the range [ 1 , MAX ] ; Iterate over the range [ 1 , MAX ] ; If the number is prime ; Subtract the number of pairs which has i as one of their factors ; Iterate over the range [ 1 , MAX ] ; Function to count the number of non co - prime pairs for each query ; The i - th element stores the count of element that are co - prime with i ; Stores the resulting array ; Function Call ; Traverse the array arr [ ] ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1005 ; void preCalculate ( vector < int > & phi , vector < int > & ans ) { phi [ 0 ] = 0 ; phi [ 1 ] = 1 ; for ( int i = 2 ; i <= MAX ; i ++ ) phi [ i ] = i ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( phi [ i ] == i ) { for ( int j = i ; j <= MAX ; j += i ) phi [ j ] -= ( phi [ j ] / i ) ; } } for ( int i = 1 ; i <= MAX ; i ++ ) ans [ i ] = ans [ i - 1 ] + ( i - phi [ i ] ) ; } void countPairs ( int * arr , int N ) { vector < int > phi ( 1e5 , 0 ) ; vector < int > ans ( 1e5 , 0 ) ; preCalculate ( phi , ans ) ; for ( int i = 0 ; i < N ; ++ i ) { cout << ans [ arr [ i ] ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , 10 , 20 } ; int N = 3 ; countPairs ( arr , N ) ; }
Maximum number of multiplication by 3 or division by 2 operations possible on an array | C ++ program for the above approach ; Function to count maximum number of multiplication by 3 or division by 2 operations that can be performed ; Stores the maximum number of operations possible ; Traverse the array arr [ ] ; Iterate until arr [ i ] is even ; Increment count by 1 ; Update arr [ i ] ; Return the value of Count as the answer ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumTurns ( int arr [ ] , int N ) { int Count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { while ( arr [ i ] % 2 == 0 ) { Count ++ ; arr [ i ] = arr [ i ] / 2 ; } } return Count ; } int main ( ) { int arr [ ] = { 5 , 2 , 4 } ; int M = 3 , K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumTurns ( arr , N ) ; return 0 ; }
Distribute the white and black objects into maximum groups under certain constraints | C ++ program for the above approach ; Function to check if it is possible to distribute W and B into maximum groups possible ; If W is greater than B , swap them ; Distribution is not possible ; Distribution is possible ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isPossible ( int W , int B , int D ) { if ( W > B ) swap ( W , B ) ; if ( B > W * ( D + 1 ) ) cout << " NO " << endl ; else cout << " YES " << endl ; } int main ( ) { int W = 2 ; int B = 5 ; int D = 2 ; isPossible ( W , B , D ) ; return 0 ; }
Find the maximum GCD possible for some pair in a given range [ L , R ] | C ++ program for the above approach ; Function to calculate GCD ; Function to calculate maximum GCD in a range ; Variable to store the answer ; If Z has two multiples in [ L , R ] ; Update ans ; Return the value ; Driver code ; Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } int maxGCDInRange ( int L , int R ) { int ans = 1 ; for ( int Z = R ; Z >= 1 ; Z -- ) { if ( ( R / Z ) - ( L - 1 ) / Z > 1 ) { ans = Z ; break ; } } return ans ; } int main ( ) { int L = 102 ; int R = 139 ; cout << maxGCDInRange ( L , R ) ; return 0 ; }
Number of ways to form a number with maximum Ks in it | C ++ program for the above approach ; Function to calculate number of ways a number can be formed that has the maximum number of Ks ; convert to string ; calculate length of subarrays that can contribute to the answer ; count length of subarray where adjacent digits add up to K ; Current subarray can contribute to the answer only if it is odd ; return the answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int noOfWays ( int N , int K ) { string S = to_string ( N ) ; int ans = 1 ; for ( int i = 1 ; i < S . length ( ) ; i ++ ) { int count = 1 ; while ( i < S . length ( ) && S [ i ] - '0' + S [ i - 1 ] - '0' == K ) { count ++ ; i ++ ; } if ( count % 2 ) ans *= ( count + 1 ) / 2 ; } return ans ; } int main ( ) { int N = 1454781 ; int K = 9 ; cout << noOfWays ( N , K ) << endl ; }
Minimum bit swaps between given numbers to make their Bitwise OR equal to Bitwise AND | C ++ implementation of the above approach ; Function for counting number of set bit ; Function to return the count of minimum operations required ; cnt to count the number of bits set in A and in B ; if odd numbers of total set bits ; one_zero = 1 in A and 0 in B at ith bit similarly for zero_one ; When bitpos is set in B , unset in B ; When bitpos is set in A , unset in B ; number of moves is half of number pairs of each group ; odd number pairs ; Driver code ; Input ; Function call to compute the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBits ( int n ) { int count = 0 ; while ( n ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } int minOperations ( int A , int B ) { int cnt1 = 0 , cnt2 = 0 ; cnt1 += countSetBits ( A ) ; cnt2 += countSetBits ( B ) ; if ( ( cnt1 + cnt2 ) % 2 != 0 ) return -1 ; int oneZero = 0 , zeroOne = 0 ; int ans = 0 ; for ( int i = 0 ; i < max ( cnt1 , cnt2 ) ; i ++ ) { int bitpos = 1 << i ; if ( ( ! ( bitpos & A ) ) && ( bitpos & B ) ) zeroOne ++ ; if ( ( bitpos & A ) && ( ! ( bitpos & B ) ) ) oneZero ++ ; } ans = ( zeroOne / 2 ) + ( oneZero / 2 ) ; if ( zeroOne % 2 != 0 ) ans += 2 ; return ans ; } int main ( ) { int A = 27 , B = 5 ; cout << minOperations ( A , B ) ; return 0 ; }