codeparrot/xlcost-text-to-code huggingface dataset:
Minimum gcd operations to make all array elements one | CPP program to find minimum GCD operations to make all array elements one . ; Function to count number of moves . ; Counting Number of ones . ; If there is a one ; Find smallest subarray with GCD equals to one . ; to calculate GCD ; Not Possible ; Final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumMoves ( int A [ ] , int N ) { int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] == 1 ) one ++ ; if ( one != 0 ) return N - one ; int minimum = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { int g = A [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { g = __gcd ( A [ j ] , g ) ; if ( g == 1 ) { minimum = min ( minimum , j - i ) ; break ; } } } if ( minimum == INT_MAX ) return -1 ; else return N + minimum - 1 ; } int main ( ) { int A [ ] = { 2 , 4 , 3 , 9 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minimumMoves ( A , N ) ; return 0 ; }
Given N and Standard Deviation , find N elements | CPP program to find n elements ; function to print series of n elements ; if S . D . is 0 then print all elements as 0. ; print n 0 's ; if S . D . is even ; print - SD , + SD , - SD , + SD ; if odd ; convert n to a float integer ; print one element to be 0 ; print ( n - 1 ) elements as xi derived from the formula ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void series ( int n , int d ) { if ( d == 0 ) { for ( int i = 0 ; i < n ; i ++ ) cout << "0 ▁ " ; cout << endl ; return ; } if ( n % 2 == 0 ) { for ( int i = 1 ; i <= n ; i ++ ) { cout << pow ( -1 , i ) * d << " ▁ " ; } cout << endl ; } else { float m = n ; float r = ( m / ( m - 1 ) ) ; float g = ( float ) ( d * ( float ) sqrtf ( r ) ) ; cout << "0 ▁ " ; for ( int i = 1 ; i < n ; i ++ ) { cout << pow ( -1 , i ) * g << " ▁ " ; } cout << endl ; } } int main ( ) { int n = 3 , d = 3 ; series ( n , d ) ; return 0 ; }
Total no of 1 's in numbers | c ++ code to count the frequency of 1 in numbers less than or equal to the given number . ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigitOne ( int n ) { int countr = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { string str = to_string ( i ) ; countr += count ( str . begin ( ) , str . end ( ) , '1' ) ; } return countr ; } int main ( ) { int n = 13 ; cout << countDigitOne ( n ) << endl ; n = 131 ; cout << countDigitOne ( n ) << endl ; n = 159 ; cout << countDigitOne ( n ) << endl ; return 0 ; }
Exponential Squaring ( Fast Modulo Multiplication ) | C ++ program to compute exponential value using ( 2 ^ k ) - ary method . ; # define N 1000000007L ; prime modulo value ; for cases where exponent is not an even value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long exponentiation ( long base , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * base ) % N ; base = ( base * base ) % N ; exp /= 2 ; } return t % N ; } int main ( ) { long base = 5 ; long exp = 100000 ; long modulo = exponentiation ( base , exp ) ; cout << ( modulo ) ; return 0 ; }
GCD of factorials of two numbers | CPP program to find GCD of factorial of two numbers . ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int x ) { if ( x <= 1 ) return 1 ; int res = 2 ; for ( int i = 3 ; i <= x ; i ++ ) res = res * i ; return res ; } int gcdOfFactorial ( int m , int n ) { return factorial ( min ( m , n ) ) ; } int main ( ) { int m = 5 , n = 9 ; cout << gcdOfFactorial ( m , n ) ; return 0 ; }
Recursive sum of digits of a number is prime or not | CPP code to check if recursive sum of digits is prime or not . ; Function for recursive digit sum ; function to check if prime or not the single digit ; calls function which returns sum till single digit ; checking prime ; Driver code
#include <iostream> NEW_LINE using namespace std ; int recDigSum ( int n ) { if ( n == 0 ) return 0 ; else { if ( n % 9 == 0 ) return 9 ; else return n % 9 ; } } void check ( int n ) { n = recDigSum ( n ) ; if ( n == 2 or n == 3 or n == 5 or n == 7 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int n = 5602 ; check ( n ) ; }
Find n | CPP program to find the value at n - th place in the given sequence ; Definition of findNumber function ; Finding x from equation n = x ( x + 1 ) / 2 + 1 ; Base of current block ; Value of n - th element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int n ) { int x = ( int ) floor ( ( -1 + sqrt ( 1 + 8 * n - 8 ) ) / 2 ) ; int base = ( x * ( x + 1 ) ) / 2 + 1 ; return n - base + 1 ; } int main ( ) { int n = 55 ; cout << findNumber ( n ) << endl ; return 0 ; }
Program for weighted mean of natural numbers . | Program to find weighted mean of first n natural numbers using formula . ; Returns weighted mean assuming for numbers { 1 , 2 , . . n } and weights { 1 , 2 , . . n } ; Driver program to test the function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int weightedMean ( int n ) { return ( 2 * n + 1 ) / 3 ; } int main ( ) { int n = 10 ; cout << weightedMean ( n ) ; return 0 ; }
Divide every element of one array by other array elements | CPP program to find quotient of array elements ; Function to calculate the quotient of every element of the array ; Calculate the product of all elements ; To calculate the quotient of every array element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void calculate ( int a [ ] , int b [ ] , int n , int m ) { int mul = 1 ; for ( int i = 0 ; i < m ; i ++ ) if ( b [ i ] != 0 ) mul = mul * b [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = floor ( a [ i ] / mul ) ; cout << x << " ▁ " ; } } int main ( ) { int a [ ] = { 5 , 100 , 8 } ; int b [ ] = { 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = sizeof ( b ) / sizeof ( b [ 0 ] ) ; calculate ( a , b , n , m ) ; return 0 ; }
Largest power of k in n ! ( factorial ) where k may not be prime | CPP program to find the largest power of k that divides n ! ; To find the power of a prime p in factorial N ; calculating floor ( n / r ) and adding to the count ; increasing the power of p from 1 to 2 to 3 and so on ; returns all the prime factors of k ; vector to store all the prime factors along with their number of occurrence in factorization of k ; Returns largest power of k that divides n ! ; calculating minimum power of all the prime factors of k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findPowerOfP ( int n , int p ) { int count = 0 ; int r = p ; while ( r <= n ) { count += ( n / r ) ; r = r * p ; } return count ; } vector < pair < int , int > > primeFactorsofK ( int k ) { vector < pair < int , int > > ans ; for ( int i = 2 ; k != 1 ; i ++ ) { if ( k % i == 0 ) { int count = 0 ; while ( k % i == 0 ) { k = k / i ; count ++ ; } ans . push_back ( make_pair ( i , count ) ) ; } } return ans ; } int largestPowerOfK ( int n , int k ) { vector < pair < int , int > > vec ; vec = primeFactorsofK ( k ) ; int ans = INT_MAX ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) ans = min ( ans , findPowerOfP ( n , vec [ i ] . first ) / vec [ i ] . second ) ; return ans ; } int main ( ) { cout << largestPowerOfK ( 7 , 2 ) << endl ; cout << largestPowerOfK ( 10 , 9 ) << endl ; return 0 ; }
Minimum number of bombs | CPP program to find number of bombings required to kill all aliens . ; function to print where to shoot ; no . of bombs required ; bomb all the even positions ; bomb all the odd positions ; bomb all the even positions again ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print ( int n ) { cout << n + n / 2 << endl ; for ( int i = 2 ; i <= n ; i += 2 ) cout << i << " ▁ " ; for ( int i = 1 ; i <= n ; i += 2 ) cout << i << " ▁ " ; for ( int i = 2 ; i <= n ; i += 2 ) cout << i << " ▁ " ; } int main ( ) { int n = 3 ; print ( n ) ; return 0 ; }
LCM of digits of a given number | CPP program to find LCM of digits of a number ; define lcm function ; If at any point LCM become 0. return it ; driver code
#include <iostream> NEW_LINE #include <boost/math/common_factor.hpp> NEW_LINE using namespace std ; int digitLCM ( int n ) { int lcm = 1 ; while ( n > 0 ) { lcm = boost :: math :: lcm ( n % 10 , lcm ) ; if ( lcm == 0 ) return 0 ; n = n / 10 ; } return lcm ; } int main ( ) { long n = 397 ; cout << digitLCM ( n ) ; return 0 ; }
Secretary Problem ( A Optimal Stopping Problem ) | C ++ Program to test 1 / e law for Secretary Problem : ; To find closest integer of num . ; Finds best candidate using n / e rule . candidate [ ] represents talents of n candidates . ; Calculating sample size for benchmarking . ; Finding best candidate in sample size ; Finding the first best candidate that is better than benchmark set . ; Driver Code ; n = 8 candidates and candidate array contains talents of n candidate where the largest number means highest talented candidate . ; generating random numbers between 1 to 8 for talent of candidate
#include <iostream> NEW_LINE #include <time.h> NEW_LINE #define e  2.71828 NEW_LINE using namespace std ; int roundNo ( float num ) { return num < 0 ? num - 0.5 : num + 0.5 ; } void printBestCandidate ( int candidate [ ] , int n ) { int sample_size = roundNo ( n / e ) ; cout << " Sample size is " int best = 0 ; for ( int i = 1 ; i < sample_size ; i ++ ) if ( candidate [ i ] > candidate [ best ] ) best = i ; for ( int i = sample_size ; i < n ; i ++ ) if ( candidate [ i ] >= candidate [ best ] ) { best = i ; break ; } if ( best >= sample_size ) cout << endl << " Best ▁ candidate ▁ found ▁ is ▁ " << best + 1 << " ▁ with ▁ talent ▁ " << candidate [ best ] << endl ; else cout << " Couldn ' t ▁ find ▁ a ▁ best ▁ candidate STRNEWLINE " ; } int main ( ) { int n = 8 ; int candidate [ n ] ; srand ( time ( 0 ) ) ; for ( int i = 0 ; i < n ; i ++ ) candidate [ i ] = 1 + rand ( ) % 8 ; cout << " Candidate ▁ : ▁ " ; for ( int i = 0 ; i < n ; i ++ ) cout << i + 1 << " ▁ " ; cout << endl ; cout << " ▁ Talents ▁ : ▁ " ; for ( int i = 0 ; i < n ; i ++ ) cout << candidate [ i ] << " ▁ " ; printBestCandidate ( candidate , n ) ; return 0 ; }
Newton Forward And Backward Interpolation | CPP Program to interpolate using newton backward interpolation ; Calculation of u mentioned in formula ; Calculating factorial of given n ; Driver code ; number of values given ; y [ ] [ ] is used for difference table and y [ ] [ 0 ] used for input ; Calculating the backward difference table ; Displaying the backward difference table ; Value to interpolate at ; Initializing u and sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; float u_cal ( float u , int n ) { float temp = u ; for ( int i = 1 ; i < n ; i ++ ) temp = temp * ( u + i ) ; return temp ; } int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) f *= i ; return f ; } int main ( ) { int n = 5 ; float x [ ] = { 1891 , 1901 , 1911 , 1921 , 1931 } ; float y [ n ] [ n ] ; y [ 0 ] [ 0 ] = 46 ; y [ 1 ] [ 0 ] = 66 ; y [ 2 ] [ 0 ] = 81 ; y [ 3 ] [ 0 ] = 93 ; y [ 4 ] [ 0 ] = 101 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = n - 1 ; j >= i ; j -- ) y [ j ] [ i ] = y [ j ] [ i - 1 ] - y [ j - 1 ] [ i - 1 ] ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) cout << setw ( 4 ) << y [ i ] [ j ] << " TABSYMBOL " ; cout << endl ; } float value = 1925 ; float sum = y [ n - 1 ] [ 0 ] ; float u = ( value - x [ n - 1 ] ) / ( x [ 1 ] - x [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { sum = sum + ( u_cal ( u , i ) * y [ n - 1 ] [ i ] ) / fact ( i ) ; } cout << " Value at " ▁ < < ▁ value ▁ < < ▁ " is " << sum << endl ; return 0 ; }
Happy Numbers | A space optimized CPP program to check if a number is happy number ; Returns sum of squares of digits of a number n . For example for n = 12 it returns 1 + 4 = 5 ; Returns true if n is Happy number else returns false . ; Keep replacing n with sum of squares of digits until we either reach 1 or we end up in a cycle ; Number is Happy if we reach 1 ; Replace n with sum of squares of digits ; Number is not Happy if we reach 4 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumDigitSquare ( int n ) { int sq = 0 ; while ( n ) { int digit = n % 10 ; sq += digit * digit ; n = n / 10 ; } return sq ; } bool isHappy ( int n ) { while ( 1 ) { if ( n == 1 ) return true ; n = sumDigitSquare ( n ) ; if ( n == 4 ) return false ; } return false ; } int main ( ) { int n = 23 ; if ( isHappy ( n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Sum of all subsets of a set formed by first n natural numbers | CPP program to find sum of all subsets of a set . ; sum of subsets is ( n * ( n + 1 ) / 2 ) * pow ( 2 , n - 1 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned long long findSumSubsets ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 1 << ( n - 1 ) ) ; } int main ( ) { int n = 3 ; cout << findSumSubsets ( n ) ; return 0 ; }
Minimum element whose n | CPP program to find minimum element whose n - th power is greater than product of an array of size n ; function to find the minimum element ; loop to traverse and store the sum of log ; calculates the elements according to formula . ; returns the minimal element ; Driver program to test above function ; initialised array ; computes the size of array ; prints out the minimal element
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMin ( int a [ ] , int n ) { double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) int x = exp ( sum / n ) ; return x + 1 ; } int main ( ) { int a [ ] = { 3 , 2 , 1 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findMin ( a , n ) ; }
Generate all cyclic permutations of a number | Program to generate all cyclic permutations of number ; Function to count the total number of digits in a number . ; Function to generate all cyclic permutations of a number ; Following three lines generates a circular pirmutation of a number . ; If all the permutations are checked and we obtain original number exit from loop . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countdigits ( int N ) { int count = 0 ; while ( N ) { count ++ ; N = N / 10 ; } return count ; } void cyclic ( int N ) { int num = N ; int n = countdigits ( N ) ; while ( 1 ) { cout << num << endl ; int rem = num % 10 ; int div = num / 10 ; num = ( pow ( 10 , n - 1 ) ) * rem + div ; if ( num == N ) break ; } } int main ( ) { int N = 5674 ; cyclic ( N ) ; return 0 ; }
Check whether a number is circular prime or not | Program to check if a number is circular prime or not . ; Function to check if a number is prime or not . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the number is circular prime or not . ; Count digits . ; Following three lines generate the next circular permutation of a number . We move last digit to first position . ; If all the permutations are checked and we obtain original number exit from loop . ; Driver Program
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } bool checkCircular ( int N ) { int count = 0 , temp = N ; while ( temp ) { count ++ ; temp /= 10 ; } int num = N ; while ( isPrime ( num ) ) { int rem = num % 10 ; int div = num / 10 ; num = ( pow ( 10 , count - 1 ) ) * rem + div ; if ( num == N ) return true ; } return false ; } int main ( ) { int N = 1193 ; if ( checkCircular ( N ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Find if two people ever meet after same number of jumps | C ++ program to find any one of them can overtake the other ; function to find if any one of them can overtake the other ; Since starting points are always different , they will meet if following conditions are met . ( 1 ) Speeds are not same ( 2 ) Difference between speeds divide the total distance between initial points . ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sackRace ( int p1 , int s1 , int p2 , int s2 ) { return ( ( s1 > s2 && ( p2 - p1 ) % ( s1 - s2 ) == 0 ) || ( s2 > s1 && ( p1 - p2 ) % ( s2 - s1 ) == 0 ) ) ; } int main ( ) { int p1 = 4 , s1 = 4 , p2 = 8 , s2 = 2 ; sackRace ( p1 , s1 , p2 , s2 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Largest proper fraction with sum of numerator and denominator equal to a given number | CPP program to find the largest fraction a / b such that a + b is equal to given number and a < b . ; Calculate N / 2 ; ; Check if N is odd or even ; If N is odd answer will be ceil ( n / 2 ) - 1 and floor ( n / 2 ) + 1 ; If N is even check if N / 2 i . e a is even or odd ; If N / 2 is even apply the previous formula ; If N / 2 is odd answer will be ceil ( N / 2 ) - 2 and floor ( N / 2 ) + 2 ; driver function
#include <iostream> NEW_LINE #include <cmath> NEW_LINE using namespace std ; void solve ( int n ) { float a = ( float ) n / 2 ; if ( n % 2 != 0 ) cout << ceil ( a ) - 1 << " ▁ " << floor ( a ) + 1 << endl ; else { if ( ( int ) a % 2 == 0 ) { cout << ceil ( a ) - 1 << " ▁ " << floor ( a ) + 1 << endl ; } else { cout << ceil ( a ) - 2 << " ▁ " << floor ( a ) + 2 << endl ; } } } int main ( ) { int n = 34 ; solve ( n ) ; return 0 ; }
Program to find simple interest | CPP program to find simple interest for given principal amount , time and rate of interest . ; We can change values here for different inputs ; Calculate simple interest ; Print the resultant value of SI
#include <iostream> NEW_LINE using namespace std ; int main ( ) { float P = 1 , R = 1 , T = 1 ; float SI = ( P * T * R ) / 100 ; cout << " Simple ▁ Interest ▁ = ▁ " << SI ; return 0 ; }
Number of digits in the product of two numbers | C ++ implementation to count number of digits in the product of two numbers ; function to count number of digits in the product of two numbers ; absolute value of the product of two numbers ; if product is 0 ; count number of digits in the product ' p ' ; required count of digits ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigits ( int a , int b ) { int count = 0 ; int p = abs ( a * b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } int main ( ) { int a = 33 ; int b = -24 ; cout << " Number ▁ of ▁ digits ▁ = ▁ " << countDigits ( a , b ) ; return 0 ; }
Find multiple of x closest to or a ^ b ( a raised to power b ) | C ++ Program to find closest multiple of x to a ^ b ; function to find closest multiple of x to a ^ b ; calculate a ^ b / x ; Answer is either ( ans * x ) or ( ans + 1 ) * x ; Printing nearest answer ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void multiple ( int a , int b , int x ) { if ( b < 0 ) { if ( a == 1 && x == 1 ) cout << "1" ; else cout << "0" ; } int mul = pow ( a , b ) ; int ans = mul / x ; int ans1 = x * ans ; int ans2 = x * ( ans + 1 ) ; cout << ( ( ( mul - ans1 ) <= ( ans2 - mul ) ) ? ans1 : ans2 ) ; } int main ( ) { int a = 349 , b = 1 , x = 4 ; multiple ( a , b , x ) ; return 0 ; }
Maximum sum of difference of adjacent elements | CPP program to find maximum sum of adjacent elements of permutation of n ; To find max sum of permutation ; Base case ; Otherwise max sum will be ( n * ( n - 1 ) / 2 ) - 1 + n / 2 ; Driver program to test maxSum ( )
#include <iostream> NEW_LINE using namespace std ; int maxSum ( int n ) { if ( n == 1 ) return 1 ; else return ( n * ( n - 1 ) / 2 ) - 1 + n / 2 ; } int main ( ) { int n = 3 ; cout << maxSum ( n ) ; return 0 ; }
Compute average of two numbers without overflow | C ++ code to compute average of two numbers ; Function to compute average of two numbers ; Driver code ; Assigning maximum integer value ; Average of two equal numbers is the same number ; Function to get the average of 2 numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; int compute_average ( int a , int b ) { return ( a + b ) / 2 ; } int main ( ) { int a = INT_MAX , b = INT_MAX ; cout << " Actual ▁ average ▁ : ▁ " << INT_MAX << endl ; cout << " Computed ▁ average ▁ : ▁ " << compute_average ( a , b ) ; return 0 ; }
Add minimum number to an array so that the sum becomes even | CPP program to add minimum number so that the sum of array becomes even ; Function to find out minimum number ; Count odd number of terms in array ; Driver code
#include <iostream> NEW_LINE using namespace std ; int minNum ( int arr [ ] , int n ) { bool odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 ) odd = ! odd ; if ( odd ) return 1 ; return 2 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minNum ( arr , n ) << " n " ; return 0 ; }
Check if a number is jumbled or not | CPP code to check if a number is jumbled or not ; Function to check if a number is jumbled or not ; Single digit number ; Checking every digit through a loop ; All digits were checked ; Digit at index i ; Digit at index i - 1 ; If difference is greater than 1 ; Number checked ; Driver code ; - 1234 to be checked ; 287 to be checked
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkJumbled ( int num ) { if ( num / 10 == 0 ) return true ; while ( num != 0 ) { if ( num / 10 == 0 ) return true ; int digit1 = num % 10 ; int digit2 = ( num / 10 ) % 10 ; if ( abs ( digit2 - digit1 ) > 1 ) return false ; num = num / 10 ; } return true ; } int main ( ) { int num = -1234 ; if ( checkJumbled ( num ) ) cout << " True ▁ STRNEWLINE " ; else cout << " False ▁ STRNEWLINE " ; num = -1247 ; if ( checkJumbled ( num ) ) cout << " True ▁ STRNEWLINE " ; else cout << " False ▁ STRNEWLINE " ; return 0 ; }
Josephus Problem Using Bit Magic | C ++ program for josephus problem ; function to find the position of the Most Significant Bit ; keeps shifting bits to the right until we are left with 0 ; function to return at which place Josephus should sit to avoid being killed ; Getting the position of the Most Significant Bit ( MSB ) . The leftmost '1' . If the number is '41' then its binary is '101001' . So msbPos ( 41 ) = 6 ; ' j ' stores the number with which to XOR the number ' n ' . Since we need '100000' We will do 1 << 6 - 1 to get '100000' ; Toggling the Most Significant Bit . Changing the leftmost '1' to '0' . 101001 ^ 100000 = 001001 ( 9 ) ; Left - shifting once to add an extra '0' to the right end of the binary number 001001 = 010010 ( 18 ) ; Toggling the '0' at the end to '1' which is essentially the same as putting the MSB at the rightmost place . 010010 | 1 = 010011 ( 19 ) ; hard coded driver main function to run the program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int msbPos ( int n ) { int pos = 0 ; while ( n != 0 ) { pos ++ ; n = n >> 1 ; } return pos ; } int josephify ( int n ) { int position = msbPos ( n ) ; int j = 1 << ( position - 1 ) ; n = n ^ j ; n = n << 1 ; n = n | 1 ; return n ; } int main ( ) { int n = 41 ; cout << josephify ( n ) ; return 0 ;
Count pairs with Odd XOR | C ++ program to count pairs in array whose XOR is odd ; A function will return number of pair whose XOR is odd ; To store count of XOR pair ; If XOR is odd increase count ; Return count ; Driver program to test countXorPair ( )
#include <iostream> NEW_LINE using namespace std ; int countXorPair ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] ^ arr [ j ] ) % 2 == 1 ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countXorPair ( arr , n ) ; return 0 ; }