codeparrot/xlcost-text-to-code huggingface dataset:
Find Nth positive number whose digital root is X | C ++ program to find the N - th number whose digital root is X ; Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findDigitalRoot ( int num ) { int sum = INT_MAX , tempNum = num ; while ( sum >= 10 ) { sum = 0 ; while ( tempNum > 0 ) { sum += tempNum % 10 ; tempNum /= 10 ; } tempNum = sum ; } return sum ; } void findAnswer ( int X , int N ) { int counter = 0 ; for ( int i = 1 ; counter < N ; ++ i ) { int digitalRoot = findDigitalRoot ( i ) ; if ( digitalRoot == X ) { ++ counter ; } if ( counter == N ) { cout << i ; break ; } } } int main ( ) { int X = 1 , N = 3 ; findAnswer ( X , N ) ; return 0 ; }
Find Nth positive number whose digital root is X | C ++ program to find the N - th number with digital root as X ; Function to find the N - th number with digital root as X ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findAnswer ( int X , int N ) { return ( N - 1 ) * 9 + X ; } int main ( ) { int X = 7 , N = 43 ; cout << findAnswer ( X , N ) ; return 0 ; }
Sum of the natural numbers ( up to N ) whose modulo with K yield R | C ++ implementation of the approach ; Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int count ( int N , int K , int R ) { long long int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % K == R ) sum += i ; } return sum ; } int main ( ) { int N = 20 , K = 4 , R = 3 ; cout << count ( N , K , R ) ; return 0 ; }
Longest sub | C ++ implementation of the approach ; Function to return the length of the longest required sub - sequence ; Find the maximum element from the array ; Insert all lucas numbers below max to the set a and b are first two elements of the Lucas sequence ; If current element is a Lucas number , increment count ; Return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LucasSequence ( int arr [ ] , int n ) { int max = * max_element ( arr , arr + n ) ; unordered_set < int > s ; int a = 2 , b = 1 , c ; s . insert ( a ) ; s . insert ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; s . insert ( b ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { auto it = s . find ( arr [ i ] ) ; if ( it != s . end ( ) ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << LucasSequence ( arr , n ) ; return 0 ; }
Find the number of solutions to the given equation | C ++ implementation of the approach ; Function to return the count of valid values of X ; Iterate through all possible sum of digits of X ; Get current value of X for sum of digits i ; Find sum of digits of cr ; If cr is a valid choice for X ; Return the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int a , int b , int c ) { int count = 0 ; for ( int i = 1 ; i <= 81 ; i ++ ) { int cr = b * pow ( i , a ) + c ; int tmp = cr ; int sm = 0 ; while ( tmp ) { sm += tmp % 10 ; tmp /= 10 ; } if ( sm == i && cr < 1e9 ) count ++ ; } return count ; } int main ( ) { int a = 3 , b = 2 , c = 8 ; cout << getCount ( a , b , c ) ; return 0 ; }
Check if an array of 1 s and 2 s can be divided into 2 parts with equal sum | C ++ implementation of the above approach : ; Function to check if it is possible to split the array in two parts with equal sum ; Calculate sum of elements and count of 1 's ; If total sum is odd , return False ; If sum of each part is even , return True ; If sum of each part is even but there is atleast one 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSpiltPossible ( int n , int a [ ] ) { int sum = 0 , c1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; if ( a [ i ] == 1 ) { c1 ++ ; } } if ( sum % 2 ) return false ; if ( ( sum / 2 ) % 2 == 0 ) return true ; if ( c1 > 0 ) return true ; else return false ; } int main ( ) { int n = 3 ; int a [ ] = { 1 , 1 , 2 } ; if ( isSpiltPossible ( n , a ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Make a tree with n vertices , d diameter and at most vertex degree k | C ++ implementation of the approach ; Function to Make a tree with n vertices , d as it 's diameter and degree of each vertex is at most k ; If diameter > n - 1 impossible to build tree ; To store the degree of each vertex ; To store the edge between vertices ; To store maximum distance among all the paths from a vertex ; Make diameter of tree equals to d ; Add an edge between them ; Store maximum distance of each vertex from other vertices ; For next ( n - d - 1 ) edges ; If the vertex already has the degree k ; If not possible ; Increase the degree of vertices ; Add an edge between them ; Store the maximum distance of this vertex from other vertices ; Print all the edges of the built tree ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Make_tree ( int n , int d , int k ) { if ( d > n - 1 ) { cout << " No " ; return ; } vector < int > deg ( n ) ; vector < pair < int , int > > ans ; set < pair < int , int > > q ; for ( int i = 0 ; i < d ; ++ i ) { ++ deg [ i ] ; ++ deg [ i + 1 ] ; if ( deg [ i ] > k deg [ i + 1 ] > k ) { cout << " NO " << endl ; return ; } ans . push_back ( make_pair ( i , i + 1 ) ) ; } for ( int i = 1 ; i < d ; ++ i ) q . insert ( make_pair ( max ( i , d - i ) , i ) ) ; for ( int i = d + 1 ; i < n ; ++ i ) { while ( ! q . empty ( ) && deg [ q . begin ( ) -> second ] == k ) q . erase ( q . begin ( ) ) ; if ( q . empty ( ) || q . begin ( ) -> first == d ) { cout << " No " ; return ; } ++ deg [ i ] ; ++ deg [ q . begin ( ) -> second ] ; ans . push_back ( make_pair ( i , q . begin ( ) -> second ) ) ; q . insert ( make_pair ( q . begin ( ) -> first + 1 , i ) ) ; } for ( int i = 0 ; i < n - 1 ; ++ i ) cout << ans [ i ] . first + 1 << " ▁ " << ans [ i ] . second + 1 << endl ; } int main ( ) { int n = 6 , d = 3 , k = 4 ; Make_tree ( n , d , k ) ; return 0 ; }
Sum of all Submatrices of a Given Matrix | C ++ program to find the sum of all possible submatrices of a given Matrix ; Function to find the sum of all possible submatrices of a given Matrix ; Variable to store the required sum ; Nested loop to find the number of submatrices , each number belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code
#include <iostream> NEW_LINE #define n  3 NEW_LINE using namespace std ; int matrixSum ( int arr [ ] [ n ] ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { int top_left = ( i + 1 ) * ( j + 1 ) ; int bottom_right = ( n - i ) * ( n - j ) ; sum += ( top_left * bottom_right * arr [ i ] [ j ] ) ; } return sum ; } int main ( ) { int arr [ ] [ n ] = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; cout << matrixSum ( arr ) ; return 0 ; }
Maximum Bitwise AND pair from given range | C ++ implementation of the approach ; Function to return the maximum bitwise AND possible among all the possible pairs ; Maximum among all ( i , j ) pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxAND ( int L , int R ) { int maximum = L & R ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = max ( maximum , ( i & j ) ) ; return maximum ; } int main ( ) { int L = 1 , R = 632 ; cout << maxAND ( L , R ) ; return 0 ; }
Split the array into odd number of segments of odd lengths | CPP to check whether given array is breakable or not ; Function to check ; Check the result by processing the first & last element and size ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkArray ( int arr [ ] , int n ) { return ( arr [ 0 ] % 2 ) && ( arr [ n - 1 ] % 2 ) && ( n % 2 ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( int ) checkArray ( arr , n ) ; return 0 ; }
Minimum removals to make array sum odd | C ++ implementation of the above approach ; Function to find minimum removals ; Count odd numbers ; If the counter is odd return 0 otherwise return 1 ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int findCount ( int arr [ ] , int n ) { int countOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 1 ) countOdd ++ ; if ( countOdd % 2 == 0 ) return 1 ; else return 0 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 5 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findCount ( arr , n ) ; return 0 ; }
Check whether the number can be made perfect square after adding 1 | C ++ implementation of the approach ; Function that returns true if x is a perfect square ; Find floating point value of square root of x ; If square root is an integer ; Function that returns true if n is a sunny number ; If ( n + 1 ) is a perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ; } bool isSunnyNum ( int n ) { if ( isPerfectSquare ( n + 1 ) ) return true ; return false ; } int main ( ) { int n = 3 ; if ( isSunnyNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimum operations to make counts of remainders same in an array | C ++ implementation of the approach ; Function to return the minimum number of operations required ; To store modulos values ; If it 's size greater than k  it needed to be decreased ; If it 's size is less than k  it needed to be increased ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int n , int a [ ] , int m ) { int k = n / m ; vector < vector < int > > val ( m ) ; for ( int i = 0 ; i < n ; ++ i ) { val [ a [ i ] % m ] . push_back ( i ) ; } long long ans = 0 ; vector < pair < int , int > > extra ; for ( int i = 0 ; i < 2 * m ; ++ i ) { int cur = i % m ; while ( int ( val [ cur ] . size ( ) ) > k ) { int elem = val [ cur ] . back ( ) ; val [ cur ] . pop_back ( ) ; extra . push_back ( make_pair ( elem , i ) ) ; } while ( int ( val [ cur ] . size ( ) ) < k && ! extra . empty ( ) ) { int elem = extra . back ( ) . first ; int mmod = extra . back ( ) . second ; extra . pop_back ( ) ; val [ cur ] . push_back ( elem ) ; ans += i - mmod ; } } return ans ; } int main ( ) { int m = 3 ; int a [ ] = { 3 , 2 , 0 , 6 , 10 , 12 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << minOperations ( n , a , m ) ; return 0 ; }
Count primes that can be expressed as sum of two consecutive primes and 1 | C ++ implementation of the approach ; To check if a number is prime or not ; To store possible numbers ; Function to return all prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count all possible prime numbers that can be expressed as the sum of two consecutive primes and one ; All possible prime numbers below N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE bool isprime [ N ] ; bool can [ N ] ; vector < int > SieveOfEratosthenes ( ) { memset ( isprime , true , sizeof ( isprime ) ) ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { for ( int i = p * p ; i < N ; i += p ) isprime [ i ] = false ; } } vector < int > primes ; for ( int i = 2 ; i < N ; i ++ ) if ( isprime [ i ] ) primes . push_back ( i ) ; return primes ; } int Prime_Numbers ( int n ) { vector < int > primes = SieveOfEratosthenes ( ) ; for ( int i = 0 ; i < ( int ) ( primes . size ( ) ) - 1 ; i ++ ) if ( primes [ i ] + primes [ i + 1 ] + 1 < N ) can [ primes [ i ] + primes [ i + 1 ] + 1 ] = true ; int ans = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( can [ i ] and isprime [ i ] ) { ans ++ ; } } return ans ; } int main ( ) { int n = 50 ; cout << Prime_Numbers ( n ) ; return 0 ; }
Sum of bitwise AND of all subarrays | CPP program to find sum of bitwise AND of all subarrays ; Function to find the sum of bitwise AND of all subarrays ; variable to store the final sum ; multiplier ; variable to check if counting is on ; variable to store the length of the subarrays ; loop to find the contiguous segments ; updating the multiplier ; returning the sum ; Driver Code
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; int findAndSum ( int arr [ ] , int n ) { int sum = 0 ; int mul = 1 ; for ( int i = 0 ; i < 30 ; i ++ ) { bool count_on = 0 ; int l = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) if ( count_on ) l ++ ; else { count_on = 1 ; l ++ ; } else if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = 0 ; l = 0 ; } } if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = 0 ; l = 0 ; } mul *= 2 ; } return sum ; } int main ( ) { int arr [ ] = { 7 , 1 , 1 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findAndSum ( arr , n ) ; return 0 ; }
Source to destination in 2 | C ++ implementation of the approach ; Function that returns true if it is possible to move from source to the destination with the given moves ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int Sx , int Sy , int Dx , int Dy , int x , int y ) { if ( abs ( Sx - Dx ) % x == 0 and abs ( Sy - Dy ) % y == 0 and ( abs ( Sx - Dx ) / x ) % 2 == ( abs ( Sy - Dy ) / y ) % 2 ) return true ; return false ; } int main ( ) { int Sx = 0 , Sy = 0 , Dx = 0 , Dy = 0 ; int x = 3 , y = 4 ; if ( isPossible ( Sx , Sy , Dx , Dy , x , y ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of pairs ( x , y ) in an array such that x < y | C ++ implementation of the approach ; Function to return the number of pairs ( x , y ) such that x < y ; To store the number of valid pairs ; If a valid pair is found ; Return the count of valid pairs ; Driver code
#include <iostream> NEW_LINE using namespace std ; int getPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( a [ i ] < a [ j ] ) count ++ ; } } return count ; } int main ( ) { int a [ ] = { 2 , 4 , 3 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << getPairs ( a , n ) ; return 0 ; }
Composite numbers with digit sum 1 | C ++ implementation of the above approach ; Function that returns true if number n is a composite number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if the eventual digit sum of number nm is 1 ; Loop till the sum is not single digit number ; Intitialize the sum as zero ; Find the sum of digits ; If sum is eventually 1 ; Function to print the required numbers from the given range ; If i is one of the required numbers ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } bool isDigitSumOne ( int nm ) { while ( nm > 9 ) { int sum_digit = 0 ; while ( nm > 0 ) { int digit = nm % 10 ; sum_digit = sum_digit + digit ; nm = nm / 10 ; } nm = sum_digit ; } if ( nm == 1 ) return true ; else return false ; } void printValidNums ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { if ( isComposite ( i ) && isDigitSumOne ( i ) ) cout << i << " ▁ " ; } } int main ( void ) { int l = 10 , r = 100 ; printValidNums ( l , r ) ; return 0 ; }
Determine the count of Leaf nodes in an N | CPP program to find number of leaf nodes ; Function to calculate leaf nodes in n - ary tree ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calcNodes ( int N , int I ) { int result = 0 ; result = I * ( N - 1 ) + 1 ; return result ; } int main ( ) { int N = 5 , I = 2 ; cout << " Leaf ▁ nodes ▁ = ▁ " << calcNodes ( N , I ) ; return 0 ; }
An application on Bertrand 's ballot theorem | C ++ implementation of the approach ; Function to calculate factorial of a number mod 1000000007 ; Factorial of i = factorial of ( i - 1 ) * i ; ; Taking mod along with calculation . ; Function for modular exponentiation ; If p is odd ; If p is even ; Function to return the count of required permutations ; Calculating multiplicative modular inverse for x ! and multiplying with ans ; Calculating multiplicative modular inverse for y ! and multiplying with ans ; Driver code ; Pre - compute factorials
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll mod = 1000000007 ; ll arr [ 1000001 ] = { 0 } ; void cal_factorial ( ) { arr [ 0 ] = 1 ; for ( int i = 1 ; i <= 1000000 ; i ++ ) { arr [ i ] = ( arr [ i - 1 ] * i ) % mod ; } } ll mod_exponent ( ll num , ll p ) { if ( p == 0 ) return 1 ; if ( p & 1 ) { return ( ( num % mod ) * ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ) % mod ; } else if ( ! ( p & 1 ) ) return ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ; } ll getCount ( ll x , ll y ) { ll ans = arr [ x + y - 1 ] ; ans *= mod_exponent ( arr [ x ] , mod - 2 ) ; ans %= mod ; ans *= mod_exponent ( arr [ y ] , mod - 2 ) ; ans %= mod ; ans *= ( x - y ) ; ans %= mod ; return ans ; } int main ( ) { cal_factorial ( ) ; ll x = 3 , y = 1 ; cout << getCount ( x , y ) ; return 0 ; }
Find the values of X and Y in the Given Equations | CPP program to find the values of X and Y using the given equations ; Function to find the values of X and Y ; base condition ; required answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findValues ( int a , int b ) { if ( ( a - b ) % 2 == 1 ) { cout << " - 1" ; return ; } cout << ( a - b ) / 2 << " ▁ " << ( a + b ) / 2 ; } int main ( ) { int a = 12 , b = 8 ; findValues ( a , b ) ; return 0 ; }
Program to implement Inverse Interpolation using Lagrange Formula | C ++ code for solving inverse interpolation ; Consider a structure to keep each pair of x and y together ; Function to calculate the inverse interpolation ; Initialize final x ; Calculate each term of the given formula ; Add term to final result ; Driver Code ; Sample dataset of 4 points Here we find the value of x when y = 4.5 ; Size of dataset ; Sample y value ; Using the Inverse Interpolation function to find the value of x when y = 4.5
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Data { double x , y ; } ; double inv_interpolate ( Data d [ ] , int n , double y ) { double x = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { double xi = d [ i ] . x ; for ( j = 0 ; j < n ; j ++ ) { if ( j != i ) { xi = xi * ( y - d [ j ] . y ) / ( d [ i ] . y - d [ j ] . y ) ; } } x += xi ; } return x ; } int main ( ) { Data d [ ] = { { 1.27 , 2.3 } , { 2.25 , 2.95 } , { 2.5 , 3.5 } , { 3.6 , 5.1 } } ; int n = 4 ; double y = 4.5 ; cout << " Value ▁ of ▁ x ▁ at ▁ y ▁ = ▁ 4.5 ▁ : ▁ " << inv_interpolate ( d , n , y ) ; return 0 ; }
Count triplet pairs ( A , B , C ) of points in 2 | C ++ implementation of the approach ; Function to return the count of possible triplets ; Insert all the points in a set ; If the mid point exists in the set ; Return the count of valid triplets ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriplets ( int n , vector < pair < int , int > > points ) { set < pair < int , int > > pts ; int ct = 0 ; for ( int i = 0 ; i < n ; i ++ ) pts . insert ( points [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int x = points [ i ] . first + points [ j ] . first ; int y = points [ i ] . second + points [ j ] . second ; if ( x % 2 == 0 && y % 2 == 0 ) if ( pts . find ( make_pair ( x / 2 , y / 2 ) ) != pts . end ( ) ) ct ++ ; } return ct ; } int main ( ) { vector < pair < int , int > > points = { { 1 , 1 } , { 2 , 2 } , { 3 , 3 } } ; int n = points . size ( ) ; cout << countTriplets ( n , points ) ; }
Concentration of juice after mixing n glasses in equal proportion | C ++ implementation of the approach ; Function to return the concentration of the resultant mixture ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double mixtureConcentration ( int n , int p [ ] ) { double res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res += p [ i ] ; res /= n ; return res ; } int main ( ) { int arr [ ] = { 0 , 20 , 20 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << mixtureConcentration ( n , arr ) ; }
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B | C ++ implementation of the approach ; Function that returns true if the num contains a and b digits only ; Modular Exponentiation ; Function to return the modular inverse of x modulo MOD ; Function to return the required count of numbers ; Generating factorials of all numbers ; Generating inverse of factorials modulo MOD of all numbers ; Keeping a as largest number ; Iterate over all possible values of s and if it is a valid S then proceed further ; Check for invalid cases in the equation ; Find answer using combinatorics ; Add this result to final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1e5 + 5 ; const int MOD = 1e9 + 7 ; #define ll  long long NEW_LINE int check ( int num , int a , int b ) { while ( num ) { int rem = num % 10 ; num /= 10 ; if ( rem != a && rem != b ) return 0 ; } return 1 ; } ll power ( ll x , ll y ) { ll ans = 1 ; while ( y ) { if ( y & 1 ) ans = ( ans * x ) % MOD ; y >>= 1 ; x = ( x * x ) % MOD ; } return ans % MOD ; } int modInverse ( int x ) { return power ( x , MOD - 2 ) ; } ll countNumbers ( int n , int a , int b ) { ll fact [ MAX ] , inv [ MAX ] ; ll ans = 0 ; fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX ; i ++ ) { fact [ i ] = ( 1LL * fact [ i - 1 ] * i ) ; fact [ i ] %= MOD ; } inv [ MAX - 1 ] = modInverse ( fact [ MAX - 1 ] ) ; for ( int i = MAX - 2 ; i >= 0 ; i -- ) { inv [ i ] = ( inv [ i + 1 ] * ( i + 1 ) ) ; inv [ i ] %= MOD ; } if ( a < b ) swap ( a , b ) ; for ( int s = n ; s <= 9 * n ; s ++ ) { if ( ! check ( s , a , b ) ) continue ; if ( s < n * b || ( s - n * b ) % ( a - b ) != 0 ) continue ; int numDig = ( s - n * b ) / ( a - b ) ; if ( numDig > n ) continue ; ll curr = fact [ n ] ; curr = ( curr * inv [ numDig ] ) % MOD ; curr = ( curr * inv [ n - numDig ] ) % MOD ; ans = ( ans + curr ) % MOD ; } return ans ; } int main ( ) { int n = 3 , a = 1 , b = 3 ; cout << countNumbers ( n , a , b ) ; return 0 ; }
Number of elements with even factors in the given range | C ++ implementation of the above approach ; Function to count the perfect squares ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOddSquares ( int n , int m ) { return ( int ) pow ( m , 0.5 ) - ( int ) pow ( n - 1 , 0.5 ) ; } int main ( ) { int n = 5 , m = 100 ; cout << " Count ▁ is ▁ " << ( m - n + 1 ) - countOddSquares ( n , m ) ; return 0 ; }
Total position where king can reach on a chessboard in exactly M moves | C ++ implementation of above approach ; Function to return the number of squares that the king can reach in the given number of moves ; Calculate initial and final coordinates ; Since chessboard is of size 8 X8 so if any coordinate is less than 1 or greater than 8 make it 1 or 8. ; Calculate total positions ; Driver code
#include <iostream> NEW_LINE using namespace std ; int Square ( int row , int column , int moves ) { int a = 0 , b = 0 , c = 0 , d = 0 , total = 0 ; a = row - moves ; b = row + moves ; c = column - moves ; d = column + moves ; if ( a < 1 ) a = 1 ; if ( c < 1 ) c = 1 ; if ( b > 8 ) b = 8 ; if ( d > 8 ) d = 8 ; total = ( b - a + 1 ) * ( d - c + 1 ) - 1 ; return total ; } int main ( ) { int R = 4 , C = 5 , M = 2 ; cout << Square ( R , C , M ) ; return 0 ; }