codeparrot/xlcost-text-to-code huggingface dataset:
Highly Totient Number | CPP program to find highly totient numbers ; Function to find euler totient number ; 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 find first n highly totient numbers ; Count of Highly totient numbers and value of count of phi of previous numbers ; Store all the values of phi ( x ) upto 10 ^ 5 with frequencies ; If count is greater than count of previous element ; Display the number ; Store the value of phi ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int phi ( int 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 ; } void Highly_Totient ( int n ) { int count = 0 , p_count = -1 , i = 1 ; map < int , int > mp ; for ( int i = 1 ; i < 100000 ; i ++ ) mp [ phi ( i ) ] ++ ; while ( count < n ) { if ( mp [ i ] > p_count ) { cout << i ; if ( count < n - 1 ) cout << " , ▁ " ; p_count = mp [ i ] ; count ++ ; } i ++ ; } } int main ( ) { int n = 20 ; Highly_Totient ( n ) ; return 0 ; }
Program to find the Speed of train as per speed of sound | C ++ implementation of the approach ; Function to find the Speed of train ; Driver code ; calling Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; float speedOfTrain ( float X , float Y ) { float Speed = 0 ; Speed = 1188 * ( ( X - Y ) / Y ) ; return Speed ; } int main ( ) { float X = 8 , Y = 7.2 ; cout << speedOfTrain ( X , Y ) << " ▁ km / hr " ; return 0 ; }
Maximum value of division of two numbers in an Array | CPP program to maximum value of division of two numbers in an array ; Function to maximum value of division of two numbers in an array ; Traverse through the array ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Division ( int a [ ] , int n ) { int maxi = INT_MIN , mini = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { maxi = max ( a [ i ] , maxi ) ; mini = min ( a [ i ] , mini ) ; } return maxi / mini ; } int main ( ) { int a [ ] = { 3 , 7 , 9 , 3 , 11 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << Division ( a , n ) ; return 0 ; }
Ramanujan Prime | CPP program to find Ramanujan numbers ; FUnction to return a vector of primes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 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 . ; Print all prime numbers ; Function to find number of primes less than or equal to x ; Binary search to find out number of primes less than or equal to x ; Function to find the nth ramanujan prime ; For n >= 1 , a ( n ) < 4 * n * log ( 4 n ) ; We start from upperbound and find where pi ( i ) - pi ( i / 2 ) < n the previous number being the nth ramanujan prime ; Function to find first n Ramanujan numbers ; Get the prime numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000000 NEW_LINE vector < int > addPrimes ( ) { int n = MAX ; bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } vector < int > ans ; for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) ans . push_back ( p ) ; return ans ; } int pi ( int x , vector < int > v ) { int l = 0 , r = v . size ( ) - 1 , m , in = -1 ; while ( l <= r ) { m = ( l + r ) / 2 ; if ( v [ m ] <= x ) { in = m ; l = m + 1 ; } else { r = m - 1 ; } } return in + 1 ; } int Ramanujan ( int n , vector < int > v ) { int upperbound = 4 * n * ( log ( 4 * n ) / log ( 2 ) ) ; for ( int i = upperbound ; ; i -- ) { if ( pi ( i , v ) - pi ( i / 2 , v ) < n ) return 1 + i ; } } void Ramanujan_Numbers ( int n ) { int c = 1 ; vector < int > v = addPrimes ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { cout << Ramanujan ( i , v ) ; if ( i != n ) cout << " , ▁ " ; } } int main ( ) { int n = 10 ; Ramanujan_Numbers ( n ) ; return 0 ; }
Quadruplet pair with XOR zero in the given Array | C ++ implementation of the approach ; Function that returns true if the array contains a valid quadruplet pair ; We can always find a valid quadruplet pair for array size greater than MAX ; For smaller size arrays , perform brute force ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 130 ; bool validQuadruple ( int arr [ ] , int n ) { if ( n >= MAX ) return true ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) for ( int l = k + 1 ; l < n ; l ++ ) { if ( ( arr [ i ] ^ arr [ j ] ^ arr [ k ] ^ arr [ l ] ) == 0 ) { return true ; } } return false ; } int main ( ) { int arr [ ] = { 1 , 0 , 2 , 3 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( validQuadruple ( arr , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants | CPP program to find the number of words of X vowels and Y consonants can be formed from M vowels and N consonants ; Function to returns factorial of n ; Function to find nCr ; Function to find the number of words of X vowels and Y consonants can be formed from M vowels and N consonants ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } int NumberOfWays ( int X , int Y , int M , int N ) { return fact ( X + Y ) * nCr ( M , X ) * nCr ( N , Y ) ; } int main ( ) { int X = 2 , Y = 2 , M = 3 , N = 3 ; cout << NumberOfWays ( X , Y , M , N ) ; return 0 ; }
Program for sum of cosh ( x ) series upto Nth term | C ++ program for the sum of cosh ( x ) series ; function to return the factorial of a number ; function to return the sum of the series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int i = 1 , fac = 1 ; for ( i = 1 ; i <= n ; i ++ ) fac = fac * i ; return fac ; } double log_Expansion ( double x , int n ) { double sum = 0 ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + pow ( x , 2 * i ) / fact ( 2 * i ) ; } return sum ; } int main ( ) { double x = 1 ; int n = 10 ; cout << setprecision ( 12 ) << log_Expansion ( x , n ) << endl ; return 0 ; }
Find prime numbers in the first half and second half of an array | C ++ program to print the prime numbers in the first half and second half of an array ; Function to check if a number is prime or not ; Function to find whether elements are prime or not ; Traverse in the given range ; Check if a number is prime or not ; Function to print the prime numbers in the first half and second half of an array ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } void prime_range ( int start , int end , int * a ) { for ( int i = start ; i < end ; i ++ ) { if ( prime ( a [ i ] ) ) cout << a [ i ] << " ▁ " ; } } void Print ( int arr [ ] , int n ) { cout << " Prime ▁ numbers ▁ in ▁ the ▁ first ▁ half ▁ are ▁ " ; prime_range ( 0 , n / 2 , arr ) ; cout << endl ; cout << " Prime ▁ numbers ▁ in ▁ the ▁ second ▁ half ▁ are ▁ " ; prime_range ( n / 2 , n , arr ) ; cout << endl ; } int main ( ) { int arr [ ] = { 2 , 5 , 10 , 15 , 17 , 21 , 23 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Print ( arr , n ) ; return 0 ; }
Count of elements that can be deleted without disturbing the mean of the initial array | C ++ implementation of the approach ; Function to find the elements which do not change the mean on removal ; To store the sum of the array elements ; To store the initial mean ; to store the count of required elements ; Iterate over the array ; Finding the new mean ; If the new mean equals to the initial mean ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countElements ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; float mean = ( float ) sum / n ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { float newMean = ( float ) ( sum - arr [ i ] ) / ( n - 1 ) ; if ( newMean == mean ) cnt ++ ; } return cnt ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countElements ( arr , n ) ; return 0 ; }
Find maximum xor of k elements in an array | C ++ implementation of the approach ; Function to return the maximum xor for a subset of size j from the given array ; If the subset is complete then return the xor value of the selected elements ; Return if already calculated for some mask and j at the i 'th index ; Initialize answer to 0 ; If we can still include elements in our subset include the i 'th element ; Exclude the i 'th element  ans store the max of both operations ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10000 NEW_LINE #define MAX_ELEMENT  50 NEW_LINE int dp [ MAX_ELEMENT ] [ MAX_ELEMENT ] [ MAX ] ; int Max_Xor ( int arr [ ] , int i , int j , int mask , int n ) { if ( i >= n ) { if ( j == 0 ) return mask ; else return 0 ; } if ( dp [ i ] [ j ] [ mask ] != -1 ) return dp [ i ] [ j ] [ mask ] ; int ans = 0 ; if ( j > 0 ) ans = Max_Xor ( arr , i + 1 , j - 1 , mask ^ arr [ i ] , n ) ; ans = max ( ans , Max_Xor ( arr , i + 1 , j , mask , n ) ) ; return dp [ i ] [ j ] [ mask ] = ans ; } int main ( ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int k = 3 ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << Max_Xor ( arr , 0 , k , 0 , n ) ; return 0 ; }
Find the prime P using given four integers | CPP program to possible prime number ; Function to check if a number is prime or not ; Function to find possible prime number ; Find a possible prime number ; Last condition ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Prime ( int n ) { for ( int j = 2 ; j <= sqrt ( n ) ; j ++ ) if ( n % j == 0 ) return false ; return true ; } int find_prime ( int x , int xsqmodp , int y , int ysqmodp ) { int n = x * x - xsqmodp ; int n1 = y * y - ysqmodp ; for ( int j = 2 ; j <= max ( sqrt ( n ) , sqrt ( n1 ) ) ; j ++ ) { if ( n % j == 0 && ( x * x ) % j == xsqmodp && n1 % j == 0 && ( y * y ) % j == ysqmodp ) if ( Prime ( j ) ) return j ; int j1 = n / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; j1 = n1 / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; } if ( n == n1 ) return n ; } int main ( ) { int x = 3 , xsqmodp = 0 , y = 5 , ysqmodp = 1 ; cout << find_prime ( x , xsqmodp , y , ysqmodp ) ; return 0 ; }
Time taken per hour for stoppage of Car | C ++ implementation of the approach ; Function to return the time taken per hour for stoppage ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfMinutes ( int S , int S1 ) { int Min = 0 ; Min = ( ( S - S1 ) / floor ( S ) ) * 60 ; return Min ; } int main ( ) { int S = 30 , S1 = 10 ; cout << numberOfMinutes ( S , S1 ) << " ▁ min " ; return 0 ; }
Removing a number from array without changing its arithmetic mean | CPP program to remove a number from the array without changing its arithmetic mean ; Function to remove a number from the array without changing its arithmetic mean ; Find sum of all elements ; If mean is an integer ; Check if mean is present in the array or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int FindElement ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + a [ i ] ; if ( sum % n == 0 ) { int m = sum / n ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == m ) return m ; } return -1 ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( a ) / sizeof ( int ) ; cout << FindElement ( a , n ) ; return 0 ; }
Sum of the Tan ( x ) expansion upto N terms | CPP program to find tan ( x ) expansion ; Function to find factorial of a number ; To store factorial of a number ; Return the factorial of a number ; Function to find tan ( x ) upto n terms ; To store value of the expansion ; This loops here calculate Bernoulli number which is further used to get the coefficient in the expansion of tan x ; Print the value of expansion ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fac ( int num ) { if ( num == 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= num ; i ++ ) fact = fact * i ; return fact ; } void Tanx_expansion ( int terms , int x ) { double sum = 0 ; for ( int i = 1 ; i <= terms ; i += 1 ) { double B = 0 ; int Bn = 2 * i ; for ( int k = 0 ; k <= Bn ; k ++ ) { double temp = 0 ; for ( int r = 0 ; r <= k ; r ++ ) temp = temp + pow ( -1 , r ) * fac ( k ) * pow ( r , Bn ) / ( fac ( r ) * fac ( k - r ) ) ; B = B + temp / ( ( double ) ( k + 1 ) ) ; } sum = sum + pow ( -4 , i ) * ( 1 - pow ( 4 , i ) ) * B * pow ( x , 2 * i - 1 ) / fac ( 2 * i ) ; } cout << setprecision ( 10 ) << sum ; } int main ( ) { int n = 6 , x = 1 ; Tanx_expansion ( n , x ) ; return 0 ; }
Minimum elements to be inserted in Array to make adjacent differences equal | C ++ program for the above approach ; Function to find gcd of two numbers ; Function to calculate minimum numbers to be inserted to make equal differences between two consecutive elements ; Check if there is only one element in the array then answer will be 0 ; Calculate difference between first and second element of array ; If there is only two elements in the array then gcd of differences of consecutive elements of array will be equal to difference of first and second element of the array ; Loop to calculate the gcd of the differences between consecutive elements of the array ; Loop to calculate the elements to be inserted ; Return the answer ; Driver code
#include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int minimum_elements ( int n , int arr [ ] ) { if ( n < 3 ) return 0 ; int g , ans = 0 , diff , cnt ; diff = arr [ 1 ] - arr [ 0 ] ; g = diff ; for ( int i = 2 ; i < n ; i ++ ) { diff = arr [ i ] - arr [ i - 1 ] ; g = gcd ( g , diff ) ; } for ( int i = 1 ; i < n ; i ++ ) { diff = arr [ i ] - arr [ i - 1 ] ; cnt = diff / g ; ans += ( cnt - 1 ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 5 , 8 , 10 , 12 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimum_elements ( n , arr ) ; return 0 ; }
Check if a number is Fermat Pseudoprime | C ++ program to check if N is Fermat pseudoprime to the base A or not ; Function to check if the given number is composite ; Check if there is any divisor of n less than sqrt ( n ) ; Effectively calculate ( x ^ y ) modulo mod ; Initialize result ; If power is odd , then update the answer ; Square the number and reduce the power to its half ; Return the result ; Function to check for Fermat Pseudoprime ; If it is composite and satisfy Fermat criterion ; Else return 0 ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkcomposite ( int n ) { for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return 1 ; } return 0 ; } int power ( int x , int y , int mod ) { int res = 1 ; while ( y ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } bool Check ( int n , int a ) { if ( a > 1 && checkcomposite ( n ) && power ( a , n - 1 , n ) == 1 ) return 1 ; return 0 ; } int main ( ) { int N = 645 ; int a = 2 ; cout << Check ( N , a ) ; return 0 ; }
Largest and smallest digit of a number | CPP program to largest and smallest digit of a number ; Function to the largest and smallest digit of a number ; Find the largest digit ; Find the smallest digit ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Digits ( int n ) { int largest = 0 ; int smallest = 9 ; while ( n ) { int r = n % 10 ; largest = max ( r , largest ) ; smallest = min ( r , smallest ) ; n = n / 10 ; } cout << largest << " ▁ " << smallest ; } int main ( ) { int n = 2346 ; Digits ( n ) ; return 0 ; }
Probability of distributing M items among X bags such that first bag contains N items | CPP program to find probability of first bag to contain N items such that M items are distributed among X bags ; Function to find factorial of a number ; Function to find nCr ; Function to find probability of first bag to contain N items such that M items are distributed among X bags ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } float Probability ( int M , int N , int X ) { return ( float ) ( nCr ( M - N - 1 , X - 2 ) / ( nCr ( M - 1 , X - 1 ) * 1.0 ) ) ; } int main ( ) { int M = 9 , X = 3 , N = 4 ; cout << Probability ( M , N , X ) ; return 0 ; }
Count of N digit numbers possible which satisfy the given conditions | C ++ implementation of the approach ; Function to return the factorial of n ; Function to return the count of numbers possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int Count_number ( int N ) { return ( N * fact ( N ) ) ; } int main ( ) { int N = 2 ; cout << Count_number ( N ) ; return 0 ; }
Find the count of M character words which have at least one character repeated | C ++ implementation for the above approach ; Function to return the factorial of a number ; Function to return the value of nPr ; Function to return the total number of M length words which have at least a single character repeated more than once ; Driver code
#include <math.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int fact ( int n ) { if ( n <= 1 ) return 1 ; return n * fact ( n - 1 ) ; } int nPr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } int countWords ( int N , int M ) { return pow ( N , M ) - nPr ( N , M ) ; } int main ( ) { int N = 10 , M = 5 ; cout << ( countWords ( N , M ) ) ; return 0 ; }
Minimum and maximum possible length of the third side of a triangle | C ++ implementation of the approach ; Function to find the minimum and the maximum possible length of the third side of the given triangle ; Not a valid triangle ; Not a valid triangle ; Driver code
#include <iostream> NEW_LINE using namespace std ; void find_length ( int s1 , int s2 ) { if ( s1 <= 0 s2 <= 0 ) { cout << -1 ; return ; } int max_length = s1 + s2 - 1 ; int min_length = max ( s1 , s2 ) - min ( s1 , s2 ) + 1 ; if ( min_length > max_length ) { cout << -1 ; return ; } cout << " Max ▁ = ▁ " << max_length << endl ; cout << " Min ▁ = ▁ " << min_length ; } int main ( ) { int s1 = 8 , s2 = 5 ; find_length ( s1 , s2 ) ; return 0 ; }
Number formed by the rightmost set bit in N | C ++ implementation of the approach ; Function to return the integer formed by taking the rightmost set bit in n ; n & ( n - 1 ) unsets the first set bit from the right in n ; Take xor with the original number The position of the ' changed ▁ bit ' will be set and rest will be unset ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int firstSetBit ( int n ) { int x = n & ( n - 1 ) ; return ( n ^ x ) ; } int main ( ) { int n = 12 ; cout << firstSetBit ( n ) ; return 0 ; }
Number of N length sequences whose product is M | C ++ implementation of the above approach ; Function to calculate the value of ncr effectively ; Initializing the result ; Multiply and divide simultaneously to avoid overflow ; Return the answer ; Function to return the number of sequences of length N such that their product is M ; Hashmap to store the prime factors of M ; Calculate the prime factors of M ; If i divides M it means it is a factor Divide M by i till it could be divided to store the exponent ; Increase the exponent count ; If the number is a prime number greater than sqrt ( M ) ; Initializing the ans ; Multiply the answer for every prime factor ; it . second represents the exponent of every prime factor ; Return the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ncr ( int n , int r ) { int res = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { res *= ( n - r + i ) ; res /= i ; } return res ; } int NoofSequences ( int N , int M ) { unordered_map < int , int > prime ; for ( int i = 2 ; i <= sqrt ( M ) ; i += 1 ) { while ( M % i == 0 ) { prime [ i ] += 1 ; M /= i ; } } if ( M > 1 ) { prime [ M ] += 1 ; } int ans = 1 ; for ( auto it : prime ) { ans *= ( ncr ( N + it . second - 1 , N - 1 ) ) ; } return ans ; } int main ( ) { int N = 2 , M = 6 ; cout << NoofSequences ( N , M ) ; return 0 ; }
Number of hours after which the second person moves ahead of the first person if they travel at a given speed | C ++ implementation of the above approach ; Function to return the number of hours for the second person to move ahead ; Time taken to equalize ; Time taken to move ahead ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findHours ( int a , int b , int k ) { if ( a >= b ) return -1 ; int time = k / ( b - a ) ; time = time + 1 ; return time ; } int main ( ) { int a = 4 , b = 5 , k = 1 ; cout << findHours ( a , b , k ) ; return 0 ; }
Number of ways of distributing N identical objects in R distinct groups with no groups empty | C ++ implementation of the above approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the number of ways to distribute N identical objects in R distinct objects ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } int NoOfDistributions ( int N , int R ) { return ncr ( N - 1 , R - 1 ) ; } int main ( ) { int N = 4 ; int R = 3 ; cout << NoOfDistributions ( N , R ) ; return 0 ; }
Number of ways of distributing N identical objects in R distinct groups | C ++ implementation of the above approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the number of ways to distribute N identical objects in R distinct objects ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } int NoOfDistributions ( int N , int R ) { return ncr ( N + R - 1 , R - 1 ) ; } int main ( ) { int N = 4 , R = 3 ; cout << NoOfDistributions ( N , R ) ; return 0 ; }
Smallest perfect cube in an array | C ++ implementation of the approach ; Function that returns true if n is a perfect cube ; Takes the sqrt of the number ; Checks if it is a perfect cube number ; Function to return the smallest perfect cube from the array ; Stores the minimum of all the perfect cubes from the array ; Traverse all elements in the array ; Store the minimum if current element is a perfect cube ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPerfectcube ( int n ) { int d = cbrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } int smallestPerfectCube ( int a [ ] , int n ) { int mini = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectcube ( a [ i ] ) ) { mini = min ( a [ i ] , mini ) ; } } return mini ; } int main ( ) { int a [ ] = { 16 , 8 , 25 , 2 , 3 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << smallestPerfectCube ( a , n ) ; return 0 ; }