codeparrot/xlcost-text-to-code huggingface dataset:
Count different numbers possible using all the digits their frequency times | C ++ implementation of the above approach ; Initialize an array to store factorial values ; Function to calculate and store X ! values ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function that return modular inverse of x under modulo p ; Function that returns the count of different number possible by using all the digits its frequency times ; Preprocess factorial values ; Initialize the result and sum of aint the frequencies ; Calculate the sum of frequencies ; Putting res equal to x ! ; Multiplying res with modular inverse of X0 ! , X1 ! , . . , X9 ! ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define MAXN  100000 NEW_LINE #define MOD  1000000007 NEW_LINE ll fact [ MAXN ] ; void factorial ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; } ll power ( ll x , ll y , ll p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } ll modInverse ( ll x , ll p ) { return power ( x , p - 2 , p ) ; } ll countDifferentNumbers ( ll arr [ ] , ll P ) { factorial ( ) ; ll res = 0 , X = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) X += arr [ i ] ; res = fact [ X ] ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( arr [ i ] > 1 ) res = ( res * modInverse ( fact [ arr [ i ] ] , P ) ) % P ; } return res ; } int main ( ) { ll arr [ ] = { 1 , 0 , 2 , 0 , 0 , 7 , 4 , 0 , 0 , 3 } ; cout << countDifferentNumbers ( arr , MOD ) ; return 0 ; }
Equation of straight line passing through a given point which bisects it into two equal line segments | C ++ implementation of the approach ; Function to print the equation of the required line ; Driver code
#include <iostream> NEW_LINE using namespace std ; void line ( double x0 , double y0 ) { double c = 2 * y0 * x0 ; cout << y0 << " x " << " ▁ + ▁ " << x0 << " y ▁ = ▁ " << c ; } int main ( ) { double x0 = 4 , y0 = 3 ; line ( x0 , y0 ) ; return 0 ; }
Find the original matrix when largest element in a row and a column are given | C ++ implementation of the approach ; Function that prints the original matrix ; Iterate in the row ; Iterate in the column ; If previously existed an element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE #define M  7 NEW_LINE void printOriginalMatrix ( int a [ ] , int b [ ] , int mat [ N ] [ M ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) cout << min ( a [ i ] , b [ j ] ) << " ▁ " ; else cout << 0 << " ▁ " ; } cout << endl ; } } int main ( ) { int a [ ] = { 2 , 1 , 3 } ; int b [ ] = { 2 , 3 , 0 , 0 , 2 , 0 , 1 } ; int mat [ N ] [ M ] = { { 1 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 , 0 } } ; printOriginalMatrix ( a , b , mat ) ; return 0 ; }
Calculate the loss incurred in selling the given items at discounted price | C ++ implementation of the approach ; Function to return the x % of n ; Function to return the total loss ; To store the total loss ; Original price of the item ; The price at which the item will be sold ; The discounted price of the item ; Loss incurred ; Driver code ; Total items
#include <bits/stdc++.h> NEW_LINE using namespace std ; float percent ( int n , int x ) { float p = n * x ; p /= 100 ; return p ; } float getLoss ( int price [ ] , int quantity [ ] , int X [ ] , int n ) { float loss = 0 ; for ( int i = 0 ; i < n ; i ++ ) { float originalPrice = price [ i ] ; float sellingPrice = originalPrice + percent ( originalPrice , X [ i ] ) ; float afterDiscount = sellingPrice - percent ( sellingPrice , X [ i ] ) ; loss += ( ( originalPrice - afterDiscount ) * quantity [ i ] ) ; } return loss ; } int main ( ) { int price [ ] = { 20 , 48 , 200 , 100 } ; int quantity [ ] = { 20 , 48 , 1 , 1 } ; int X [ ] = { 0 , 48 , 200 , 5 } ; int n = sizeof ( X ) / sizeof ( X [ 0 ] ) ; cout << getLoss ( price , quantity , X , n ) ; return 0 ; }
Maximum difference between two elements in an Array | C ++ implementation of the approach ; Function to return the maximum absolute difference between any two elements of the array ; To store the minimum and the maximum elements from the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxAbsDiff ( int arr [ ] , int n ) { int minEle = arr [ 0 ] ; int maxEle = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { minEle = min ( minEle , arr [ i ] ) ; maxEle = max ( maxEle , arr [ i ] ) ; } return ( maxEle - minEle ) ; } int main ( ) { int arr [ ] = { 2 , 1 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxAbsDiff ( arr , n ) ; return 0 ; }
Maximize the maximum subarray sum after removing atmost one element | C ++ implementation of the approach ; Function to return the maximum sub - array sum ; Initialized ; Traverse in the array ; Increase the sum ; If sub - array sum is more than the previous ; If sum is negative ; Function that returns the maximum sub - array sum after removing an element from the same sub - array ; Maximum sub - array sum using Kadane 's Algorithm ; Re - apply Kadane 's with minor changes ; Increase the sum ; If sub - array sum is greater than the previous ; If elements are 0 , no removal ; If elements are more , then store the minimum value in the sub - array obtained till now ; If sum is negative ; Re - initialize everything ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } int maximizeSum ( int a [ ] , int n ) { int cnt = 0 ; int mini = INT_MAX ; int minSubarray = INT_MAX ; int sum = maxSubArraySum ( a , n ) ; int max_so_far = INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; cnt ++ ; minSubarray = min ( a [ i ] , minSubarray ) ; if ( sum == max_ending_here ) { if ( cnt == 1 ) mini = min ( mini , 0 ) ; else mini = min ( mini , minSubarray ) ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; cnt = 0 ; minSubarray = INT_MAX ; } } return sum - mini ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , -2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maximizeSum ( a , n ) ; return 0 ; }
3 | C ++ implementation of the approach ; Function that returns true if n is an Osiris number ; 3 rd digit ; 2 nd digit ; 1 st digit ; Check the required condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isOsiris ( int n ) { int a = n % 10 ; int b = ( n / 10 ) % 10 ; int c = n / 100 ; int digit_sum = a + b + c ; if ( n == ( 2 * ( digit_sum ) * 11 ) ) { return true ; } return false ; } int main ( ) { int n = 132 ; if ( isOsiris ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Dudeney Numbers | C ++ implementation of the approach ; Function that returns true if n is a Dudeney number ; If n is not a perfect cube ; Last digit ; Update the digit sum ; Remove the last digit ; If cube root of n is not equal to the sum of its digits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDudeney ( int n ) { int cube_rt = int ( round ( ( pow ( n , 1.0 / 3.0 ) ) ) ) ; if ( cube_rt * cube_rt * cube_rt != n ) return false ; int dig_sum = 0 ; int temp = n ; while ( temp > 0 ) { int rem = temp % 10 ; dig_sum += rem ; temp /= 10 ; } if ( cube_rt != dig_sum ) return false ; return true ; } int main ( ) { int n = 17576 ; if ( isDudeney ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Number of triangles possible with given lengths of sticks which are powers of 2 | C ++ implementation of the approach ; Function to return the number of positive area triangles ; To store the count of total triangles ; To store the count of pairs of sticks with equal lengths ; Back - traverse and count the number of triangles ; Count the number of pairs ; If we have one remaining stick and we have a pair ; Count 1 triangle ; Reduce one pair ; Count the remaining triangles that can be formed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriangles ( int a [ ] , int n ) { int cnt = 0 ; int pairs = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { pairs += a [ i ] / 2 ; if ( a [ i ] % 2 == 1 && pairs > 0 ) { cnt += 1 ; pairs -= 1 ; } } cnt += ( 2 * pairs ) / 3 ; return cnt ; } int main ( ) { int a [ ] = { 1 , 2 , 2 , 2 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countTriangles ( a , n ) ; return 0 ; }
Smallest N digit number which is a multiple of 5 | C ++ implementation of the approach ; Function to return the smallest n digit number which is a multiple of 5 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestMultiple ( int n ) { if ( n == 1 ) return 5 ; return pow ( 10 , n - 1 ) ; } int main ( ) { int n = 4 ; cout << smallestMultiple ( n ) ; return 0 ; }
Find HCF of two numbers without using recursion or Euclidean algorithm | C ++ implementation of the approach ; Function to return the HCF of x and y ; Minimum of the two numbers ; If both the numbers are divisible by the minimum of these two then the HCF is equal to the minimum ; Highest number between 2 and minimum / 2 which can divide both the numbers is the required HCF ; If both the numbers are divisible by i ; 1 divides every number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int getHCF ( int x , int y ) { int minimum = min ( x , y ) ; if ( x % minimum == 0 && y % minimum == 0 ) return minimum ; for ( int i = minimum / 2 ; i >= 2 ; i -- ) { if ( x % i == 0 && y % i == 0 ) return i ; } return 1 ; } int main ( ) { int x = 16 , y = 32 ; cout << getHCF ( x , y ) ; return 0 ; }
Check if product of first N natural numbers is divisible by their sum | C ++ implementation of the approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that return true if the product of the first n natural numbers is divisible by the sum of first n natural numbers ; Driver code
#include <bits/stdc++.h> 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 isDivisible ( int n ) { if ( isPrime ( n + 1 ) ) return false ; return true ; } int main ( ) { int n = 6 ; if ( isDivisible ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximum sum of cocktail glass in a 2D matrix | C ++ implementation of the approach ; Function to return the maximum sum of a cocktail glass ; If no cocktail glass is possible ; Initialize max_sum with the mini ; Here loop runs ( R - 2 ) * ( C - 2 ) times considering different top left cells of cocktail glasses ; Considering mat [ i ] [ j ] as the top left cell of the cocktail glass ; Update the max_sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int R = 5 ; const int C = 5 ; int findMaxCock ( int ar [ R ] [ C ] ) { if ( R < 3 C < 3 ) return -1 ; int max_sum = INT_MIN ; for ( int i = 0 ; i < R - 2 ; i ++ ) { for ( int j = 0 ; j < C - 2 ; j ++ ) { int sum = ( ar [ i ] [ j ] + ar [ i ] [ j + 2 ] ) + ( ar [ i + 1 ] [ j + 1 ] ) + ( ar [ i + 2 ] [ j ] + ar [ i + 2 ] [ j + 1 ] + ar [ i + 2 ] [ j + 2 ] ) ; max_sum = max ( max_sum , sum ) ; } } return max_sum ; } int main ( ) { int ar [ ] [ C ] = { { 0 , 3 , 0 , 6 , 0 } , { 0 , 1 , 1 , 0 , 0 } , { 1 , 1 , 1 , 0 , 0 } , { 0 , 0 , 2 , 0 , 1 } , { 0 , 2 , 0 , 1 , 3 } } ; cout << findMaxCock ( ar ) ; return 0 ; }
Find the number of sub arrays in the permutation of first N natural numbers such that their median is M | C ++ implementation of the approach ; Function to return the count of sub - arrays in the given permutation of first n natural numbers such that their median is m ; If element is less than m ; If element greater than m ; If m is found ; Count the answer ; Increment sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int segments ( int n , int p [ ] , int m ) { map < int , int > c ; c [ 0 ] = 1 ; bool has = false ; int sum = 0 ; long long ans = 0 ; for ( int r = 0 ; r < n ; r ++ ) { if ( p [ r ] < m ) sum -- ; else if ( p [ r ] > m ) sum ++ ; if ( p [ r ] == m ) has = true ; if ( has ) ans += c [ sum ] + c [ sum - 1 ] ; else c [ sum ] ++ ; } return ans ; } int main ( ) { int a [ ] = { 2 , 4 , 5 , 3 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int m = 4 ; cout << segments ( n , a , m ) ; return 0 ; }
Program to calculate the number of odd days in given number of years | C ++ implementation of the approach ; Function to return the count of odd days ; Count of years divisible by 100 and 400 ; Every 4 th year is a leap year ; Every 100 th year is divisible by 4 but is not a leap year ; Every 400 th year is divisible by 100 but is a leap year ; Total number of extra days ; modulo ( 7 ) for final answer ; Driver code ; Number of days
#include <iostream> NEW_LINE using namespace std ; int oddDays ( int N ) { int hund1 = N / 100 ; int hund4 = N / 400 ; int leap = N >> 2 ; int ord = N - leap ; if ( hund1 ) { ord += hund1 ; leap -= hund1 ; } if ( hund4 ) { ord -= hund4 ; leap += hund4 ; } int days = ord + leap * 2 ; int odd = days % 7 ; return odd ; } int main ( ) { int N = 100 ; cout << oddDays ( N ) ; return 0 ; }
Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle | C ++ Program to find the biggest ellipse which can be inscribed within a rectangle which in turn is inscribed within a semicircle ; Function to find the area of the biggest ellipse ; the radius cannot be negative ; area of the ellipse ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float ellipsearea ( float r ) { if ( r < 0 ) return -1 ; float a = ( 3.14 * r * r ) / 4 ; return a ; } int main ( ) { float r = 5 ; cout << ellipsearea ( r ) << endl ; return 0 ; }
Find the minimum number of operations required to make all array elements equal | C ++ implementation of the approach ; Function to return the minimum operations required to make all array elements equal ; To store the frequency of all the array elements ; Traverse through array elements and update frequencies ; To store the maximum frequency of an element from the array ; Traverse through the map and find the maximum frequency for any element ; Return the minimum operations required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; int maxFreq = INT_MIN ; for ( auto x : mp ) maxFreq = max ( maxFreq , x . second ) ; return ( n - maxFreq ) ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperations ( arr , n ) ; 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 ; Convert all prefixes to decimal ; If number is divisible by x then increase count ; 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 ] ; if ( ( number % x == 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 ; }
Count consecutive pairs of same elements | C ++ implementation of the approach ; Function to return the count of consecutive elements in the array which are equal ; If consecutive elements are same ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countCon ( int ar [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) cnt ++ ; } return cnt ; } int main ( ) { int ar [ ] = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 5 , 5 , 5 } ; int n = sizeof ( ar ) / sizeof ( ar [ 0 ] ) ; cout << countCon ( ar , n ) ; return 0 ; }
Reduce the fraction to its lowest form | C ++ program to reduce a fraction x / y to its lowest form ; Function to reduce a fraction to its lowest form ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reduceFraction ( int x , int y ) { int d ; d = __gcd ( x , y ) ; x = x / d ; y = y / d ; cout << " x ▁ = ▁ " << x << " , ▁ y ▁ = ▁ " << y << endl ; } int main ( ) { int x = 16 ; int y = 10 ; reduceFraction ( x , y ) ; return 0 ; }
Number of ways of choosing K equal substrings of any length for every query | C ++ implementation of the approach ; Function to generate all the sub - strings ; Length of the string ; Generate all sub - strings ; Count the occurrence of every sub - string ; Compute the Binomial Coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the result for a query ; Iterate for every unique sub - string ; Count the combinations ; Driver code ; Get all the sub - strings Store the occurrence of all the sub - strings ; Pre - computation ; Queries ; Perform queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxlen  100 NEW_LINE void generateSubStrings ( string s , unordered_map < string , int > & mpp ) { int l = s . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { string temp = " " ; for ( int j = i ; j < l ; j ++ ) { temp += s [ j ] ; mpp [ temp ] += 1 ; } } } void binomialCoeff ( int C [ maxlen ] [ maxlen ] ) { int i , j ; for ( i = 0 ; i < 100 ; i ++ ) { for ( j = 0 ; j < 100 ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } } int answerQuery ( unordered_map < string , int > & mpp , int C [ maxlen ] [ maxlen ] , int k ) { int ans = 0 ; for ( auto it : mpp ) { if ( it . second >= k ) ans += C [ it . second ] [ k ] ; } return ans ; } int main ( ) { string s = " aabaab " ; unordered_map < string , int > mpp ; generateSubStrings ( s , mpp ) ; int C [ maxlen ] [ maxlen ] ; memset ( C , 0 , sizeof C ) ; binomialCoeff ( C ) ; int queries [ ] = { 2 , 3 , 4 } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << answerQuery ( mpp , C , queries [ i ] ) << endl ; return 0 ; }
Times required by Simple interest for the Principal to become Y times itself | C ++ implementation of the approach ; Function to return the no . of years ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float noOfYears ( int t1 , int n1 , int t2 ) { float years = ( ( t2 - 1 ) * n1 / ( float ) ( t1 - 1 ) ) ; return years ; } int main ( ) { int T1 = 3 , N1 = 5 , T2 = 6 ; cout << noOfYears ( T1 , N1 , T2 ) ; return 0 ; }
Check if a given number divides the sum of the factorials of its digits | C ++ implementation of the approach ; Function that returns true if n divides the sum of the factorials of its digits ; To store factorials of digits ; To store sum of the factorials of the digits ; Store copy of the given number ; Store sum of the factorials of the digits ; If it is divisible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int n ) { int fac [ 10 ] ; fac [ 0 ] = fac [ 1 ] = 1 ; for ( int i = 2 ; i < 10 ; i ++ ) fac [ i ] = fac [ i - 1 ] * i ; int sum = 0 ; int x = n ; while ( x ) { sum += fac [ x % 10 ] ; x /= 10 ; } if ( sum % n == 0 ) return true ; return false ; } int main ( ) { int n = 19 ; if ( isPossible ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the longest subsequence of an array having LCM at most K | C ++ implementation of the approach ; Function to find the longest subsequence having LCM less than or equal to K ; Map to store unique elements and their frequencies ; Update the frequencies ; Array to store the count of numbers whom 1 <= X <= K is a multiple of ; Check every unique element ; Find all its multiples <= K ; Store its frequency ; Obtain the number having maximum count ; Condition to check if answer doesn 't exist ; Print the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubsequence ( int * arr , int n , int k ) { map < int , int > M ; for ( int i = 0 ; i < n ; ++ i ) ++ M [ arr [ i ] ] ; int * numCount = new int [ k + 1 ] ; for ( int i = 0 ; i <= k ; ++ i ) numCount [ i ] = 0 ; for ( auto p : M ) { if ( p . first <= k ) { for ( int i = 1 ; ; ++ i ) { if ( p . first * i > k ) break ; numCount [ p . first * i ] += p . second ; } } else break ; } int lcm = 0 , length = 0 ; for ( int i = 1 ; i <= k ; ++ i ) { if ( numCount [ i ] > length ) { length = numCount [ i ] ; lcm = i ; } } if ( lcm == 0 ) cout << -1 << endl ; else { cout << " LCM ▁ = ▁ " << lcm << " , ▁ Length ▁ = ▁ " << length << endl ; cout << " Indexes ▁ = ▁ " ; for ( int i = 0 ; i < n ; ++ i ) if ( lcm % arr [ i ] == 0 ) cout << i << " ▁ " ; } } int main ( ) { int k = 14 ; int arr [ ] = { 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findSubsequence ( arr , n , k ) ; return 0 ; }
Count number of binary strings of length N having only 0 ' s ▁ and ▁ 1' s | C ++ implementation of the approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to count the number of binary strings of length N having only 0 ' s ▁ and ▁ 1' s ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define mod  (ll)(1e9 + 7) NEW_LINE ll power ( ll x , ll y , ll p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } ll findCount ( ll N ) { int count = power ( 2 , N , mod ) ; return count ; } int main ( ) { ll N = 25 ; cout << findCount ( N ) ; return 0 ; }
XOR of all the elements in the given range [ L , R ] | C ++ implementation of the approach ; Function to return the most significant bit ; Function to return the required XOR ; Finding the MSB ; Value of the current bit to be added ; To store the final answer ; Loop for case 1 ; Edge case when both the integers lie in the same segment of continuous 1 s ; To store whether parity of count is odd ; Updating the answer if parity is odd ; Updating the number to be added ; Case 2 ; Driver code ; Final answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int msb ( int x ) { int ret = 0 ; while ( ( x >> ( ret + 1 ) ) != 0 ) ret ++ ; return ret ; } int xorRange ( int l , int r ) { int max_bit = msb ( r ) ; int mul = 2 ; int ans = 0 ; for ( int i = 1 ; i <= max_bit ; i ++ ) { if ( ( l / mul ) * mul == ( r / mul ) * mul ) { if ( ( ( l & ( 1 << i ) ) != 0 ) && ( r - l + 1 ) % 2 == 1 ) ans += mul ; mul *= 2 ; continue ; } bool odd_c = 0 ; if ( ( ( l & ( 1 << i ) ) != 0 ) && l % 2 == 1 ) odd_c = ( odd_c ^ 1 ) ; if ( ( ( r & ( 1 << i ) ) != 0 ) && r % 2 == 0 ) odd_c = ( odd_c ^ 1 ) ; if ( odd_c ) ans += mul ; mul *= 2 ; } int zero_bit_cnt = zero_bit_cnt = ( r - l + 1 ) / 2 ; if ( l % 2 == 1 && r % 2 == 1 ) zero_bit_cnt ++ ; if ( zero_bit_cnt % 2 == 1 ) ans ++ ; return ans ; } int main ( ) { int l = 1 , r = 4 ; cout << xorRange ( l , r ) ; return 0 ; }