codeparrot/xlcost-text-to-code huggingface dataset:
Check whether all the bits are set in the given range | C ++ implementation to check whether all the bits are set in the given range or not ; function to check whether all the bits are set in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which will only have one or more set bits in the range l to r and nowhere else ; if both are equal , then all bits are set in the given range ; else all bits are not set ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string allBitsSetInTheGivenRange ( unsigned int n , unsigned int l , unsigned int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; int new_num = n & num ; if ( num == new_num ) return " Yes " ; return " No " ; } int main ( ) { unsigned int n = 22 ; unsigned int l = 2 , r = 3 ; cout << allBitsSetInTheGivenRange ( n , l , r ) ; return 0 ; }
1 to n bit numbers with no consecutive 1 s in binary representation | Print all numbers upto n bits with no consecutive set bits . ; Let us first compute 2 raised to power n . ; loop 1 to n to check all the numbers ; A number i doesn ' t ▁ contain ▁ ▁ consecutive ▁ set ▁ bits ▁ if ▁ ▁ bitwise ▁ and ▁ of ▁ i ▁ and ▁ left ▁ ▁ shifted ▁ i ▁ do ' t contain a commons set bit . ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printNonConsecutive ( int n ) { int p = ( 1 << n ) ; for ( int i = 1 ; i < p ; i ++ ) if ( ( i & ( i << 1 ) ) == 0 ) cout << i << " ▁ " ; } int main ( ) { int n = 3 ; printNonConsecutive ( n ) ; return 0 ; }
Find the n | Efficient C ++ program to find n - th palindrome ; Construct the nth binary palindrome with the given group number , aux_number and operation type ; No need to insert any bit in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Insert bit 0 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit  and fill around middle ; Insert bit 1 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit  and fill around middle ; Convert the number to decimal from binary ; Will return the nth binary palindrome number ; Add number of elements in all the groups , until the group of the nth number is found ; Total number of elements until this group ; Element 's offset position in the group ; Finding which bit to be placed in the middle and finding the number , which we will fill from the middle in both directions ; No need to put extra bit in middle ; We need to fill this auxiliary number in binary form the middle in both directions ; Need to Insert 0 at middle ; Need to Insert 1 at middle ; Driver code ; Function Call
#include <iostream> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; int constructNthNumber ( int group_no , int aux_num , int op ) { int INT_SIZE = 32 ; int a [ INT_SIZE ] = { 0 } ; int num = 0 , len_f ; int i = 0 ; if ( op == 2 ) { len_f = 2 * group_no ; a [ len_f - 1 ] = a [ 0 ] = 1 ; while ( aux_num ) { a [ group_no + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } else if ( op == 0 ) { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 0 ; while ( aux_num ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } else { len_f = 2 * group_no + 1 ; a [ len_f - 1 ] = a [ 0 ] = 1 ; a [ group_no ] = 1 ; while ( aux_num ) { a [ group_no + 1 + i ] = a [ group_no - 1 - i ] = aux_num & 1 ; aux_num = aux_num >> 1 ; i ++ ; } } for ( i = 0 ; i < len_f ; i ++ ) num += ( 1 << i ) * a [ i ] ; return num ; } int getNthNumber ( int n ) { int group_no = 0 , group_offset ; int count_upto_group = 0 , count_temp = 1 ; int op , aux_num ; while ( count_temp < n ) { group_no ++ ; count_upto_group = count_temp ; count_temp += 3 * ( 1 << ( group_no - 1 ) ) ; } group_offset = n - count_upto_group - 1 ; if ( ( group_offset + 1 ) <= ( 1 << ( group_no - 1 ) ) ) { op = 2 ; aux_num = group_offset ; } else { if ( ( ( group_offset + 1 ) - ( 1 << ( group_no - 1 ) ) ) % 2 ) op = 0 ; else op = 1 ; aux_num = ( ( group_offset ) - ( 1 << ( group_no - 1 ) ) ) / 2 ; } return constructNthNumber ( group_no , aux_num , op ) ; } int main ( ) { int n = 9 ; cout << getNthNumber ( n ) ; return 0 ; }
Shuffle a pack of cards and answer the query | C ++ program to find the card at given index after N shuffles ; function to find card at given index ; Answer will be reversal of N bits from MSB ; Calculating the reverse binary representation ; Printing the result ; driver code ; No . of Shuffle Steps ; Key position
#include <bits/stdc++.h> NEW_LINE using namespace std ; void shuffle ( int N , int key ) { unsigned int NO_OF_BITS = N ; unsigned int reverse_num = 0 , temp ; for ( int i = 0 ; i < NO_OF_BITS ; i ++ ) { temp = ( key & ( 1 << i ) ) ; if ( temp ) reverse_num |= ( 1 << ( ( NO_OF_BITS - 1 ) - i ) ) ; } cout << reverse_num ; } int main ( ) { int N = 3 ; unsigned int key = 3 ; shuffle ( N , key ) ; return 0 ; }
Sum of numbers with exactly 2 bits set | CPP program to find sum of numbers upto n whose 2 bits are set ; To count number of set bits ; To calculate sum of numbers ; To count sum of number whose 2 bit are set ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBits ( int n ) { int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( countSetBits ( i ) == 2 ) sum += i ; return sum ; } int main ( ) { int n = 10 ; cout << findSum ( n ) ; return 0 ; }
Toggle the last m bits | C ++ implementation to toggle the last m bits ; function to toggle the last m bits ; calculating a number ' num ' having ' m ' bits and all are set . ; toggle the last m bits and return the number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int toggleLastMBits ( unsigned int n , unsigned int m ) { unsigned int num = ( 1 << m ) - 1 ; return ( n ^ num ) ; } int main ( ) { unsigned int n = 107 ; unsigned int m = 4 ; cout << toggleLastMBits ( n , m ) ; return 0 ; }
Set the rightmost unset bit | C ++ implementation to set the rightmost unset bit ; function to find the position of rightmost set bit ; if n = 0 , return 1 ; if all bits of ' n ' are set ; position of rightmost unset bit in ' n ' passing ~ n as argument ; set the bit at position ' pos ' ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPosOfRightmostSetBit ( int n ) { return log2 ( n & - n ) + 1 ; } int setRightmostUnsetBit ( int n ) { if ( n == 0 ) return 1 ; if ( ( n & ( n + 1 ) ) == 0 ) return n ; int pos = getPosOfRightmostSetBit ( ~ n ) ; return ( ( 1 << ( pos - 1 ) ) n ) ; } int main ( ) { int n = 21 ; cout << setRightmostUnsetBit ( n ) ; return 0 ; }
Previous smaller integer having one less number of set bits | C ++ implementation to find the previous smaller integer with one less number of set bits ; function to find the position of rightmost set bit . ; function to find the previous smaller integer ; position of rightmost set bit of n ; turn off or unset the bit at position ' pos ' ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getFirstSetBitPos ( int n ) { return log2 ( n & - n ) + 1 ; } int previousSmallerInteger ( int n ) { int pos = getFirstSetBitPos ( n ) ; return ( n & ~ ( 1 << ( pos - 1 ) ) ) ; } int main ( ) { int n = 25 ; cout << previousSmallerInteger ( n ) ; return 0 ; }
Convert a binary number to octal | C ++ implementation to convert a binary number to octal number ; function to create map between binary number and its equivalent octal ; Function to find octal equivalent of binary ; length of string before ' . ' ; add min 0 's in the beginning to make  left substring length divisible by 3 ; if decimal point exists ; length of string after ' . ' ; add min 0 's in the end to make right  substring length divisible by 3 ; create map between binary and its equivalent octal code ; one by one extract from left , substring of size 3 and add its octal code ; if ' . ' is encountered add it to result ; required octal number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createMap ( unordered_map < string , char > * um ) { ( * um ) [ "000" ] = '0' ; ( * um ) [ "001" ] = '1' ; ( * um ) [ "010" ] = '2' ; ( * um ) [ "011" ] = '3' ; ( * um ) [ "100" ] = '4' ; ( * um ) [ "101" ] = '5' ; ( * um ) [ "110" ] = '6' ; ( * um ) [ "111" ] = '7' ; } string convertBinToOct ( string bin ) { int l = bin . size ( ) ; int t = bin . find_first_of ( ' . ' ) ; int len_left = t != -1 ? t : l ; for ( int i = 1 ; i <= ( 3 - len_left % 3 ) % 3 ; i ++ ) bin = '0' + bin ; if ( t != -1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 3 - len_right % 3 ) % 3 ; i ++ ) bin = bin + '0' ; } unordered_map < string , char > bin_oct_map ; createMap ( & bin_oct_map ) ; int i = 0 ; string octal = " " ; while ( 1 ) { octal += bin_oct_map [ bin . substr ( i , 3 ) ] ; i += 3 ; if ( i == bin . size ( ) ) break ; if ( bin . at ( i ) == ' . ' ) { octal += ' . ' ; i ++ ; } } return octal ; } int main ( ) { string bin = "1111001010010100001.010110110011011" ; cout << " Octal ▁ number ▁ = ▁ " << convertBinToOct ( bin ) ; return 0 ; }
Check if all bits of a number are set | C ++ implementation to check whether every digit in the binary representation of the given number is set or not ; function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; loop till n becomes '0' ; if the last bit is not set ; right shift ' n ' by 1 ; all bits are set ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string areAllBitsSet ( int n ) { if ( n == 0 ) return " No " ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) return " No " ; n = n >> 1 ; } return " Yes " ; } int main ( ) { int n = 7 ; cout << areAllBitsSet ( n ) ; return 0 ; }
Check if all bits of a number are set | C ++ implementation to check whether every digit in the binary representation of the given number is set or not ; function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; if true , then all bits are set ; else all bits are not set ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; string areAllBitsSet ( int n ) { if ( n == 0 ) return " No " ; if ( ( ( n + 1 ) & n ) == 0 ) return " Yes " ; return " No " ; } int main ( ) { int n = 7 ; cout << areAllBitsSet ( n ) ; return 0 ; }
Next greater integer having one more number of set bits | C ++ implementation to find the next greater integer with one more number of set bits ; function to find the position of rightmost set bit . Returns - 1 if there are no set bits ; function to find the next greater integer ; position of rightmost unset bit of n by passing ~ n as argument ; if n consists of unset bits , then set the rightmost unset bit ; n does not consists of unset bits ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getFirstSetBitPos ( int n ) { return ( log2 ( n & - n ) + 1 ) - 1 ; } int nextGreaterWithOneMoreSetBit ( int n ) { int pos = getFirstSetBitPos ( ~ n ) ; if ( pos > -1 ) return ( 1 << pos ) | n ; return ( ( n << 1 ) + 1 ) ; } int main ( ) { int n = 10 ; cout << " Next ▁ greater ▁ integer ▁ = ▁ " << nextGreaterWithOneMoreSetBit ( n ) ; return 0 ; }
Toggle all the bits of a number except k | C ++ program to toggle all bits except kth bit ; Returns a number with all bit toggled in n except k - th bit ; 1 ) Toggle k - th bit by doing n ^ ( 1 << k ) 2 ) Toggle all bits of the modified number ; Driver code
#include <iostream> NEW_LINE using namespace std ; unsigned int toggleAllExceptK ( unsigned int n , unsigned int k ) { return ~ ( n ^ ( 1 << k ) ) ; } int main ( ) { unsigned int n = 4294967295 ; unsigned int k = 0 ; cout << toggleAllExceptK ( n , k ) ; return 0 ; }
Count numbers whose sum with x is equal to XOR with x | C ++ program to count numbers whose bitwise XOR and sum with x are equal ; Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to x , whose bitwise XOR and SUM with x are equal . ; count number of zero bit in x ; power of 2 to count ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long CountZeroBit ( long x ) { unsigned int count = 0 ; while ( x ) { if ( ! ( x & 1LL ) ) count ++ ; x >>= 1LL ; } return count ; } long CountXORandSumEqual ( long x ) { long count = CountZeroBit ( x ) ; return ( 1LL << count ) ; } int main ( ) { long x = 10 ; cout << CountXORandSumEqual ( x ) ; return 0 ; }
Find missing number in another array which is shuffled copy | C ++ implementation to find the missing number in shuffled array C ++ implementation to find the missing number in shuffled array ; Returns the missing number Size of arr2 [ ] is n - 1 ; Missing number ' mnum ' ; 1 st array is of size ' n ' ; 2 nd array is of size ' n ▁ - ▁ 1' ; Required missing number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int missingNumber ( int arr1 [ ] , int arr2 [ ] , int n ) { int mnum = 0 ; for ( int i = 0 ; i < n ; i ++ ) mnum = mnum ^ arr1 [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) mnum = mnum ^ arr2 [ i ] ; return mnum ; } int main ( ) { int arr1 [ ] = { 4 , 8 , 1 , 3 , 7 } ; int arr2 [ ] = { 7 , 4 , 3 , 1 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << " Missing ▁ number ▁ = ▁ " << missingNumber ( arr1 , arr2 , n ) ; return 0 ; }
Find Duplicates of array using bit array | C ++ program to print all Duplicates in array ; A class to represent an array of bits using array of integers ; Constructor ; Divide by 32. To store n bits , we need n / 32 + 1 integers ( Assuming int is stored using 32 bits ) ; Get value of a bit at given position ; Divide by 32 to find position of integer . ; Now find bit number in arr [ index ] ; Find value of given bit number in arr [ index ] ; Sets a bit at given position ; Find index of bit position ; Set bit number in arr [ index ] ; Main function to print all Duplicates ; create a bit with 32000 bits ; Traverse array elements ; Index in bit array ; If num is already present in bit array ; Else insert num ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class BitArray { int * arr ; public : BitArray ( ) { } BitArray ( int n ) { arr = new int [ ( n >> 5 ) + 1 ] ; } bool get ( int pos ) { int index = ( pos >> 5 ) ; int bitNo = ( pos & 0x1F ) ; return ( arr [ index ] & ( 1 << bitNo ) ) != 0 ; } void set ( int pos ) { int index = ( pos >> 5 ) ; int bitNo = ( pos & 0x1F ) ; arr [ index ] |= ( 1 << bitNo ) ; } void checkDuplicates ( int arr [ ] , int n ) { BitArray ba = BitArray ( 320000 ) ; for ( int i = 0 ; i < n ; i ++ ) { int num = arr [ i ] ; if ( ba . get ( num ) ) cout << num << " ▁ " ; else ba . set ( num ) ; } } } ; int main ( ) { int arr [ ] = { 1 , 5 , 1 , 10 , 12 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; BitArray obj = BitArray ( ) ; obj . checkDuplicates ( arr , n ) ; return 0 ; }
Change bits to make specific OR value | C ++ program to change least bits to get desired OR value ; Returns max of three numbers ; Returns count of bits in N ; Returns bit at ' pos ' position ; Utility method to toggle bit at ' pos ' position ; method returns minimum number of bit flip to get T as OR value of A and B ; Loop over maximum number of bits among A , B and T ; T 's bit is set, try to toggle bit  of B, if not already ; if A 's bit is set, flip that ; if B 's bit is set, flip that ; if K is less than 0 then we can make A | B == T ; Loop over bits one more time to minimise A further ; If both bit are set , then Unset A 's bit to minimise it ; If A ' s ▁ bit ▁ is ▁ 1 ▁ and ▁ B ' s bit is 0 , toggle both ; Output changed value of A and B ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max ( int a , int b , int c ) { return max ( a , max ( b , c ) ) ; } int bitCount ( int N ) { int cnt = 0 ; while ( N ) { cnt ++ ; N >>= 1 ; } return cnt ; } bool at_position ( int num , int pos ) { bool bit = num & ( 1 << pos ) ; return bit ; } void toggle ( int & num , int pos ) { num ^= ( 1 << pos ) ; } void minChangeToReachTaregetOR ( int A , int B , int K , int T ) { int maxlen = max ( bitCount ( A ) , bitCount ( B ) , bitCount ( T ) ) ; for ( int i = maxlen - 1 ; i >= 0 ; i -- ) { bool bitA = at_position ( A , i ) ; bool bitB = at_position ( B , i ) ; bool bitT = at_position ( T , i ) ; if ( bitT ) { if ( ! bitA && ! bitB ) { toggle ( B , i ) ; K -- ; } } else { if ( bitA ) { toggle ( A , i ) ; K -- ; } if ( bitB ) { toggle ( B , i ) ; K -- ; } } } if ( K < 0 ) { cout << " Not ▁ possible STRNEWLINE " ; return ; } for ( int i = maxlen - 1 ; K > 0 && i >= 0 ; -- i ) { bool bitA = at_position ( A , i ) ; bool bitB = at_position ( B , i ) ; bool bitT = at_position ( T , i ) ; if ( bitT ) { if ( bitA && bitB ) { toggle ( A , i ) ; K -- ; } } if ( bitA && ! bitB && K >= 2 ) { toggle ( A , i ) ; toggle ( B , i ) ; K -= 2 ; } } cout << A << " ▁ " << B << endl ; } int main ( ) { int A = 175 , B = 66 , K = 5 , T = 100 ; minChangeToReachTaregetOR ( A , B , K , T ) ; return 0 ; }
Count smaller values whose XOR with x is greater than x | C ++ program to find count of values whose XOR with x is greater than x and values are smaller than x ; Initialize result ; Traversing through all bits of x ; If current last bit of x is set then increment count by n . Here n is a power of 2 corresponding to position of bit ; Simultaneously calculate the 2 ^ n ; Replace x with x / 2 ; ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countValues ( int x ) { int count = 0 , n = 1 ; while ( x != 0 ) { if ( x % 2 == 0 ) count += n ; n *= 2 ; x /= 2 ; } return count ; } int main ( ) { int x = 10 ; cout << countValues ( x ) ; return 0 ; }
Construct an array from XOR of all elements of array except element at same index | C ++ program to construct array from XOR of elements of given array ; function to construct new array ; calculate xor of array ; update array ; Driver code ; print result
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructXOR ( int A [ ] , int n ) { int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) XOR ^= A [ i ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = XOR ^ A [ i ] ; } int main ( ) { int A [ ] = { 2 , 4 , 1 , 3 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; constructXOR ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << A [ i ] << " ▁ " ; return 0 ; }
Count all pairs of an array which differ in K bits | C ++ program to count all pairs with bit difference as k ; Utility function to count total ones in a number ; Function to count pairs of K different bits ; long long ans = 0 ; initialize final answer ; Check for K differ bit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bitCount ( int n ) { int count = 0 ; while ( n ) { if ( n & 1 ) ++ count ; n >>= 1 ; } return count ; } long long countPairsWithKDiff ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n - 1 ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int xoredNum = arr [ i ] ^ arr [ j ] ; if ( k == bitCount ( xoredNum ) ) ++ ans ; } } return ans ; } int main ( ) { int k = 2 ; int arr [ ] = { 2 , 4 , 1 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " << k << " ▁ are ▁ " << countPairsWithKDiff ( arr , n , k ) << " STRNEWLINE " ; return 0 ; }
Count all pairs of an array which differ in K bits | Below is C ++ approach of finding total k bit difference pairs ; Function to calculate K bit different pairs in array ; Get the maximum value among all array elemensts ; Set the count array to 0 , count [ ] stores the total frequency of array elements ; Initialize result ; For 0 bit answer will be total count of same number ; if count [ i ] is 0 , skip the next loop as it will not contribute the answer ; Update answer if k differ bit found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long kBitDifferencePairs ( int arr [ ] , int n , int k ) { int MAX = * max_element ( arr , arr + n ) ; long long count [ MAX + 1 ] ; memset ( count , 0 , sizeof ( count ) ) ; for ( int i = 0 ; i < n ; ++ i ) ++ count [ arr [ i ] ] ; long long ans = 0 ; if ( k == 0 ) { for ( int i = 0 ; i <= MAX ; ++ i ) ans += ( count [ i ] * ( count [ i ] - 1 ) ) / 2 ; return ans ; } for ( int i = 0 ; i <= MAX ; ++ i ) { if ( ! count [ i ] ) continue ; for ( int j = i + 1 ; j <= MAX ; ++ j ) { if ( __builtin_popcount ( i ^ j ) == k ) ans += count [ i ] * count [ j ] ; } } return ans ; } int main ( ) { int k = 2 ; int arr [ ] = { 2 , 4 , 1 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " << k << " ▁ are ▁ = ▁ " << kBitDifferencePairs ( arr , n , k ) << " STRNEWLINE " ; k = 3 ; cout << " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " << k << " ▁ are ▁ = ▁ " << kBitDifferencePairs ( arr , n , k ) ; return 0 ; }
Ways to represent a number as a sum of 1 ' s ▁ and ▁ 2' s | C ++ program to find number of ways to representing a number as a sum of 1 ' s ▁ and ▁ 2' s ; Function to multiply matrix . ; Power function in log n ; function that returns ( n + 1 ) th Fibonacci number Or number of ways to represent n as sum of 1 ' s ▁ ▁ 2' s ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void multiply ( int F [ 2 ] [ 2 ] , int M [ 2 ] [ 2 ] ) { int x = F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ; int y = F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ; int z = F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ; int w = F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ; F [ 0 ] [ 0 ] = x ; F [ 0 ] [ 1 ] = y ; F [ 1 ] [ 0 ] = z ; F [ 1 ] [ 1 ] = w ; } void power ( int F [ 2 ] [ 2 ] , int n ) { if ( n == 0 n == 1 ) return ; int M [ 2 ] [ 2 ] = { { 1 , 1 } , { 1 , 0 } } ; power ( F , n / 2 ) ; multiply ( F , F ) ; if ( n % 2 != 0 ) multiply ( F , M ) ; } int countWays ( int n ) { int F [ 2 ] [ 2 ] = { { 1 , 1 } , { 1 , 0 } } ; if ( n == 0 ) return 0 ; power ( F , n ) ; return F [ 0 ] [ 0 ] ; } int main ( ) { int n = 5 ; cout << countWays ( n ) << endl ; return 0 ; }
Multiplication of two numbers with shift operator | CPP program to find multiplication of two number without use of multiplication operator ; Function for multiplication ; check for set bit and left shift n , count times ; increment of place value ( count ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int multiply ( int n , int m ) { int ans = 0 , count = 0 ; while ( m ) { if ( m % 2 == 1 ) ans += n << count ; count ++ ; m /= 2 ; } return ans ; } int main ( ) { int n = 20 , m = 13 ; cout << multiply ( n , m ) ; return 0 ; }
Compare two integers without using any Comparison operator | C ++ program to compare two integers without any comparison operator . ; function return true if A ^ B > 0 else false ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool EqualNumber ( int A , int B ) { return ( A ^ B ) ; } int main ( ) { int A = 5 , B = 6 ; cout << ! EqualNumber ( A , B ) << endl ; return 0 ; }