codeparrot/xlcost-text-to-code huggingface dataset:
Repunit numbers | C ++ implementation to check if a number is Repunit Number ; Function to check if a number contains all the digits 0 , 1 , . . , ( b - 1 ) an equal number of times ; to store number of digits of n in base B ; to count frequency of digit 1 ; condition to check three or more 1 's  and number of ones is equal to number  of digits of n in base B ; Driver Code ; taking inputs ; function to check
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; bool isRepunitNum ( int n , int b ) { int length = 0 ; int countOne = 0 ; while ( n != 0 ) { int r = n % b ; length ++ ; if ( r == 1 ) countOne ++ ; n = n / b ; } return countOne >= 3 && countOne == length ; } int main ( ) { int n = 31 ; int base = 2 ; if ( isRepunitNum ( n , base ) ) cout << " Yes " ; else cout << " NO " ; return 0 ; }
Gapful Numbers | C ++ program for the above approach ; Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; return the last digit ; A function to check Gapful numbers ; Return true if n is gapful number ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstDigit ( int n ) { int digits = ( int ) log10 ( n ) ; n = ( int ) ( n / pow ( 10 , digits ) ) ; return n ; } int lastDigit ( int n ) { return ( n % 10 ) ; } bool isGapful ( int n ) { int first_dig = firstDigit ( n ) ; int last_dig = lastDigit ( n ) ; int concatenation = first_dig * 10 + last_dig ; return ( n % concatenation == 0 ) ; } int main ( ) { int n = 108 ; if ( isGapful ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Economical Numbers | C ++ implementation to find Economical Numbers till n ; Array to store all prime less than and equal to MAX . ; Utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to check if a number is a Economical number ; Count digits in original number ; Count all digits in prime factors of n pDigit is going to hold this value . ; Count powers of p in n ; If primes [ i ] is a prime factor , ; Count the power of prime factors ; Add its digits to pDigit . ; Add digits of power of prime factors to pDigit . ; If n != 1 then one prime factor still to be summed up ; ; If digits in prime factors is less than digits in original number then return true . Else return false . ; Driver code ; Finding all prime numbers before limit . These numbers are used to find prime factors .
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10000 ; vector < int > primes ; void sieveSundaram ( ) { bool marked [ MAX / 2 + 1 ] = { 0 } ; for ( int i = 1 ; i <= ( sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . push_back ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . push_back ( 2 * i + 1 ) ; } bool isEconomical ( int n ) { if ( n == 1 ) return false ; int original_no = n ; int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits ++ ; original_no = original_no / 10 ; } int pDigit = 0 , count_exp = 0 , p ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { p = primes [ i ] ; n = n / p ; count_exp ++ ; } while ( p > 0 ) { pDigit ++ ; p = p / 10 ; } while ( count_exp > 1 ) { pDigit ++ ; count_exp = count_exp / 10 ; } } if ( n != 1 ) { while ( n > 0 ) { pDigit ++ ; n = n / 10 ; } } return ( pDigit < sumDigits ) ; } int main ( ) { sieveSundaram ( ) ; for ( int i = 1 ; i < 200 ; i ++ ) if ( isEconomical ( i ) ) cout << i << " ▁ " ; return 0 ; }
Check if all objects of type A and B can be placed on N shelves | C ++ implementation of the above approach ; Function to return if allocation is possible or not ; Stores the shelves needed for items of type - A and type - B ; Find number of shelves needed for items of type - A ; Fill A / K shelves fully by the items of type - A ; Otherwise ; Fill A / L shelves fully and add remaining to an extra shelf ; Find number of shelves needed for items of type - B ; Fill B / L shelves fully by the items of type - B ; Fill B / L shelves fully and add remaining to an an extra shelf ; Total shelves needed ; If required shelves exceed N ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int A , int B , int N , int K , int L ) { int needa , needb ; if ( A % K == 0 ) needa = A / K ; else needa = A / K + 1 ; if ( B % L == 0 ) needb = B / L ; else needb = B / L + 1 ; int total = needa + needb ; if ( total > N ) return false ; else return true ; } int main ( ) { int A = 3 , B = 3 , N = 3 ; int K = 4 , M = 2 ; if ( isPossible ( A , B , N , K , M ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Minimise N such that sum of count of all factors upto N is greater than or equal to X | C ++ program for the above approach ; Array to store smallest prime factors of each no . ; Function to calculate smallest prime factor of N . ; marking spf [ j ] if it is not previously marked ; Array to store the count of factor for N ; Prefix array which contains the count of factors from 1 to N ; Function to count total factors from 1 to N ; Store total factors of i ; Stores total factors from 1 to i ; Function to search lowest X such that the given condition is satisfied ; Find mid ; Search in the right half ; Search in the left half ; Return the position after Binary Search ; Function to find the required sum ; Precompute smallest prime factor of each value ; Calculate count of total factors from 1 to N ; Binary search to find minimum N ; Driver Code ; Given Sum ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000050 NEW_LINE #define lli  long int NEW_LINE lli spf [ MAX + 1 ] ; void calculate_SPF ( ) { for ( lli i = 0 ; i <= MAX ; i ++ ) spf [ i ] = i ; for ( lli i = 4 ; i <= MAX ; i += 2 ) spf [ i ] = 2 ; for ( lli i = 3 ; i * i <= MAX ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j <= MAX ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } lli tfactor [ MAX + 1 ] ; lli pre [ MAX + 1 ] ; void CountTotalfactors ( ) { tfactor [ 1 ] = pre [ 1 ] = 1 ; for ( lli i = 2 ; i <= MAX ; i ++ ) { lli mspf = spf [ i ] ; lli prim = mspf ; lli temp = i ; lli cnt = 0 ; while ( temp % mspf == 0 ) { temp /= mspf ; cnt += 1 ; prim = prim * mspf ; } tfactor [ i ] = ( cnt + 1 ) * tfactor [ temp ] ; pre [ i ] = pre [ i - 1 ] + tfactor [ i ] ; } } lli BinarySearch ( lli X ) { lli start = 1 ; lli end = MAX - 1 ; while ( start < end ) { lli mid = ( start + end ) / 2 ; if ( pre [ mid ] == X ) return mid ; else if ( pre [ mid ] < X ) start = mid + 1 ; else end = mid ; } return start ; } void findSumOfCount ( int X ) { calculate_SPF ( ) ; CountTotalfactors ( ) ; cout << BinarySearch ( X ) << endl ; } int main ( ) { int X = 10 ; findSumOfCount ( X ) ; return 0 ; }
Check if the Matrix follows the given constraints or not | C ++ implementation of the above approach ; Stores true at prime indices ; Function to generate the prime numbers using Sieve of Eratosthenes ; If p is still true ; Mark all multiples of p ; Function returns sum of all elements of matrix ; Function to check if for all prime ( i + j ) , a [ i ] [ j ] is prime ; If index is prime ; Driver Code ; Check for both conditions
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < bool > prime ; void buildSieve ( int sum ) { prime = vector < bool > ( sum + 1 , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < ( sum + 1 ) ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < ( sum + 1 ) ; i += p ) prime [ i ] = false ; } } } int getSum ( int a [ 4 ] [ 5 ] ) { int s = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) s += a [ i ] [ j ] ; return s ; } bool checkIndex ( int n , int m , int a [ 4 ] [ 5 ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) { if ( prime [ i + j ] && ! prime [ a [ i ] [ j ] ] ) { return false ; } } return true ; } int main ( ) { int n = 4 , m = 5 ; int a [ 4 ] [ 5 ] = { { 1 , 2 , 3 , 2 , 2 } , { 2 , 2 , 7 , 7 , 7 } , { 7 , 7 , 21 , 7 , 10 } , { 2 , 2 , 3 , 6 , 7 } } ; int sum = getSum ( a ) ; buildSieve ( sum ) ; if ( prime [ sum ] && checkIndex ( n , m , a ) ) { cout << " YES " << endl ; } else cout << " NO " << endl ; return 0 ; }
How to calculate the Easter date for a given year using Gauss ' Algorithm | C ++ program for the above approach ; Function calculates and prints easter date for given year Y ; All calculations done on the basis of Gauss Easter Algorithm ; A corner case , when D is 29 ; Another corner case , when D is 28 ; If days > 31 , move to April April = 4 th Month ; Otherwise , stay on March March = 3 rd Month ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void gaussEaster ( int Y ) { float A , B , C , P , Q , M , N , D , E ; A = Y % 19 ; B = Y % 4 ; C = Y % 7 ; P = ( float ) floor ( Y / 100 ) ; Q = ( float ) floor ( ( 13 + 8 * P ) / 25 ) ; M = ( int ) ( 15 - Q + P - P / 4 ) % 30 ; N = ( int ) ( 4 + P - P / 4 ) % 7 ; D = ( int ) ( 19 * A + M ) % 30 ; E = ( int ) ( 2 * B + 4 * C + 6 * D + N ) % 7 ; int days = ( int ) ( 22 + D + E ) ; if ( ( D == 29 ) && ( E == 6 ) ) { cout << Y << " - 04-19" ; return ; } else if ( ( D == 28 ) && ( E == 6 ) ) { cout << Y << " - 04-18" ; return ; } else { if ( days > 31 ) { cout << Y << " - 04 - " << ( days - 31 ) ; return ; } else { cout << Y << " - 03 - " << days ; return ; } } } int main ( ) { int Y = 2020 ; gaussEaster ( Y ) ; return 0 ; }
Min steps to convert N | C ++ program for the above problem ; adjacency list for numbers till 100001 ; visited array ; to store distance of every vertex from A ; function to check if number is a prime ; function to check if numbers differ by only a single - digit ; check the last digit of both numbers and increase count if different ; generate all N digit primes ; for every prime number i check if an edge can be made . ; for every prime number i check if an edge can be made from i to j . ; if edge is possible then insert in the adjacency list ; function to count distance ; if unvisited push onto queue and mark visited as 1 and add the distance of curr + 1. ; Driver code ; Call bfs traversal with root as node A ; Indicates not possible
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE #define mod  1000000007 NEW_LINE #define pb  push_back NEW_LINE #define mod  1000000007 NEW_LINE #define vi  vector<int> NEW_LINE vi lis [ 100001 ] ; vi primes ; int vis [ 100001 ] ; int dis [ 100001 ] ; bool isPrime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } bool valid ( int a , int b ) { int c = 0 ; while ( a ) { if ( ( a % 10 ) != ( b % 10 ) ) { c ++ ; } a = a / 10 ; b = b / 10 ; } if ( c == 1 ) { return true ; } else { return false ; } } void makePrimes ( int N ) { int i , j ; int L = pow ( 10 , N - 1 ) ; int R = pow ( 10 , N ) - 1 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime ( i ) ) { primes . pb ( i ) ; } } for ( i = 0 ; i < primes . size ( ) ; i ++ ) { for ( j = i + 1 ; j < primes . size ( ) ; j ++ ) { int a = primes [ i ] ; int b = primes [ j ] ; if ( valid ( a , b ) ) { lis [ a ] . pb ( b ) ; lis [ b ] . pb ( a ) ; } } } } void bfs ( int src ) { queue < int > q ; q . push ( src ) ; vis [ src ] = 1 ; dis [ src ] = 0 ; while ( ! q . empty ( ) ) { int curr = q . front ( ) ; q . pop ( ) ; for ( int x : lis [ curr ] ) { if ( vis [ x ] == 0 ) { vis [ x ] = 1 ; q . push ( x ) ; dis [ x ] = dis [ curr ] + 1 ; } } } } int main ( ) { int N = 4 ; makePrimes ( N ) ; int A = 1033 , B = 8179 ; bfs ( A ) ; if ( dis [ B ] == -1 ) cout << " - 1" << endl ; else cout << dis [ B ] << endl ; return 0 ; }
Place Value of a given digit in a number | C ++ implementation to find place value of a number ; Function to find place value ; Driver Code ; Digit , which we want to find place value . ; Number from where we want to find place value .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int placeValue ( int N , int num ) { int total = 1 , value = 0 , rem = 0 ; while ( true ) { rem = N % 10 ; N = N / 10 ; if ( rem == num ) { value = total * rem ; break ; } total = total * 10 ; } return value ; } int main ( ) { int D = 5 ; int N = 85932 ; cout << ( placeValue ( N , D ) ) ; }
Beatty sequence | C ++ implementation of the above approach ; Function to print the first N terms of the Beatty sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void BeattySequence ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { double ans = floor ( i * sqrt ( 2 ) ) ; cout << ans << " , ▁ " ; } } int main ( ) { int n = 5 ; BeattySequence ( n ) ; return 0 ; }
Primitive Abundant Number | C ++ implementation of the above approach ; Function to sum of divisors ; Note that this loop runs till square root of N ; If divisors are equal , take only one of them ; else Otherwise take both ; Function to check Abundant Number ; Return true if sum of divisors is greater than N . ; Function to check Deficient Number ; Check if sum ( n ) < 2 * n ; Function to check all proper divisors of N is deficient number or not ; if number itself is not abundant return false ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } bool checkAbundant ( int n ) { return ( getSum ( n ) - n > n ) ; } bool isDeficient ( int n ) { return ( getSum ( n ) < ( 2 * n ) ) ; } bool checkPrimitiveAbundant ( int num ) { if ( ! checkAbundant ( num ) ) { return false ; } for ( int i = 2 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 && i != num ) { if ( i * i == num ) { if ( ! isDeficient ( i ) ) { return false ; } } else if ( ! isDeficient ( i ) || ! isDeficient ( num / i ) ) { return false ; } } } return true ; } int main ( ) { int n = 20 ; if ( checkPrimitiveAbundant ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Sum of the first N Pronic Numbers | C ++ implementation to find sum of first N terms ; Function to calculate the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSum ( int N ) { return N * ( N - 1 ) / 2 + N * ( N - 1 ) * ( 2 * N - 1 ) / 6 ; } int main ( ) { int N = 3 ; cout << calculateSum ( N ) ; return 0 ; }
Canada Numbers | C ++ implementation for the above approach ; Function to calculate sum of all trivial divisors of given natural number ; Final result of summation of trivial divisors ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Function to return sum of squares of digits of N ; Function to check if N is a Canada number ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int num ) { int result = 0 ; for ( int i = 1 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return ( result - 1 - num ) ; } int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r * r ; n = n / 10 ; } return sum ; } bool isCanada ( int n ) { return divSum ( n ) == getSum ( n ) ; } int main ( ) { int n = 125 ; if ( isCanada ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Self Numbers | C ++ implementation to check if the number is a self number or not ; Function to find the sum of digits of a number N ; Function to check for Self number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } bool isSelfNum ( int n ) { for ( int m = 1 ; m <= n ; m ++ ) { if ( m + getSum ( m ) == n ) return false ; } return true ; } int main ( ) { int n = 20 ; if ( isSelfNum ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Central binomial coefficient | C ++ implementation to find the Nth Central Binomial Coefficient ; Function to find the value of Nth Central Binomial Coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int main ( ) { int n = 3 ; int k = n ; n = 2 * n ; cout << binomialCoeff ( n , k ) ; }
Multiply perfect number | C ++ implementation of the above approach ; Function to find the sum of divisors ; Note that this loop runs till square root of N ; If divisors are equal , take only one of them ; Otherwise take both ; Function to check Multiply - perfect number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } bool MultiplyPerfectNumber ( int n ) { if ( getSum ( n ) % n == 0 ) return true ; else return false ; } int main ( ) { int n = 28 ; if ( MultiplyPerfectNumber ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Perfect totient number | C ++ implementation to find the number of digits in a Nth fibonacci number ; Function to find the Totient number of the given value ; Initialize result as n ; Consider all prime factors of n and subtract their multiples from result ; Check if p is a prime factor . ; If yes , then update N and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Function to check if the number is a perfect totient number ; store original value of n ; loop to calculate sum of iterated totients ; condition for Perfect Totient Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int phi ( int n ) { int result = n ; for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) n /= p ; result -= result / p ; } } if ( n > 1 ) result -= result / n ; return result ; } int isPerfectTotientNum ( int n ) { int temp = n ; int sum = 0 ; while ( n > 1 ) { sum = sum + phi ( n ) ; n = phi ( n ) ; } if ( sum == temp ) return true ; return false ; } int main ( ) { int n = 9 ; if ( isPerfectTotientNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Cunningham Numbers | C ++ implementation for the above approach ; Function to check if a number can be expressed as a ^ b . ; Function to check if N is a Cunningham number ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = log ( a ) / log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } bool isCunningham ( int n ) { return isPower ( n - 1 ) || isPower ( n + 1 ) ; } int main ( ) { int n = 126 ; if ( isCunningham ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Product of all Subarrays of an Array | Set 2 | C ++ program for the above approach ; Function to find the product of elements of all subarray ; Initialize the result ; Computing the product of subarray using formula ; Return the product of all elements of each subarray ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long int SubArrayProdct ( int arr [ ] , int n ) { long int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result *= pow ( arr [ i ] , ( i + 1 ) * ( n - i ) ) ; return result ; } int main ( ) { int arr [ ] = { 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << SubArrayProdct ( arr , N ) << endl ; return 0 ; }
Number of ways to reach ( M , N ) in a matrix starting from the origin without visiting ( X , Y ) | C ++ program from the above approach ; Function for computing nCr ; Function to find factorial of a number ; Function for counting the number of ways to reach ( m , n ) without visiting ( x , y ) ; Driver Code ; Given Dimensions of Matrix ; Cell not to be visited ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) ; int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int countWays ( int m , int n , int x , int y ) { return nCr ( m + n , m ) - nCr ( x + y , x ) * nCr ( m + n - x - y , m - x ) ; } int main ( ) { int m = 5 ; int n = 4 ; int x = 3 ; int y = 2 ; cout << countWays ( m , n , x , y ) ; return 0 ; }
Smallest number greater than or equal to N using only digits 1 to K | C ++ Program to find the smallest number greater than or equal to N which is made up of first K digits ; Function to count the digits greater than K ; Function to print the list ; Function to find the number greater than or equal to n , which is only made of first k digits ; If the number itself satisfy the conditions ; Check digit from back ; If digit > K is present previously and current digit is less than K ; If current digit is greater than K ; If an extra digit needs to be added ; Print the number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountGreater ( int n , int k ) { int a = 0 ; while ( n ) { if ( ( n % 10 ) > k ) { a ++ ; } n = n / 10 ; } return a ; } int PrintList ( list < int > ans ) { for ( auto it = ans . begin ( ) ; it != ans . end ( ) ; it ++ ) cout << * it ; } void getNumber ( int n , int k ) { int count = CountGreater ( n , k ) ; if ( count == 0 ) { cout << n ; return ; } list < int > ans ; bool changed = false ; while ( n > 0 ) { int digit = n % 10 ; if ( changed == true ) { ans . push_front ( digit ) ; } else { if ( count == 0 && digit < k ) { ans . push_front ( digit + 1 ) ; changed = true ; } else { ans . push_front ( 1 ) ; if ( digit > k ) { count -- ; } } } n = n / 10 ; } if ( changed == false ) { ans . push_front ( 1 ) ; } PrintList ( ans ) ; return ; } int main ( ) { int N = 51234 ; int K = 4 ; getNumber ( N , K ) ; return 0 ; }
Find the Batting Average of a batsman | C ++ program to calculate the average of a batsman ; Function to find the average of a batsman ; Calculate number of dismissals ; check for 0 times out ; Calculate batting average ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; double averageRuns ( int runs , int matches , int notout ) { int out = matches - notout ; if ( out == 0 ) return -1 ; double avg = double ( runs ) / out ; return avg ; } int main ( ) { int runs = 10000 ; int matches = 250 ; int notout = 50 ; double avg = averageRuns ( runs , matches , notout ) ; if ( avg == -1 ) cout << " NA " ; else cout << avg ; return 0 ; }