codeparrot/xlcost-text-to-code huggingface dataset:
Check whether Quadrilateral is valid or not if angles are given | C ++ program to check if a given quadrilateral is valid or not ; Function to check if a given quadrilateral is valid or not ; Check condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Valid ( int a , int b , int c , int d ) { if ( a + b + c + d == 360 ) return true ; return false ; } int main ( ) { int a = 80 , b = 70 , c = 100 , d = 110 ; if ( Valid ( a , b , c , d ) ) cout << " Valid ▁ quadrilateral " ; else cout << " Invalid ▁ quadrilateral " ; return 0 ; }
Central angle of a N sided Regular Polygon | C ++ program for the above approach ; Function to calculate central angle of a polygon ; Calculate the angle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double calculate_angle ( double n ) { double total_angle = 360 ; return total_angle / n ; } int main ( ) { double N = 5 ; cout << calculate_angle ( N ) ; return 0 ; }
Program to calculate the area of Kite | C ++ implementation of the approach ; Function to return the area of kite ; use above formula ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float areaOfKite ( int d1 , int d2 ) { float area = ( d1 * d2 ) / 2 ; return area ; } int main ( ) { int d1 = 4 , d2 = 6 ; cout << " Area ▁ of ▁ Kite ▁ = ▁ " << areaOfKite ( d1 , d2 ) ; return 0 ; }
Program to calculate the area of Kite | C ++ implementation of the approach ; Function to return the area of the kite ; convert angle degree to radians ; use above formula ; Driver code
#include <bits/stdc++.h> NEW_LINE #define PI  3.14159 / 180 NEW_LINE using namespace std ; float areaOfKite ( int a , int b , double angle ) { angle = angle * PI ; double area = a * b * sin ( angle ) ; return area ; } int main ( ) { int a = 4 , b = 7 , angle = 78 ; cout << " Area ▁ of ▁ Kite ▁ = ▁ " << areaOfKite ( a , b , angle ) ; return 0 ; }
Program to calculate angle on circumference subtended by the chord when the central angle subtended by the chord is given | C ++ Program to calculate angle on the circumference subtended by the chord when the central angle subtended by the chord is given ; Driver code ; Angle on center
#include <iostream> NEW_LINE using namespace std ; float angleOncirCumference ( float z ) { return ( z / 2 ) ; } int main ( ) { float angle = 65 ; float z = angleOncirCumference ( angle ) ; cout << " The ▁ angle ▁ is ▁ " << ( z ) << " ▁ degrees " ; return 0 ; }
Maximum and Minimum value of a quadratic function | C ++ implementation of the above approach ; Function to print the Maximum and Minimum values of the quadratic function ; Calculate the value of second part ; Print the values ; Open upward parabola function ; Open downward parabola function ; If a = 0 then it is not a quadratic function ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PrintMaxMinValue ( double a , double b , double c ) { double secondPart = c * 1.0 - ( b * b / ( 4.0 * a ) ) ; if ( a > 0 ) { cout << " Maxvalue ▁ = ▁ " << " Infinity STRNEWLINE " ; cout << " Minvalue ▁ = ▁ " << secondPart ; } else if ( a < 0 ) { cout << " Maxvalue ▁ = ▁ " << secondPart << " STRNEWLINE " ; cout << " Minvalue ▁ = ▁ " << " - Infinity " ; } else { cout << " Not ▁ a ▁ quadratic ▁ function STRNEWLINE " ; } } int main ( ) { double a = -1 , b = 3 , c = -2 ; PrintMaxMinValue ( a , b , c ) ; return 0 ; }
Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant | C ++ program to find percentage increase in the cylinder if the height is increased by given percentage but radius remains constant ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void newvol ( double x ) { cout << " percentage ▁ increase ▁ " << " in ▁ the ▁ volume ▁ of ▁ the ▁ cylinder ▁ is ▁ " << x << " % " << endl ; } int main ( ) { double x = 10 ; newvol ( x ) ; return 0 ; }
Find the side of the squares which are inclined diagonally and lined in a row | C ++ program to find side of the squares inclined and touch each other externally at vertices and are lined in a row and distance between the centers of first and last squares is given ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void radius ( double n , double d ) { cout << " The ▁ side ▁ of ▁ each ▁ square ▁ is ▁ " << d / ( ( n - 1 ) * sqrt ( 2 ) ) << endl ; } int main ( ) { double d = 42 , n = 4 ; radius ( n , d ) ; return 0 ; }
Program to calculate area of inner circle which passes through center of outer circle and touches its circumference | C ++ implementation of the above approach ; Function calculate the area of the inner circle ; the radius cannot be negative ; area of the circle ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; double innerCirclearea ( double radius ) { if ( radius < 0 ) { return -1 ; } double r = radius / 2 ; double Area = ( 3.14 * pow ( r , 2 ) ) ; return Area ; } int main ( ) { double radius = 4 ; cout << ( " Area ▁ of ▁ circle ▁ c2 ▁ = ▁ " , innerCirclearea ( radius ) ) ; return 0 ; }
Angle subtended by the chord when the angle subtended by another chord of same length is given | C ++ program to find the angle subtended at the center by the chord when the angle subtended at center by another chord of equal length is given ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void angleequichord ( int z ) { cout << " The ▁ angle ▁ subtended ▁ at ▁ the ▁ center ▁ is ▁ " << z << " ▁ degrees " << endl ; } int main ( ) { int z = 48 ; angleequichord ( z ) ; return 0 ; }
Distance of chord from center when distance between center and another equal length chord is given | C ++ program to find the distance of chord from center when distance between center and another equal length chord is given ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lengequichord ( int z ) { cout << " The ▁ distance ▁ between ▁ the ▁ " << " chord ▁ and ▁ the ▁ center ▁ is ▁ " << z << endl ; } int main ( ) { int z = 48 ; lengequichord ( z ) ; return 0 ; }
Length of the perpendicular bisector of the line joining the centers of two circles | C ++ program to find the Length of the perpendicular bisector of the line joining the centers of two circles in which one lies completely inside touching the bigger circle at one point ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lengperpbisect ( double r1 , double r2 ) { double z = 2 * sqrt ( ( r1 * r1 ) - ( ( r1 - r2 ) * ( r1 - r2 ) / 4 ) ) ; cout << " The ▁ length ▁ of ▁ the ▁ " << " perpendicular ▁ bisector ▁ is ▁ " << z << endl ; } int main ( ) { double r1 = 5 , r2 = 3 ; lengperpbisect ( r1 , r2 ) ; return 0 ; }
Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given | C ++ program to find the length of the chord the circle if length of the another chord which is equally inclined through the diameter is given ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lengchord ( int z ) { cout << " The ▁ length ▁ is ▁ " << z << endl ; } int main ( ) { int z = 48 ; lengchord ( z ) ; return 0 ; }
Distance between centers of two intersecting circles if the radii and common chord length is given | C ++ program to find the distance between centers of two intersecting circles if the radii and common chord length is given ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void distcenter ( int r1 , int r2 , int x ) { int z = sqrt ( ( r1 * r1 ) - ( x / 2 * x / 2 ) ) + sqrt ( ( r2 * r2 ) - ( x / 2 * x / 2 ) ) ; cout << " distance ▁ between ▁ the " << " ▁ centers ▁ is ▁ " << z << endl ; } int main ( ) { int r1 = 24 , r2 = 37 , x = 40 ; distcenter ( r1 , r2 , x ) ; return 0 ; }
Exterior angle of a cyclic quadrilateral when the opposite interior angle is given | C ++ program to find the exterior angle of a cyclic quadrilateral when the opposite interior angle is given ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void angleextcycquad ( int z ) { cout << " The ▁ exterior ▁ angle ▁ of ▁ the " << " ▁ cyclic ▁ quadrilateral ▁ is ▁ " << z << " ▁ degrees " << endl ; } int main ( ) { int z = 48 ; angleextcycquad ( z ) ; return 0 ; }
Angle between a chord and a tangent when angle in the alternate segment is given | C ++ program to find the angle between a chord and a tangent when angle in the alternate segment is given ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void anglechordtang ( int z ) { cout << " The ▁ angle ▁ between ▁ tangent " << " ▁ and ▁ the ▁ chord ▁ is ▁ " << z << " ▁ degrees " << endl ; } int main ( ) { int z = 48 ; anglechordtang ( z ) ; return 0 ; }
Print all Perfect Numbers from an array whose sum of digits is also a Perfect Number | C ++ program for the above approach ; Function to check if a number is perfect number or not ; Stores sum of proper divisors ; If sum of digits is equal to N , then it 's a perfect number ; Otherwise , not a perfect number ; Function to find the sum of digits of a number ; Stores sum of digits ; Return sum of digits ; Function to count perfect numbers from an array whose sum of digits is also perfect ; Traverse the array ; If number is perfect ; Stores sum of digits of the number ; If that is also perfect number ; Print that number ; Driver Code ; Given array ; Size of the array ; Function call to count perfect numbers having sum of digits also perfect
#include <bits/stdc++.h> NEW_LINE using namespace std ; int isPerfect ( int N ) { int sumOfDivisors = 1 ; for ( int i = 2 ; i <= N / 2 ; ++ i ) { if ( N % i == 0 ) { sumOfDivisors += i ; } } if ( sumOfDivisors == N ) { return 1 ; } else return 0 ; } int sumOfDigits ( int N ) { int sum = 0 ; while ( N != 0 ) { sum += ( N % 10 ) ; N = N / 10 ; } return sum ; } void countPerfectNumbers ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; ++ i ) { if ( isPerfect ( arr [ i ] ) ) { int sum = sumOfDigits ( arr [ i ] ) ; if ( isPerfect ( sum ) ) { cout << arr [ i ] << " ▁ " ; } } } } int main ( ) { int arr [ ] = { 3 , 8 , 12 , 28 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPerfectNumbers ( arr , N ) ; return 0 ; }
Maximum Prefix Sum possible by merging two given arrays | C ++ implementation of above approach ; Stores the dp states ; Recursive Function to calculate the maximum prefix sum ontained by merging two arrays ; If subproblem is already computed ; If x >= N or y >= M ; If x < N ; If y < M ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define int  long long NEW_LINE using namespace std ; map < pair < int , int > , int > dp ; int maxPreSum ( vector < int > a , vector < int > b , int x , int y ) { if ( dp . find ( { x , y } ) != dp . end ( ) ) return dp [ { x , y } ] ; if ( x == a . size ( ) && y == b . size ( ) ) return 0 ; int curr = dp [ { x , y } ] ; if ( x == a . size ( ) ) { curr = max ( curr , b [ y ] + maxPreSum ( a , b , x , y + 1 ) ) ; } else if ( y == b . size ( ) ) { curr = max ( curr , a [ x ] + maxPreSum ( a , b , x + 1 , y ) ) ; } else { curr = max ( { curr , a [ x ] + maxPreSum ( a , b , x + 1 , y ) , b [ y ] + maxPreSum ( a , b , x , y + 1 ) } ) ; } return dp [ { x , y } ] = curr ; } signed main ( ) { vector < int > A = { 2 , 1 , 13 , 5 , 14 } ; vector < int > B = { -1 , 4 , -13 } ; cout << maxPreSum ( A , B , 0 , 0 ) << endl ; return 0 ; }
Sum of Bitwise AND of the sum of all leaf and non | C ++ program for given approach ; Structure of a Binary tree node ; Helper function to allocate a new node with the given data and left and right pointers as None ; Function to calculate the sum of bitwise AND of the sum of all leaf nodes and non - leaf nodes for each level ; Initialize a queue and append root to it ; Store the required answer ; Stores the sum of leaf nodes at the current level ; Stores the sum of non - leaf nodes at the current level ; Get the size of the queue ; Iterate for all the nodes in the queue currently ; Dequeue a node from queue ; Check if the node is a leaf node ; If true , update the leaf node sum ; Otherwise , update the non - leaf node sum ; Enqueue left and right children of removed node ; Update the answer ; Return the answer ; Driver Code ; Given Tree ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct TreeNode { int val ; TreeNode * left , * right ; TreeNode ( int x = 0 ) { val = x ; left = NULL ; right = NULL ; } } ; int findSum ( TreeNode * root ) { queue < TreeNode * > que ; que . push ( root ) ; int ans = 0 ; while ( que . size ( ) ) { int leaf = 0 ; int nonleaf = 0 ; int length = que . size ( ) ; while ( length ) { auto temp = que . front ( ) ; que . pop ( ) ; if ( ! temp -> left && ! temp -> right ) leaf += temp -> val ; else nonleaf += temp -> val ; if ( temp -> left ) que . push ( temp -> left ) ; if ( temp -> right ) que . push ( temp -> right ) ; length -= 1 ; } ans += leaf & nonleaf ; } return ans ; } int main ( ) { TreeNode * root = new TreeNode ( 5 ) ; root -> left = new TreeNode ( 3 ) ; root -> right = new TreeNode ( 9 ) ; root -> left -> left = new TreeNode ( 6 ) ; root -> left -> right = new TreeNode ( 4 ) ; root -> left -> left -> right = new TreeNode ( 7 ) ; cout << findSum ( root ) ; }
Minimize replacements to make every element in an array exceed every element in another given array | C ++ program for the above approach ; Function to find the minimize replacements to make every element in the array A [ ] strictly greater than every element in B [ ] or vice - versa ; Store the final result ; Create two arrays and initialize with 0 s ; Traverse the array a [ ] ; Increment prefix_a [ a [ i ] ] by 1 ; Traverse the array b [ ] ; Increment prefix_b [ b [ i ] ] by 1 ; Calculate prefix sum of the array a [ ] ; Calculate prefix sum of the array b [ ] ; Iterate over the range [ 0 , 9 ] ; Make every element in array a [ ] strictly greater than digit i and make every element in the array b [ ] less than digit i ; Make every element in array b [ ] strictly greater than digit i and make every element in array a [ ] less than digit i ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MinTime ( int * a , int * b , int n , int m ) { int ans = INT_MAX ; int prefix_a [ 10 ] = { 0 } ; int prefix_b [ 10 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { prefix_a [ a [ i ] ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { prefix_b [ b [ i ] ] ++ ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_a [ i ] += prefix_a [ i - 1 ] ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_b [ i ] += prefix_b [ i - 1 ] ; } for ( int i = 0 ; i <= 9 ; i ++ ) { ans = min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) ; ans = min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) ; } cout << ans ; } int main ( ) { int A [ ] = { 0 , 0 , 1 , 3 , 3 } ; int B [ ] = { 2 , 0 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( B ) / sizeof ( B [ 0 ] ) ; MinTime ( A , B , N , M ) ; return 0 ; }
Count remaining array elements after reversing binary representation of each array element | C ++ program for the above approach ; Function to reverse the binary representation of a number ; Traverse bits of N from the right ; Bitwise left shift ' rev ' by 1 ; If current bit is '1' ; Bitwise right shift N by 1 ; Required number ; Function to count elements from the original array that are also present in the array formed by reversing the binary representation of each element ; Stores the reversed num ; Iterate from [ 0 , N ] ; Stores the presence of integer ; Stores count of elements present in original array ; Traverse the array ; If current number is present ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findReverse ( int N ) { int rev = 0 ; while ( N > 0 ) { rev <<= 1 ; if ( N & 1 == 1 ) rev ^= 1 ; N >>= 1 ; } return rev ; } void countElements ( int arr [ ] , int N ) { vector < int > ans ; for ( int i = 0 ; i < N ; i ++ ) { ans . push_back ( findReverse ( arr [ i ] ) ) ; } unordered_map < int , int > cnt ; for ( int i = 0 ; i < N ; i ++ ) { cnt [ arr [ i ] ] = 1 ; } int count = 0 ; for ( auto i : ans ) { if ( cnt [ i ] ) count ++ ; } cout << count << endl ; } int main ( ) { int arr [ ] = { 1 , 30 , 3 , 8 , 12 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countElements ( arr , N ) ; return 0 ; }
Fizz Buzz Implementation | Set 2 | C ++ program for the above approach ; Function to generate FizzBuzz sequence ; Stores count of multiples of 3 and 5 respectively ; Iterate from 1 to N ; Increment count3 by 1 ; Increment count5 by 1 ; Initialize a boolean variable to check if none of the condition matches ; Check if the value of count3 is equal to 3 ; Reset count3 to 0 , and set flag as True ; Check if the value of count5 is equal to 5 ; Reset count5 to 0 , and set flag as True ; If none of the condition matches ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void fizzBuzz ( int N ) { int count3 = 0 ; int count5 = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count3 ++ ; count5 ++ ; bool flag = false ; if ( count3 == 3 ) { cout << " Fizz " ; count3 = 0 ; flag = true ; } if ( count5 == 5 ) { cout << " Buzz " ; count5 = 0 ; flag = true ; } if ( ! flag ) { cout << i ; } cout << " ▁ " ; } } int main ( ) { int N = 15 ; fizzBuzz ( N ) ; return 0 ; }
Check if diagonal elements of a Matrix are Prime or not | C ++ program for the above approach ; Stores if a number is prime or not ; Function to generate and store primes using Sieve Of Eratosthenes ; Set all numbers as prime ; If p is a prime ; Set all its multiples as non - prime ; Function to check if all diagonal elements are prime or not ; Stores if all diagonal elements are prime or not ; Precompute primes ; Traverse the range [ 0 , N - 1 ] ; Check if numbers on the cross diagonal and main diagonal are primes or not ; If true , then print " Yes " ; Otherwise , print " No " ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prime [ 1000005 ] ; void SieveOfEratosthenes ( int N ) { memset ( prime , true , sizeof ( prime ) ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= N ; i += p ) prime [ i ] = false ; } } } void checkElementsOnDiagonal ( vector < vector < int > > M , int N ) { int flag = 1 ; SieveOfEratosthenes ( 1000000 ) ; for ( int i = 0 ; i < N ; i ++ ) { flag &= ( prime [ M [ i ] [ i ] ] && prime [ M [ i ] [ N - 1 - i ] ] ) ; } if ( flag ) cout << " Yes " << endl ; else cout << " No " ; } int main ( ) { vector < vector < int > > M = { { 1 , 2 , 3 , 13 } , { 5 , 3 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 7 } } ; int N = M . size ( ) ; checkElementsOnDiagonal ( M , N ) ; return 0 ; }
Generate a circular permutation with number of mismatching bits between pairs of adjacent elements exactly 1 | C ++ program for the above approach ; Function to find the permutation of integers from a given range such that number of mismatching bits between pairs of adjacent elements is 1 ; Initialize an arrayList to store the resultant permutation ; Store the index of rotation ; Iterate over the range [ 0 , N - 1 ] ; Traverse all the array elements up to ( 2 ^ k ) - th index in reverse ; If current element is S ; Check if S is zero ; Rotate the array by index value to the left ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > circularPermutation ( int n , int start ) { vector < int > res = { 0 } ; vector < int > ret ; int index = -1 ; for ( int k = 0 , add = 1 << k ; k < n ; k ++ , add = 1 << k ) { for ( int i = res . size ( ) - 1 ; i >= 0 ; i -- ) { if ( res [ i ] + add == start ) index = res . size ( ) ; res . push_back ( res [ i ] + add ) ; } } if ( start == 0 ) return res ; while ( ret . size ( ) < res . size ( ) ) { ret . push_back ( res [ index ] ) ; index = ( index + 1 ) % res . size ( ) ; } return ret ; } int main ( ) { int N = 2 , S = 3 ; vector < int > print = circularPermutation ( N , S ) ; cout << " [ " ; for ( int i = 0 ; i < print . size ( ) - 1 ; i ++ ) { cout << print [ i ] << " , ▁ " ; } cout << print [ print . size ( ) - 1 ] << " ] " ; return 0 ; }
Count pairs from an array having equal sum and quotient | C ++ program for the above approach ; Function to count all pairs ( i , j ) such that a [ i ] + [ j ] = a [ i ] / a [ j ] ; Stores total count of pairs ; Generate all possible pairs ; If a valid pair is found ; Increment count ; Return the final count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] != 0 && a [ i ] % a [ j ] == 0 ) { if ( ( a [ i ] + a [ j ] ) == ( a [ i ] / a [ j ] ) ) count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { -4 , -3 , 0 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , N ) ; return 0 ; }
Count pairs from an array having equal sum and quotient | C ++ program for the above approach ; Function to find number of pairs with equal sum and quotient from a given array ; Store the count of pairs ; Stores frequencies ; Traverse the array ; If y is neither 1 or 0 ; Evaluate x ; Increment count by frequency of x ; Update map ; Print the final count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int a [ ] , int n ) { int count = 0 ; map < double , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { int y = a [ i ] ; if ( y != 0 && y != 1 ) { double x = ( ( y * 1.0 ) / ( 1 - y ) ) * y ; count += mp [ x ] ; } mp [ y ] ++ ; } return count ; } int main ( ) { int arr [ ] = { -4 , -3 , 0 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , N ) ; return 0 ; }
Queries to count numbers from a range which does not contain digit K in their decimal or octal representation | C ++ program for the above approach ; Function to check if the given digit ' K ' is present in the decimal and octal representations of num or not ; Stores if the digit exists or not ; Iterate till nums is non - zero ; Find the remainder ; If the remainder is K ; Function to count the numbers in the range [ 1 , N ] such that it doesn ' t ▁ contain ▁ the ▁ digit ▁ ' K ' in its decimal and octal representation ; Stores count of numbers in the range [ 0 , i ] that contains the digit ' K ' in its octal or decimal representation ; Traverse the range [ 0 , 1e6 + 5 ] ; Check if i contains the digit ' K ' in its decimal or octal representation ; Update pref [ i ] ; Print the answer of queries ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool contains ( int num , int K , int base ) { bool isThere = 0 ; while ( num ) { int remainder = num % base ; if ( remainder == K ) { isThere = 1 ; } num /= base ; } return isThere ; } void count ( int n , int k , vector < vector < int > > v ) { int pref [ 1000005 ] = { 0 } ; for ( int i = 1 ; i < 1e6 + 5 ; i ++ ) { bool present = contains ( i , k , 10 ) || contains ( i , k , 8 ) ; pref [ i ] += pref [ i - 1 ] + present ; } for ( int i = 0 ; i < n ; ++ i ) { cout << v [ i ] [ 1 ] - v [ i ] [ 0 ] + 1 - ( pref [ v [ i ] [ 1 ] ] - pref [ v [ i ] [ 0 ] - 1 ] ) << ' ▁ ' ; } } int main ( ) { int K = 7 ; vector < vector < int > > Q = { { 2 , 5 } , { 1 , 15 } } ; int N = Q . size ( ) ; count ( N , K , Q ) ; }
Count tiles of dimensions 2 * 1 that can be placed in an M * N rectangular board that satisfies the given conditions | C ++ Program to implement the above approach ; Function to count tiles of dimensions 2 x 1 that can be placed in a grid of dimensions M * N as per given conditions ; Number of tiles required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfTiles ( int N , int M ) { if ( N % 2 == 1 ) { return -1 ; } return ( N * 1LL * M ) / 2 ; } int main ( ) { int N = 2 , M = 4 ; cout << numberOfTiles ( N , M ) ; return 0 ; }