codeparrot/xlcost-text-to-code huggingface dataset:
Find the sum of elements of the Matrix generated by the given rules | C ++ implementation of the approach ; Function to return the required sum ; To store the sum ; For every row ; Update the sum as A appears i number of times in the current row ; Update A for the next row ; Return the sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; int sum ( int A , int B , int R ) { int sum = 0 ; for ( int i = 1 ; i <= R ; i ++ ) { sum = sum + ( i * A ) ; A = A + B ; } return sum ; } int main ( ) { int A = 5 , B = 3 , R = 3 ; cout << sum ( A , B , R ) ; return 0 ; }
Count total set bits in all numbers from 1 to n | Set 2 | C ++ implementation of the approach ; Function to return the sum of the count of set bits in the integers from 1 to n ; Ignore 0 as all the bits are unset ; To store the powers of 2 ; To store the result , it is initialized with n / 2 because the count of set least significant bits in the integers from 1 to n is n / 2 ; Loop for every bit required to represent n ; Total count of pairs of 0 s and 1 s ; totalPairs / 2 gives the complete count of the pairs of 1 s Multiplying it with the current power of 2 will give the count of 1 s in the current bit ; If the count of pairs was odd then add the remaining 1 s which could not be groupped together ; Next power of 2 ; Return the result ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countSetBits ( int n ) { n ++ ; int powerOf2 = 2 ; int cnt = n / 2 ; while ( powerOf2 <= n ) { int totalPairs = n / powerOf2 ; cnt += ( totalPairs / 2 ) * powerOf2 ; cnt += ( totalPairs & 1 ) ? ( n % powerOf2 ) : 0 ; powerOf2 <<= 1 ; } return cnt ; } int main ( ) { int n = 14 ; cout << countSetBits ( n ) ; return 0 ; }
Find the height of a right | C ++ implementation of the approach ; Function to return the height of the right - angled triangle whose area is X times its base ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getHeight ( int X ) { return ( 2 * X ) ; } int main ( ) { int X = 35 ; cout << getHeight ( X ) ; return 0 ; }
Find sum of inverse of the divisors when sum of divisors and the number is given | C ++ implementation of above approach ; Function to return the sum of inverse of divisors ; Calculating the answer ; Return the answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double SumofInverseDivisors ( int N , int Sum ) { double ans = ( double ) ( Sum ) * 1.0 / ( double ) ( N ) ; return ans ; } int main ( ) { int N = 9 ; int Sum = 13 ; cout << setprecision ( 2 ) << fixed << SumofInverseDivisors ( N , Sum ) ; return 0 ; }
Number of triplets such that each value is less than N and each pair sum is a multiple of K | C ++ implementation of the above approach ; Function to return the number of triplets ; Initializing the count array ; Storing the frequency of each modulo class ; If K is odd ; If K is even ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int NoofTriplets ( int N , int K ) { int cnt [ K ] ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 1 ; i <= N ; i += 1 ) { cnt [ i % K ] += 1 ; } if ( K & 1 ) return cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] ; else { return ( cnt [ 0 ] * cnt [ 0 ] * cnt [ 0 ] + cnt [ K / 2 ] * cnt [ K / 2 ] * cnt [ K / 2 ] ) ; } } int main ( ) { int N = 3 , K = 2 ; cout << NoofTriplets ( N , K ) ; return 0 ; }
Find a number containing N | C ++ implementation of the approach ; Function to compute number using our deduced formula ; Initialize num to n - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll findNumber ( int n ) { ll num = n - 1 ; num = 2 * ( ll ) pow ( 4 , num ) ; num = floor ( num / 3.0 ) ; return num ; } int main ( ) { int n = 5 ; cout << findNumber ( n ) ; return 0 ; }
Find XOR of numbers from the range [ L , R ] | C ++ implementation of the approach ; Function to return the XOR of elements from the range [ 1 , n ] ; If n is a multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to return the XOR of elements from the range [ l , r ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findXOR ( int n ) { int mod = n % 4 ; if ( mod == 0 ) return n ; else if ( mod == 1 ) return 1 ; else if ( mod == 2 ) return n + 1 ; else if ( mod == 3 ) return 0 ; } int findXOR ( int l , int r ) { return ( findXOR ( l - 1 ) ^ findXOR ( r ) ) ; } int main ( ) { int l = 4 , r = 8 ; cout << findXOR ( l , r ) ; return 0 ; }
Number of elements from the array which are reachable after performing given operations on D | C ++ implementation of the approach ; Function to return the GCD of a and b ; Function to return the count of reachable integers from the given array ; GCD of A and B ; To store the count of reachable integers ; If current element can be reached ; Return the count ; Driver code
#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 findReachable ( int arr [ ] , int D , int A , int B , int n ) { int gcd_AB = GCD ( A , B ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - D ) % gcd_AB == 0 ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 4 , 5 , 6 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int D = 4 , A = 4 , B = 6 ; cout << findReachable ( arr , D , A , B , n ) ; return 0 ; }
Number of trees whose sum of degrees of all the vertices is L | C ++ implementation of the approach ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Function to return the count of required trees ; number of nodes ; Return the result ; Driver code
#include <iostream> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll power ( int x , ll y ) { ll res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } ll solve ( int L ) { int n = L / 2 + 1 ; ll ans = power ( n , n - 2 ) ; return ans ; } int main ( ) { int L = 6 ; cout << solve ( L ) ; return 0 ; }
Change one element in the given array to make it an Arithmetic Progression | C ++ program to change one element of an array such that the resulting array is in arithmetic progression . ; Finds the initial term and common difference and prints the resulting sequence . ; Check if the first three elements are in arithmetic progression ; Check if the first element is not in arithmetic progression ; The first and fourth element are in arithmetic progression ; Print the arithmetic progression ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void makeAP ( int arr [ ] , int n ) { int initial_term , common_difference ; if ( n == 3 ) { common_difference = arr [ 2 ] - arr [ 1 ] ; initial_term = arr [ 1 ] - common_difference ; } else if ( ( arr [ 1 ] - arr [ 0 ] ) == arr [ 2 ] - arr [ 1 ] ) { initial_term = arr [ 0 ] ; common_difference = arr [ 1 ] - arr [ 0 ] ; } else if ( ( arr [ 2 ] - arr [ 1 ] ) == ( arr [ 3 ] - arr [ 2 ] ) ) { common_difference = arr [ 2 ] - arr [ 1 ] ; initial_term = arr [ 1 ] - common_difference ; } else { common_difference = ( arr [ 3 ] - arr [ 0 ] ) / 3 ; initial_term = arr [ 0 ] ; } for ( int i = 0 ; i < n ; i ++ ) cout << initial_term + ( i * common_difference ) << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 1 , 3 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; makeAP ( arr , n ) ; return 0 ; }
Find if nCr is divisible by the given prime | C ++ implementation of the approach ; Function to return the highest power of p that divides n ! implementing Legendre Formula ; Return the highest power of p which divides n ! ; Function that returns true if nCr is divisible by p ; Find the highest powers of p that divide n ! , r ! and ( n - r ) ! ; If nCr is divisible by p ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; int getfactor ( int n , int p ) { int pw = 0 ; while ( n ) { n /= p ; pw += n ; } return pw ; } bool isDivisible ( int n , int r , int p ) { int x1 = getfactor ( n , p ) ; int x2 = getfactor ( r , p ) ; int x3 = getfactor ( n - r , p ) ; if ( x1 > x2 + x3 ) return true ; return false ; } int main ( ) { int n = 7 , r = 2 , p = 7 ; if ( isDivisible ( n , r , p ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if the number is even or odd whose digits and base ( radix ) is given | C ++ implementation of the approach ; Function that returns true if the number represented by arr [ ] is even in base r ; If the base is even , then the last digit is checked ; If base is odd , then the number of odd digits are checked ; To store the count of odd digits ; Number is odd ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isEven ( int arr [ ] , int n , int r ) { if ( r % 2 == 0 ) { if ( arr [ n - 1 ] % 2 == 0 ) return true ; } else { int oddCount = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount % 2 == 0 ) return true ; } return false ; } int main ( ) { int arr [ ] = { 1 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int r = 2 ; if ( isEven ( arr , n , r ) ) cout << " Even " ; else cout << " Odd " ; return 0 ; }
Bitwise AND of sub | C ++ implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; No need to perform more AND operations as | k - X | will increase ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int closetAND ( int arr [ ] , int n , int k ) { int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = min ( ans , abs ( k - X ) ) ; if ( X <= k ) break ; } } return ans ; } int main ( ) { int arr [ ] = { 4 , 7 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << closetAND ( arr , n , k ) ; return 0 ; }
Count of quadruplets from range [ L , R ] having GCD equal to K | C ++ implementation of the approach ; Function to return the gcd of a and b ; Function to return the count of quadruplets having gcd = k ; Count the frequency of every possible gcd value in the range ; To store the required count ; Calculate the answer using frequency values ; Return the required count ; Driver code
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int countQuadruplets ( int l , int r , int k ) { int frequency [ r + 1 ] = { 0 } ; for ( int i = l ; i <= r ; i ++ ) { for ( int j = l ; j <= r ; j ++ ) { frequency [ gcd ( i , j ) ] ++ ; } } long long answer = 0 ; for ( int i = 1 ; i <= r ; i ++ ) { for ( int j = 1 ; j <= r ; j ++ ) { if ( gcd ( i , j ) == k ) { answer += ( frequency [ i ] * frequency [ j ] ) ; } } } return answer ; } int main ( ) { int l = 1 , r = 10 , k = 2 ; cout << countQuadruplets ( l , r , k ) ; return 0 ; }
Rearrange the array to maximize the number of primes in prefix sum of the array | C ++ implementation of the approach ; Function to print the re - arranged array ; Count the number of ones and twos in a [ ] ; If the array element is 1 ; Array element is 2 ; If it has at least one 2 Fill up first 2 ; Decrease the cnt of ones if even ; Fill up with odd count of ones ; Fill up with remaining twos ; If even ones , then fill last position ; Print the rearranged array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int a [ ] , int n ) { int ones = 0 , twos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) ones ++ ; else twos ++ ; } int ind = 0 ; if ( twos ) a [ ind ++ ] = 2 ; bool evenOnes = ( ones % 2 == 0 ) ? true : false ; if ( evenOnes ) ones -= 1 ; for ( int i = 0 ; i < ones ; i ++ ) a [ ind ++ ] = 1 ; for ( int i = 0 ; i < twos - 1 ; i ++ ) a [ ind ++ ] = 2 ; if ( evenOnes ) a [ ind ++ ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] << " ▁ " ; } int main ( ) { int a [ ] = { 1 , 2 , 1 , 2 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; solve ( a , n ) ; return 0 ; }
Generate an Array in which count of even and odd sum sub | C ++ implementation of the approach ; Function to generate and print the required array ; Find the number of odd prefix sums ; If no odd prefix sum found ; Calculating the number of even prefix sums ; Stores the current prefix sum ; If current prefix sum is even ; Print 0 until e = EvenPreSums - 1 ; Print 1 when e = EvenPreSums ; Print 0 for rest of the values ; Driver code
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void CreateArray ( int N , int even , int odd ) { int temp = -1 ; int OddPreSums ; for ( int i = 0 ; i <= N + 1 ; i ++ ) { if ( i * ( ( N + 1 ) - i ) == odd ) { temp = 0 ; OddPreSums = i ; break ; } } if ( temp == -1 ) { cout << temp << endl ; } else { int EvenPreSums = ( N + 1 ) - OddPreSums ; int e = 1 ; int o = 0 ; int CurrSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( CurrSum % 2 == 0 ) { if ( e < EvenPreSums ) { e ++ ; cout << "0 ▁ " ; } else { o ++ ; cout << "1 ▁ " ; CurrSum ++ ; } } else { if ( e < EvenPreSums ) { e ++ ; cout << "1 ▁ " ; CurrSum ++ ; } else { o ++ ; cout << "0 ▁ " ; } } } cout << endl ; } } int main ( ) { int N = 15 ; int even = 60 , odd = 60 ; CreateArray ( N , even , odd ) ; return 0 ; }
Minimum operations required to change the array such that | arr [ i ] | C ++ implementation of the approach ; Function to return the minimum number of operations required ; Minimum and maximum elements from the array ; To store the minimum number of operations required ; To store the number of operations required to change every element to either ( num - 1 ) , num or ( num + 1 ) ; If current element is not already num ; Add the count of operations required to change arr [ i ] ; Update the minimum operations so far ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int changeTheArray ( int arr [ ] , int n ) { int minEle = * ( std :: min_element ( arr , arr + n ) ) ; int maxEle = * ( std :: max_element ( arr , arr + n ) ) ; int minOperations = INT_MAX ; for ( int num = minEle ; num <= maxEle ; num ++ ) { int operations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != num ) { operations += ( abs ( num - arr [ i ] ) - 1 ) ; } } minOperations = min ( minOperations , operations ) ; } return minOperations ; } int main ( ) { int arr [ ] = { 10 , 1 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << changeTheArray ( arr , n ) ; return 0 ; }
Choose X such that ( A xor X ) + ( B xor X ) is minimized | C ++ implementation of the approach ; Function to return the integer X such that ( A xor X ) + ( B ^ X ) is minimized ; While either A or B is non - zero ; Position at which both A and B have a set bit ; Inserting a set bit in x ; Right shifting both numbers to traverse all the bits ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findX ( int A , int B ) { int j = 0 , x = 0 ; while ( A B ) { if ( ( A & 1 ) && ( B & 1 ) ) { x += ( 1 << j ) ; } A >>= 1 ; B >>= 1 ; j += 1 ; } return x ; } int main ( ) { int A = 2 , B = 3 ; int X = findX ( A , B ) ; cout << " X ▁ = ▁ " << X << " , ▁ Sum ▁ = ▁ " << ( A ^ X ) + ( B ^ X ) ; return 0 ; }
Choose X such that ( A xor X ) + ( B xor X ) is minimized | c ++ implementation of above approach ; finding X ; finding Sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findX ( int A , int B ) { return A & B ; } int findSum ( int A , int B ) { return A ^ B ; } int main ( ) { int A = 2 , B = 3 ; cout << " X ▁ = ▁ " << findX ( A , B ) << " , ▁ Sum ▁ = ▁ " << findSum ( A , B ) ; return 0 ; }
Compare sum of first N | C ++ implementation of the approach ; Function that returns true if sum of first n - 1 elements of the array is equal to the last element ; Find the sum of first n - 1 elements of the array ; If sum equals to the last element ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isSumEqual ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) sum += ar [ i ] ; if ( sum == ar [ n - 1 ] ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isSumEqual ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count number of 1 s in the array after N moves | C ++ implementation of the above approach ; Function to count number of perfect squares ; Counting number of perfect squares between a and b ; Function to count number of 1 s in array after N moves ; Driver Code ; Initialize array size ; Initialize all elements to 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; int perfectSquares ( int a , int b ) { return ( floor ( sqrt ( b ) ) - ceil ( sqrt ( a ) ) + 1 ) ; } int countOnes ( int arr [ ] , int n ) { return perfectSquares ( 1 , n ) ; } int main ( ) { int N = 10 ; int arr [ 10 ] = { 0 } ; cout << countOnes ( arr , N ) ; return 0 ; }
Find the position of box which occupies the given ball | C ++ implementation of the approach ; Function to print the position of each boxes where a ball has to be placed ; Find the cumulative sum of array A [ ] ; Find the position of box for each ball ; Row number ; Column ( position of box in particular row ) ; Row + 1 denotes row if indexing of array start from 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPosition ( int A [ ] , int B [ ] , int sizeOfA , int sizeOfB ) { for ( int i = 1 ; i < sizeOfA ; i ++ ) A [ i ] += A [ i - 1 ] ; for ( int i = 0 ; i < sizeOfB ; i ++ ) { int row = lower_bound ( A , A + sizeOfA , B [ i ] ) - A ; int boxNumber = ( row >= 1 ) ? B [ i ] - A [ row - 1 ] : B [ i ] ; cout << row + 1 << " , ▁ " << boxNumber << " STRNEWLINE " ; } } int main ( ) { int A [ ] = { 2 , 2 , 2 , 2 } ; int B [ ] = { 1 , 2 , 3 , 4 } ; int sizeOfA = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int sizeOfB = sizeof ( B ) / sizeof ( B [ 0 ] ) ; printPosition ( A , B , sizeOfA , sizeOfB ) ; return 0 ; }
Highest power of a number that divides other number | C ++ program to implement the above approach ; Function to get the prime factors and its count of times it divides ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , count i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to return the highest power ; Initialize two arrays ; Get the prime factors of n and m ; Iterate and find the maximum power ; If i not a prime factor of n and m ; If i is a prime factor of n and m If count of i dividing m is more than i dividing n , then power will be 0 ; If i is a prime factor of M ; get the maximum power ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void primeFactors ( int n , int freq [ ] ) { int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } freq [ 2 ] = cnt ; for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { cnt = 0 ; while ( n % i == 0 ) { cnt ++ ; n = n / i ; } freq [ i ] = cnt ; } if ( n > 2 ) freq [ n ] = 1 ; } int getMaximumPower ( int n , int m ) { int freq1 [ n + 1 ] , freq2 [ m + 1 ] ; memset ( freq1 , 0 , sizeof freq1 ) ; memset ( freq2 , 0 , sizeof freq2 ) ; primeFactors ( n , freq1 ) ; primeFactors ( m , freq2 ) ; int maxi = 0 ; for ( int i = 2 ; i <= m ; i ++ ) { if ( freq1 [ i ] == 0 && freq2 [ i ] == 0 ) continue ; if ( freq2 [ i ] > freq1 [ i ] ) return 0 ; if ( freq2 [ i ] ) { maxi = max ( maxi , freq1 [ i ] / freq2 [ i ] ) ; } } return maxi ; } int main ( ) { int n = 48 , m = 4 ; cout << getMaximumPower ( n , m ) ; return 0 ; }
Find the number of divisors of all numbers in the range [ 1 , n ] | C ++ implementation of the approach ; Function to find the number of divisors of all numbers in the range [ 1 , n ] ; Array to store the count of divisors ; For every number from 1 to n ; Increase divisors count for every number divisible by i ; Print the divisors ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findDivisors ( int n ) { int div [ n + 1 ] ; memset ( div , 0 , sizeof div ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j * i <= n ; j ++ ) div [ i * j ] ++ ; } for ( int i = 1 ; i <= n ; i ++ ) cout << div [ i ] << " ▁ " ; } int main ( ) { int n = 10 ; findDivisors ( n ) ; return 0 ; }
Predict the winner of the game on the basis of absolute difference of sum by selecting numbers | C ++ implementation of the approach ; Function to decide the winner ; Iterate for all numbers in the array ; If mod gives 0 ; If mod gives 1 ; If mod gives 2 ; If mod gives 3 ; Check the winning condition for X ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int decideWinner ( int a [ ] , int n ) { int count0 = 0 ; int count1 = 0 ; int count2 = 0 ; int count3 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 4 == 0 ) count0 ++ ; else if ( a [ i ] % 4 == 1 ) count1 ++ ; else if ( a [ i ] % 4 == 2 ) count2 ++ ; else if ( a [ i ] % 4 == 3 ) count3 ++ ; } if ( count0 % 2 == 0 && count1 % 2 == 0 && count2 % 2 == 0 && count3 == 0 ) return 1 ; else return 2 ; } int main ( ) { int a [ ] = { 4 , 8 , 5 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( decideWinner ( a , n ) == 1 ) cout << " X ▁ wins " ; else cout << " Y ▁ wins " ; return 0 ; }
Count all prefixes of the given binary array which are divisible by x | C ++ implementation of the approach ; Function to return the count of total binary prefix which are divisible by x ; Initialize with zero ; Instead of converting all prefixes to decimal , take reminder with x ; If number is divisible by x then reminder = 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CntDivbyX ( int arr [ ] , int n , int x ) { int number = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { number = ( number * 2 + arr [ i ] ) % x ; if ( number == 0 ) count += 1 ; } return count ; } int main ( ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 2 ; cout << CntDivbyX ( arr , n , x ) ; return 0 ; }
Length of the smallest number which is divisible by K and formed by using 1 's only | C ++ implementation of the approach ; Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Instead of generating all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's  Take remainder with K ; If number is divisible by k then remainder will be 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) return -1 ; int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = ( number * 10 + 1 ) % K ; if ( number == 0 ) return len ; } return -1 ; } int main ( ) { int K = 7 ; cout << numLen ( K ) ; return 0 ; }