codeparrot/xlcost-text-to-code huggingface dataset:
Smallest and Largest N | C ++ program for the above approach ; Function to find n digit largest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit largest number ; Find the number of digits in number n ; Append 9 ; To make it largest n digit number starting and ending with n , we just need to append n at start and end ; Return the largest number ; Function to find n digit smallest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit smallest number ; Find the number of digits in number n ; To make it smallest n digit number starting and ending with n , we just need to append n at start and end ; Return the smallest number ; Driver code ; Given number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findNumberL ( int n ) { if ( n == 1 ) return "1" ; string result = " " ; int length = ( int ) floor ( log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '9' ; } result = to_string ( n ) + result + to_string ( n ) ; return result ; } string findNumberS ( int n ) { if ( n == 1 ) return "1" ; string result = " " ; int length = ( int ) floor ( log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '0' ; } result = to_string ( n ) + result + to_string ( n ) ; return result ; } int main ( ) { int N = 3 ; cout << " Smallest ▁ Number ▁ = ▁ " << findNumberS ( N ) << endl ; cout << " Largest ▁ Number ▁ = ▁ " << findNumberL ( N ) ; return 0 ; }
Maximum number of 0 s that can be flipped such that Array has no adjacent 1 s | C ++ program for the above approach ; Maximum number of 0 s that can be replaced by 1 ; Check for three consecutive 0 s ; Flip the bit ; Increase the count ; Driver 's Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int canReplace ( int array [ ] , int n ) { int i = 0 , count = 0 ; while ( i < n ) { if ( array [ i ] == 0 && ( i == 0 array [ i - 1 ] == 0 ) && ( i == n - 1 array [ i + 1 ] == 0 ) ) { array [ i ] = 1 ; count ++ ; } i ++ ; } return count ; } int main ( ) { int array [ 5 ] = { 1 , 0 , 0 , 0 , 1 } ; cout << canReplace ( array , 5 ) ; }
Sum of first N Star Numbers | C ++ program to find the sum of the first N star numbers ; Function to find the sum of the first N star number ; Variable to store the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_star_num ( int n ) { int summ = 2 * n * ( n + 1 ) * ( n - 1 ) + n ; return summ ; } int main ( ) { int n = 3 ; cout << sum_star_num ( n ) ; return 0 ; }
Count distinct median possible for an Array using given ranges of elements | C ++ implementation to Count the number of distinct medians of an array where each array elements are given by a range ; Function to Count the number of distinct medians of an array where each array elements are given by a range ; Loop to store the starting and end range in the array ; Condition to check if the length of the array is odd ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int  long long int NEW_LINE void solve ( int n , const vector < pair < int , int > > & vec ) { vector < int > a , b ; for ( auto pr : vec ) { a . push_back ( pr . first ) ; b . push_back ( pr . second ) ; } sort ( a . begin ( ) , a . end ( ) ) ; sort ( b . begin ( ) , b . end ( ) ) ; int left , right , ans ; if ( ( n & 1 ) ) { left = a [ n / 2 ] ; right = b [ n / 2 ] ; ans = right - left + 1 ; } else { left = ( a [ n / 2 ] + a [ n / 2 - 1 ] ) ; right = ( b [ n / 2 ] + b [ n / 2 - 1 ] ) ; ans = right - left + 1 ; } cout << ans << endl ; } signed main ( ) { int N = 3 ; vector < pair < int , int > > vec = { { 100 , 100 } , { 10 , 10000 } , { 1 , 1000000000 } } ; solve ( N , vec ) ; return 0 ; }
Count of pairs from Array with sum equal to twice their bitwise AND | C ++ implementation to find the pairs with equal sum and twice the bitwise AND of the pairs ; Map to store the occurrence of elements of array ; Function to find the pairs with equal sum and twice the bitwise AND of the pairs ; Loop to find the frequency of elements of array ; Function to find the count such pairs in the array ; if an element occurs more than once then the answer will by incremented by nC2 times ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < int , int > mp ; int find_pairs ( int ar [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mp [ ar [ i ] ] ++ ; } for ( auto i : mp ) { int count = i . second ; if ( count > 1 ) { ans += ( ( count * ( count - 1 ) ) / 2 ) ; } } return ans ; } int main ( ) { int ar [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 5 , 7 , 8 } ; int arr_size = ( sizeof ( ar ) / sizeof ( ar [ 0 ] ) ) ; cout << find_pairs ( ar , arr_size ) ; return 0 ; }
360 | C ++ implementation for above approach ; Function to find the nth 360 - gon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gonNum360 ( int n ) { return ( 358 * n * n - 356 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << gonNum360 ( n ) ; return 0 ; }
120 | C ++ implementation for above approach ; Function to find the nth 120 - gon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gonNum120 ( int n ) { return ( 118 * n * n - 116 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << gonNum120 ( n ) ; return 0 ; }
Tetracontaoctagonal Number | C ++ implementation for above approach ; Function to find the nth Tetracontaoctagonal Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int TetracontaoctagonalNum ( int n ) { return ( 46 * n * n - 44 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << TetracontaoctagonalNum ( n ) ; return 0 ; }
257 | C ++ implementation for above approach ; Function to find the nth 257 - gon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gonNum257 ( int n ) { return ( 255 * n * n - 253 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << gonNum257 ( n ) ; return 0 ; }
Tetracontadigonal Number | C ++ implementation for the above approach ; Function to find the nth Tetracontadigonal Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int TetracontadigonalNum ( int n ) { return ( 40 * n * n - 38 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << TetracontadigonalNum ( n ) ; return 0 ; }
Index of smallest triangular number with N digits | C ++ implementation of the above approach ; Function to return index of smallest triangular no n digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findIndex ( int n ) { float x = sqrt ( 2 * pow ( 10 , ( n - 1 ) ) ) ; return round ( x ) ; } int main ( ) { int n = 3 ; cout << findIndex ( n ) ; return 0 ; }
Program to find the LCM of two prime numbers | C ++ Program to find LCM of two prime numbers ; Function to return the LCM of two prime numbers ; If the two numbers are equal then return any one of a and b ; Else return product of numbers ; Driver code ; Given two numbers ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLCMPrime ( int a , int b ) { if ( a == b ) { return a ; } return a * b ; } int main ( ) { int a = 3 , b = 5 ; cout << findLCMPrime ( a , b ) ; return 0 ; }
Smallest multiple of N with exactly N digits in its Binary number representation | C ++ program to find smallest multiple of n with exactly N digits in Binary number System . ; Function to find smallest multiple of n with exactly n digits in Binary number representation . ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void smallestNumber ( int N ) { cout << N * ceil ( pow ( 2 , ( N - 1 ) ) / N ) ; } int main ( ) { int N = 3 ; smallestNumber ( N ) ; return 0 ; }
Find the largest N digit multiple of N | C ++ program to find largest multiple of N containing N digits ; Function to find the largest N digit multiple of N ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void smallestNumber ( int N ) { cout << N * floor ( ( pow ( 10 , N ) - 1 ) / N ) ; } int main ( ) { int N = 2 ; smallestNumber ( N ) ; return 0 ; }
Icosikaiheptagonal Number | C ++ program to find N - th icosikaiheptagonal number ; Function to find the nth icosikaiheptagonal Number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int icosikaiheptagonalNum ( int n ) { return ( 25 * n * n - 23 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ icosikaiheptagonal ▁ Number ▁ is ▁ " << icosikaiheptagonalNum ( n ) ; return 0 ; }
Program to check if N is a triacontagonal number | C ++ program to check whether a number is an triacontagonal number or not ; Function to check whether a number is an triacontagonal number or not ; Condition to check whether a number is an triacontagonal number or not ; Driver Code ; Given number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool istriacontagonal ( int N ) { float n = ( 26 + sqrt ( 224 * N + 676 ) ) / 56 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 30 ; if ( istriacontagonal ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Check whether one root of the Quadratic Equation is twice of other or not | C ++ program to check if one root of a Quadratic Equation is twice of other or not ; Function to find the required answer ; Driver code
#include <iostream> NEW_LINE using namespace std ; void checkSolution ( int a , int b , int c ) { if ( 2 * b * b == 9 * a * c ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int a = 1 , b = 3 , c = 2 ; checkSolution ( a , b , c ) ; return 0 ; }
Parity of the given mathematical expression using given N numbers | C ++ program to determine the parity of the given mathematical expression ; Iterating through the given integers ; If any odd number is present , then S is even parity ; Else , S is odd parity ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getParity ( int n , const vector < int > & A ) { for ( auto x : A ) { if ( x & 1 ) { cout << " Even " << endl ; return ; } } cout << " Odd " << endl ; } int main ( ) { int N = 3 ; vector < int > A = { 2 , 3 , 1 } ; getParity ( N , A ) ; return 0 ; }
Count of digits after concatenation of first N positive integers | C ++ program to find the number of digits after concatenating the first N positive integers ; Function to find the number of digits after concatenating the first N positive integers ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void numberOfDigits ( int N ) { int nod = floor ( log10 ( N ) + 1 ) ; int toDecrease = ( pow ( 10 , nod ) - 1 ) / 9 ; cout << ( N + 1 ) * nod - toDecrease << endl ; } int main ( ) { int N = 13 ; numberOfDigits ( N ) ; return 0 ; }
Length of maximum product subarray | C ++ program to find maximum length subarray having non zero product ; Function that returns the maximum length subarray having non zero product ; zeroindex list to store indexex of zero ; If zeroindex list is empty then Maxlength is as size of array ; If zeroindex list is not empty ; first zero is on index 2 that means two numbers positive , before index 2 so as their product is positive to ; Checking for other indexex ; If the difference is greater than maxlen then maxlen is updated ; To check the length of remaining array after last zeroindex ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Maxlength ( int arr [ ] , int N ) { vector < int > zeroindex ; int maxlen ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zeroindex . push_back ( i ) ; } if ( zeroindex . size ( ) == 0 ) { maxlen = N ; } else { maxlen = zeroindex [ 0 ] ; for ( int i = 0 ; i < zeroindex . size ( ) - 1 ; i ++ ) { if ( zeroindex [ i + 1 ] - zeroindex [ i ] - 1 > maxlen ) { maxlen = zeroindex [ i + 1 ] - zeroindex [ i ] - 1 ; } } if ( N - zeroindex [ zeroindex . size ( ) - 1 ] - 1 > maxlen ) { maxlen = N - zeroindex [ zeroindex . size ( ) - 1 ] - 1 ; } } cout << maxlen << endl ; } int main ( ) { int N = 9 ; int arr [ ] = { 7 , 1 , 0 , 1 , 2 , 0 , 9 , 2 , 1 } ; Maxlength ( arr , N ) ; }
Check if sum of exactly K elements of the Array can be odd or not | C ++ implementation of the above approach ; Function returns true if it is possible to have odd sum ; counting number of odd and even elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int arr [ ] , int N , int K ) { int oddCount = 0 , evenCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) evenCount ++ ; else oddCount ++ ; } if ( evenCount == N || ( oddCount == N && K % 2 == 0 ) || ( K == N && oddCount % 2 == 0 ) ) return false ; else return true ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 8 } ; int K = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPossible ( arr , N , K ) ) cout << " Possible " ; else cout << " Not ▁ Possible " ; return 0 ; }
Find the last two digits of Factorial of a given Number | C ++ implementation to find last two digits factorial of a given number ; Function to print the last two digits of N ! ; For N >= 10 , N ! % 100 will always be 0 ; Calculating N ! % 100 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lastTwoDigits ( long long N ) { if ( N >= 10 ) { cout << "00" ; return ; } long long fac = 1 ; for ( int i = 1 ; i <= N ; i ++ ) fac = ( fac * i ) % 100 ; cout << fac ; } int main ( ) { int N = 7 ; lastTwoDigits ( N ) ; }
Product of N terms of a given Geometric series | C ++ program for the above approach ; Function to calculate product of geometric series ; Return the final product with the above formula ; Driver Code ; Given first term and common ratio ; Number of terms ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; float productOfGP ( float a , float r , int n ) { return pow ( a , n ) * pow ( r , n * ( n - 1 ) / 2 ) ; } int main ( ) { float a = 1 , r = 2 ; int N = 4 ; cout << productOfGP ( a , r , N ) ; }
Product of N terms of a given Geometric series | C ++ program for the above approach ; Function to calculate product of N terms of geometric series ; Find the product of first and the last term ; Return the sqrt of the above expression to find the product ; Driver Code ; Given first term and common ratio ; Number of terms ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; float productOfGP ( float a , float r , int n ) { int an = a * pow ( r , n - 1 ) ; return sqrt ( pow ( a * an , n ) ) ; } int main ( ) { float a = 1 , r = 2 ; int N = 4 ; cout << productOfGP ( a , r , N ) ; }
Find two numbers such that difference of their squares equal to N | C ++ Program to find two numbers with difference of their squares equal to N ; Function to check and print the required two positive integers ; Iterate till sqrt ( n ) to find factors of N ; Check if x is one of the factors of N ; Store the factor ; Compute the other factor ; Check if the two factors are of the same parity ; Compute a and b ; If no pair exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int n ) { for ( int x = 1 ; x <= sqrt ( n ) ; x ++ ) { if ( n % x == 0 ) { int small = x ; int big = n / x ; if ( small % 2 == big % 2 ) { int a = ( small + big ) / 2 ; int b = ( big - small ) / 2 ; cout << a << " ▁ " << b << endl ; return ; } } } cout << -1 << endl ; } int main ( ) { int n = 7 ; solve ( n ) ; return 0 ; }
Probability that an arbitrary positive divisor of 10 ^ X is an integral multiple of 10 ^ Y | C ++ program to find the probability of an arbitrary positive divisor of Xth power of 10 to be a multiple of Yth power of 10 ; Function to calculate and print the required probability ; Count of potential divisors of X - th power of 10 which are also multiples of Y - th power of 10 ; Count of divisors of X - th power of 10 ; Calculate GCD ; Print the reduced fraction probability ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int  long long int NEW_LINE void prob ( int x , int y ) { int num = abs ( x - y + 1 ) * abs ( x - y + 1 ) ; int den = ( x + 1 ) * ( x + 1 ) ; int gcd = __gcd ( num , den ) ; cout << num / gcd << " / " << den / gcd << endl ; } signed main ( ) { int X = 2 , Y = 1 ; prob ( X , Y ) ; return 0 ; }
Program to check if N is a Chiliagon Number | C ++ for the above approach ; Function to check that if N is Chiliagon Number or not ; Condition to check if N is a Chiliagon Number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_Chiliagon ( int N ) { float n = ( 996 + sqrt ( 7984 * N + 992016 ) ) / 1996 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 1000 ; if ( is_Chiliagon ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Count of common subarrays in two different permutations of 1 to N | C ++ implementation of above approach ; Initialising Map for Index Mapping ; Mapping elements of A ; Modify elements of B according to Map ; Changing B [ i ] as the index of B [ i ] in A ; Count of common subarrays ; Traversing array B ; While consecutive elements are found , we increment K ; Add number of subarrays with length K to total count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int commonSubarrays ( int * A , int * B , int N ) { int Map [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) Map [ * ( A + i ) ] = i ; for ( int i = 0 ; i < N ; i ++ ) { * ( B + i ) = Map [ * ( B + i ) ] ; } int count = 0 ; int i = 0 , K ; while ( i < N ) { K = 1 ; i += 1 ; while ( i < N && B [ i ] == B [ i - 1 ] + 1 ) { i += 1 ; K += 1 ; } count = count + ( ( K ) * ( K + 1 ) ) / 2 ; } return count ; } int main ( ) { int N = 3 ; int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 2 , 3 , 1 } ; cout << ( commonSubarrays ( A , B , N ) ) << endl ; N = 5 ; int C [ ] = { 1 , 2 , 3 , 4 , 5 } ; int D [ ] = { 2 , 3 , 1 , 4 , 5 } ; cout << ( commonSubarrays ( C , D , N ) ) ; }
Represent N as sum of K even or K odd numbers with repetitions allowed | C ++ implementation to find an array of size K with all the even or odd elements in the array ; Function to find the array with all the even / odd elements ; First let 's check  kth is odd or even ; if last element is also an odd number then we can choose odd elements for our answer ; Add 1 in the array ( k - 1 ) times ; Add last odd element ; If array of even elements would be the answer then k - 1 elements would be 2 ; if last element is also an even number then we can choose even elements for our answer ; Add 2 in the array ( k - 1 ) times ; Add last even element ; Printing the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getArrayOfSizeK ( int n , int k ) { vector < int > ans ; int odd = n - ( ( k - 1 ) * 1 ) ; if ( odd > 0 && odd % 2 != 0 ) { for ( int i = 0 ; i < k - 1 ; i ++ ) { ans . push_back ( 1 ) ; } ans . push_back ( odd ) ; } int even = n - ( ( k - 1 ) * 2 ) ; if ( even > 0 && even % 2 == 0 && ans . size ( ) == 0 ) { for ( int i = 0 ; i < k - 1 ; i ++ ) { ans . push_back ( 2 ) ; } ans . push_back ( even ) ; } if ( ans . size ( ) > 0 ) { for ( int i = 0 ; i < k ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } else { cout << " NO " << endl ; } } int main ( ) { int n = 10 , k = 3 ; getArrayOfSizeK ( n , k ) ; return 0 ; }
Check whether a given number N is a Nude Number or not | C ++ program to check if the number if Nude number or not ; Check if all digits of num divide num ; array to store all digits of the given number ; If any of the condition is true for any digit Then N is not a nude number ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool checkDivisbility ( int num ) { int digit ; int N = num ; while ( num != 0 ) { digit = num % 10 ; num = num / 10 ; if ( digit == 0 N % digit != 0 ) return false ; } return true ; } int main ( ) { int N = 128 ; bool result = checkDivisbility ( N ) ; if ( result ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Hensel 's Lemma | C ++ program to illustrate the Hensel 's Lemma ; Function to find the modular inverse of a modulo m ; Apply the Euclidean algorithm , to find the modular inverse ; Function to find the derivative of f ( x ) and f '(x) = 3 * (x ^ 2) ; Function to find the image of x in f ( x ) = x ^ 3 - k . ; Function to find the next power of the number ; Next power of prime for which solution is to be found ; Using Hensel 's recursion to  find the solution (next_a) for  next power of prime ; If next_a < 0 return equivalent positive remainder modulo p ; Return the next power of a ; Function to find the solution of the required exponent of prime ; The lemma does not work for derivative of f ( x ) at a1 ; Looping from 1 to power of prime whose solution is to be found ; Final answer after evaluating all the exponents up till the required exponent ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } int derivative ( int x ) { return 3 * x * x ; } int Image ( int x , int k ) { return x * x * x - k ; } int next_power ( int a_t , int t , int a1 , int prime , int k ) { int power_p = ( int ) pow ( prime , t + 1 ) ; int next_a = ( a_t - Image ( a_t , k ) * inv ( derivative ( a1 ) , prime ) ) % power_p ; if ( next_a < 0 ) return next_a += power_p ; return next_a ; } int powerOfPrime ( int prime , int power , int k , int a1 ) { if ( derivative ( a1 ) != 0 ) { int a_t = a1 ; for ( int p = 1 ; p < power ; p ++ ) { a_t = next_power ( a_t , p , a1 , prime , k ) ; } return a_t ; } return -1 ; } int main ( ) { int prime = 7 , a1 = 3 ; int power = 2 , k = 3 ; cout << powerOfPrime ( prime , power , k , a1 ) ; return 0 ; }
Count of numbers with all digits same in a given range | C ++ program to count the total numbers in the range L and R which have all the digit same ; Function that count the total numbersProgram between L and R which have all the digit same ; length of R ; tmp has all digits as 1 ; For each multiple of tmp in range 1 to 9 , check if it present in range [ L , R ] ; Increment the required count ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_same_digit ( int L , int R ) { int tmp = 0 , ans = 0 ; int n = log10 ( R ) + 1 ; for ( int i = 0 ; i < n ; i ++ ) { tmp = tmp * 10 + 1 ; for ( int j = 1 ; j <= 9 ; j ++ ) { if ( L <= ( tmp * j ) && ( tmp * j ) <= R ) { ans ++ ; } } } return ans ; } int main ( ) { int L = 12 , R = 68 ; cout << count_same_digit ( L , R ) << endl ; return 0 ; }
Minimum LCM of all pairs in a given array | C ++ program to find the pair having minimum LCM ; function that return pair having minimum LCM ; find max element in the array as the gcd of two elements from the array can 't greater than max element. ; created a 2D array to store minimum two multiple of any particular i . ; we already found two smallest multiple ; iterating over all gcd ; iterating over its multiple ; if we already found the two smallest multiple of i ; choosing smallest two multiple ; calculating lcm ; return final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLCM ( int arr [ ] , int n ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = max ( mx , arr [ i ] ) ; } vector < vector < int > > mul ( mx + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mul [ arr [ i ] ] . size ( ) > 1 ) { continue ; } mul [ arr [ i ] ] . push_back ( arr [ i ] ) ; } for ( int i = 1 ; i <= mx ; i ++ ) { for ( int j = i + i ; j <= mx ; j += i ) { if ( mul [ i ] . size ( ) > 1 ) { break ; } for ( int k : mul [ j ] ) { if ( mul [ i ] . size ( ) > 1 ) break ; mul [ i ] . push_back ( k ) ; } } } int ans = INT_MAX ; for ( int i = 1 ; i <= mx ; i ++ ) { if ( mul [ i ] . size ( ) <= 1 ) continue ; int a = mul [ i ] [ 0 ] , b = mul [ i ] [ 1 ] ; int lcm = ( a * b ) / i ; ans = min ( ans , lcm ) ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 , 6 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minLCM ( arr , n ) << endl ; return 0 ; }