codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum after repeatedly dividing N by a divisor | C ++ implementation of the above approach ; Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestDivisor ( int n ) { int mx = sqrt ( n ) ; for ( int i = 2 ; i <= mx ; i ++ ) if ( n % i == 0 ) return i ; return n ; } int maxSum ( int n ) { long long res = n ; while ( n > 1 ) { int divi = smallestDivisor ( n ) ; n /= divi ; res += n ; } return res ; } int main ( ) { int n = 34 ; cout << maxSum ( n ) ; return 0 ; }
Make all elements of an array equal with the given operation | C ++ implementation of the approach ; Function that returns true if all the elements of the array can be made equal with the given operation ; To store the sum of the array elements and the maximum element from the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int n , int k , int arr [ ] ) { int sum = arr [ 0 ] , maxVal = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum += arr [ i ] ; maxVal = max ( maxVal , arr [ i ] ) ; } if ( ( float ) maxVal > ( float ) ( sum + k ) / n ) return false ; return true ; } int main ( ) { int k = 8 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPossible ( n , k , arr ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximize the value of x + y + z such that ax + by + cz = n | C ++ implementation of the approach ; Function to return the maximum value of ( x + y + z ) such that ( ax + by + cz = n ) ; i represents possible values of a * x ; j represents possible values of b * y ; If z is an integer ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxResult ( int n , int a , int b , int c ) { int maxVal = 0 ; for ( int i = 0 ; i <= n ; i += a ) { for ( int j = 0 ; j <= n - i ; j += b ) { float z = ( float ) ( n - ( i + j ) ) / ( float ) ( c ) ; if ( floor ( z ) == ceil ( z ) ) { int x = i / a ; int y = j / b ; maxVal = max ( maxVal , x + y + ( int ) z ) ; } } } return maxVal ; } int main ( ) { int n = 10 , a = 5 , b = 3 , c = 4 ; cout << maxResult ( n , a , b , c ) ; return 0 ; }
Make all numbers of an array equal | C ++ implementation of the approach ; Function that returns true if all the array elements can be made equal with the given operation ; Divide number by 2 ; Divide number by 3 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool EqualNumbers ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] % 2 == 0 ) a [ i ] /= 2 ; while ( a [ i ] % 3 == 0 ) a [ i ] /= 3 ; if ( a [ i ] != a [ 0 ] ) { return false ; } } return true ; } int main ( ) { int a [ ] = { 50 , 75 , 150 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( EqualNumbers ( a , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximum GCD from Given Product of Unknowns | C ++ implementation of the approach ; Function to return the required gcd ; Count the number of times 2 divides p ; Equivalent to p = p / 2 ; ; If 2 divides p ; Check all the possible numbers that can divide p ; If n in the end is a prime number ; Return the required gcd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long max_gcd ( long n , long p ) { int count = 0 ; long gcd = 1 ; while ( p % 2 == 0 ) { p >>= 1 ; count ++ ; } if ( count > 0 ) gcd *= ( long ) pow ( 2 , count / n ) ; for ( long i = 3 ; i <= sqrt ( p ) ; i += 2 ) { count = 0 ; while ( p % i == 0 ) { count ++ ; p = p / i ; } if ( count > 0 ) { gcd *= ( long ) pow ( i , count / n ) ; } } if ( p > 2 ) gcd *= ( long ) pow ( p , 1 / n ) ; return gcd ; } int main ( ) { long n = 3 ; long p = 80 ; cout << max_gcd ( n , p ) ; }
Minimum positive integer divisible by C and is not in range [ A , B ] | C ++ implementation of the approach ; Function to return the required number ; If doesn 't belong to the range  then c is the required number ; Else get the next multiple of c starting from b + 1 ; Driver code
#include <iostream> NEW_LINE using namespace std ; int getMinNum ( int a , int b , int c ) { if ( c < a c > b ) return c ; int x = ( ( b / c ) * c ) + c ; return x ; } int main ( ) { int a = 2 , b = 4 , c = 4 ; cout << getMinNum ( a , b , c ) ; return 0 ; }
Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | C ++ implementation of the approach ; Function to return the count of required pairs ; Special case ; Number which will give the max value for ( ( n % i ) % j ) % n ; To store the maximum possible value of ( ( n % i ) % j ) % n ; Count of possible pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int n ) { if ( n == 2 ) return 4 ; int num = ( ( n / 2 ) + 1 ) ; int max = n % num ; int count = n - max ; return count ; } int main ( ) { int n = 5 ; cout << countPairs ( n ) ; }
Remove characters from a numeric string such that string becomes divisible by 8 | C ++ program to remove digits from a numeric string such that the number becomes divisible by 8 ; Function that return true if sub is a sub - sequence in s ; Function to return a multiple of 8 formed after removing 0 or more characters from the given string ; Iterate over all multiples of 8 ; If current multiple exists as a subsequence in the given string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkSub ( string sub , string s ) { int j = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) if ( sub [ j ] == s [ i ] ) j ++ ; return j == ( int ) sub . size ( ) ; } int getMultiple ( string s ) { for ( int i = 0 ; i < 1e3 ; i += 8 ) { if ( checkSub ( to_string ( i ) , s ) ) return i ; } return -1 ; } int main ( ) { string s = "3454" ; cout << getMultiple ( s ) ; return 0 ; }
Program to check if a number is divisible by any of its digits | C ++ implementation of above approach ; Converting integer to string ; Traversing the string ; find the actual digit ; If the number is divisible by digits then return yes ; If no digits are dividing the number then return no ; Driver Code ; passing this number to get result function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getResult ( int n ) { string st = to_string ( n ) ; for ( int i = 0 ; i < st . length ( ) ; i ++ ) { int d = st [ i ] - 48 ; if ( n % d == 0 ) { return " Yes " ; } } return " No " ; } int main ( ) { int n = 9876543 ; cout << getResult ( n ) ; }
Program to find sum of harmonic series | CPP program to find sum of harmonic series using recursion ; Base condition ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float sum ( float n ) { if ( n < 2 ) return 1 ; else return 1 / n + ( sum ( n - 1 ) ) ; } int main ( ) { cout << ( sum ( 8 ) ) << endl ; cout << ( sum ( 10 ) ) << endl ; return 0 ; }
Sum of P terms of an AP if Mth and Nth terms are given | C ++ implementation of the above approach ; Function to calculate the value of the ; Calculate value of d using formula ; Calculate value of a using formula ; Return pair ; Function to calculate value sum of first p numbers of the series ; First calculate value of a and d ; Calculate the sum by using formula ; Return the sum ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < double , double > findingValues ( double m , double n , double mth , double nth ) { double d = ( abs ( mth - nth ) ) / abs ( ( m - 1 ) - ( n - 1 ) ) ; double a = mth - ( ( m - 1 ) * d ) ; return make_pair ( a , d ) ; } double findSum ( int m , int n , int mth , int nth , int p ) { pair < double , double > ad ; ad = findingValues ( m , n , mth , nth ) ; double a = ad . first , d = ad . second ; double sum = ( p * ( 2 * a + ( p - 1 ) * d ) ) / 2 ; return sum ; } int main ( ) { double m = 6 , n = 10 , mTerm = 12 , nTerm = 20 , p = 5 ; cout << findSum ( m , n , mTerm , nTerm , p ) << endl ; return 0 ; }
Print all integers that are sum of powers of two given numbers | C ++ implementation of the approach ; Function to print powerful integers ; Set is used to store distinct numbers in sorted order ; Store all the powers of y < bound in a vector to avoid calculating them again and again ; x ^ i ; If num is within limits insert it into the set ; Break out of the inner loop ; Adding any number to it will be out of bounds ; Increment i ; Print the contents of the set ; Driver code ; Print powerful integers
#include <bits/stdc++.h> NEW_LINE using namespace std ; void powerfulIntegers ( int x , int y , int bound ) { set < int > s ; vector < int > powersOfY ; int i ; powersOfY . push_back ( 1 ) ; for ( i = y ; i < bound && y != 1 ; i = i * y ) powersOfY . push_back ( i ) ; i = 0 ; while ( true ) { int xPowI = pow ( x , i ) ; for ( auto j = powersOfY . begin ( ) ; j != powersOfY . end ( ) ; ++ j ) { int num = xPowI + * j ; if ( num <= bound ) s . insert ( num ) ; else break ; } if ( xPowI >= bound x == 1 ) break ; i ++ ; } set < int > :: iterator itr ; for ( itr = s . begin ( ) ; itr != s . end ( ) ; itr ++ ) { cout << * itr << " ▁ " ; } } int main ( ) { int x = 2 , y = 3 , bound = 10 ; powerfulIntegers ( x , y , bound ) ; return 0 ; }
Distribute N candies among K people | C ++ code for better approach to distribute candies ; Function to find out the number of candies every person received ; Count number of complete turns ; Get the last term ; Stores the number of candies ; Last term of last and current series ; Sum of current and last series ; Sum of current series only ; If sum of current is less than N ; else Individually distribute ; First term ; Distribute candies till there ; Candies available ; Not available ; Count the total candies ; Print the total candies ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void candies ( int n , int k ) { int count = 0 ; int ind = 1 ; int arr [ k ] ; memset ( arr , 0 , sizeof ( arr ) ) ; while ( n ) { int f1 = ( ind - 1 ) * k ; int f2 = ind * k ; int sum1 = ( f1 * ( f1 + 1 ) ) / 2 ; int sum2 = ( f2 * ( f2 + 1 ) ) / 2 ; int res = sum2 - sum1 ; if ( res <= n ) { count ++ ; n -= res ; ind ++ ; } { int i = 0 ; int term = ( ( ind - 1 ) * k ) + 1 ; while ( n > 0 ) { if ( term <= n ) { arr [ i ++ ] = term ; n -= term ; term ++ ; } else { arr [ i ++ ] = n ; n = 0 ; } } } } for ( int i = 0 ; i < k ; i ++ ) arr [ i ] += ( count * ( i + 1 ) ) + ( k * ( count * ( count - 1 ) ) / 2 ) ; for ( int i = 0 ; i < k ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int n = 10 , k = 3 ; candies ( n , k ) ; return 0 ; }
Distribute N candies among K people | C ++ implementation of the above approach ; Function to find out the number of candies every person received ; Count number of complete turns ; Get the last term ; Stores the number of candies ; Do a binary search to find the number whose sum is less than N . ; Get mide ; If sum is below N ; Find number of complete turns ; Right halve ; Left halve ; Last term of last complete series ; Subtract the sum till ; First term of incomplete series ; Count the total candies ; Print the total candies ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void candies ( int n , int k ) { int count = 0 ; int ind = 1 ; int arr [ k ] ; memset ( arr , 0 , sizeof ( arr ) ) ; int low = 0 , high = n ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; int sum = ( mid * ( mid + 1 ) ) >> 1 ; if ( sum <= n ) { count = mid / k ; low = mid + 1 ; } else { high = mid - 1 ; } } int last = ( count * k ) ; n -= ( last * ( last + 1 ) ) / 2 ; int i = 0 ; int term = ( count * k ) + 1 ; while ( n ) { if ( term <= n ) { arr [ i ++ ] = term ; n -= term ; term ++ ; } else { arr [ i ] += n ; n = 0 ; } } for ( int i = 0 ; i < k ; i ++ ) arr [ i ] += ( count * ( i + 1 ) ) + ( k * ( count * ( count - 1 ) ) / 2 ) ; for ( int i = 0 ; i < k ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int n = 7 , k = 4 ; candies ( n , k ) ; return 0 ; }
Smallest multiple of 3 which consists of three given non | C ++ implementation of the approach ; Function to return the minimum number divisible by 3 formed by the given digits ; Sort the given array in ascending ; Check if any single digit is divisible by 3 ; Check if any two digit number formed by the given digits is divisible by 3 starting from the minimum ; Generate the two digit number ; If none of the above is true , we can form three digit number by taking a [ 0 ] three times . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printSmallest ( int a [ 3 ] ) { int sum , sum1 ; sort ( a , a + 3 ) ; int i , j , k , num ; for ( int i = 0 ; i < 3 ; i ++ ) { if ( a [ i ] % 3 == 0 ) return a [ i ] ; } for ( i = 0 ; i < 3 ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { num = ( a [ i ] * 10 ) + a [ j ] ; if ( num % 3 == 0 ) return num ; } } return a [ 0 ] * 100 + a [ 0 ] * 10 + a [ 0 ] ; } int main ( ) { int arr [ ] = { 7 , 7 , 1 } ; cout << printSmallest ( arr ) ; return 0 ; }
Print matrix after applying increment operations in M ranges | C ++ implementation of the approach ; Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Driver code ; Size of the matrix ; Queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void updateMatrix ( int n , int q [ 3 ] [ 4 ] ) { int i , j ; int mat [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) mat [ i ] [ j ] = 0 ; for ( i = 0 ; i < 3 ; i ++ ) { int X1 = q [ i ] [ 0 ] ; int Y1 = q [ i ] [ 1 ] ; int X2 = q [ i ] [ 2 ] ; int Y2 = q [ i ] [ 3 ] ; mat [ X1 ] [ Y1 ] ++ ; if ( Y2 + 1 < n ) mat [ X2 ] [ Y2 + 1 ] -- ; else if ( X2 + 1 < n ) mat [ X2 + 1 ] [ 0 ] -- ; } int sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { sum += mat [ i ] [ j ] ; cout << sum << " ▁ " ; } cout << endl ; } } int main ( ) { int n = 5 ; int q [ 3 ] [ 4 ] = { { 0 , 0 , 1 , 2 } , { 1 , 2 , 3 , 4 } , { 1 , 4 , 3 , 4 } } ; updateMatrix ( n , q ) ; return 0 ; }
Replace the maximum element in the array by coefficient of range | C ++ implementation to replace maximum element by coefficient of range ; Utility function to print the contents of the array ; Function to replace the maximum element from the array with the coefficient of range of the array ; Maximum element from the array ; Minimum element from the array ; Calculate the coefficient of range for the array ; Assuming all the array elements are distinc Replace the maximum element with the coefficient of range of the array ; Print the updated array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( float arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void replaceMax ( float arr [ ] , int n ) { float max = * std :: max_element ( arr , arr + n ) ; float min = * std :: min_element ( arr , arr + n ) ; float range = max - min ; float coeffOfRange = range / ( max + min ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == max ) { arr [ i ] = coeffOfRange ; break ; } } printArr ( arr , n ) ; } int main ( ) { float arr [ ] = { 15 , 16 , 10 , 9 , 6 , 7 , 17 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; replaceMax ( arr , n ) ; return 0 ; }
Divide the two given numbers by their common divisors | C ++ implementation of above approach ; print the numbers after dividing them by their common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; divide A and B by their common factors
#include <bits/stdc++.h> NEW_LINE using namespace std ; void divide ( int a , int b ) { for ( int i = 2 ; i <= min ( a , b ) ; i ++ ) { while ( a % i == 0 && b % i == 0 ) { a = a / i ; b = b / i ; } } cout << " A ▁ = ▁ " << a << " , ▁ B ▁ = ▁ " << b << endl ; } int main ( ) { int A = 10 , B = 15 ; divide ( A , B ) ; return 0 ; }
Divide the two given numbers by their common divisors | C ++ implementation of above approach ; Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } void commDiv ( int a , int b ) { int n = gcd ( a , b ) ; a = a / n ; b = b / n ; cout << " A ▁ = ▁ " << a << " , ▁ B ▁ = ▁ " << b << endl ; } int main ( ) { int a = 10 , b = 15 ; commDiv ( a , b ) ; return 0 ; }
Minimum absolute difference between N and a power of 2 | C ++ implementation of the above approach ; Function to return the minimum difference between N and a power of 2 ; Power of 2 closest to n on its left ; Power of 2 closest to n on its right ; Return the minimum abs difference ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minAbsDiff ( int n ) { int left = 1 << ( ( int ) floor ( log2 ( n ) ) ) ; int right = left * 2 ; return min ( ( n - left ) , ( right - n ) ) ; } int main ( ) { int n = 15 ; cout << minAbsDiff ( n ) ; return 0 ; }
Find probability that a player wins when probabilities of hitting the target are given | C ++ implementation of the approach ; Function to return the probability of the winner ; Driver Code ; Will print 9 digits after the decimal point
#include <bits/stdc++.h> NEW_LINE using namespace std ; double find_probability ( double p , double q , double r , double s ) { double t = ( 1 - p / q ) * ( 1 - r / s ) ; double ans = ( p / q ) / ( 1 - t ) ; return ans ; } int main ( ) { double p = 1 , q = 2 , r = 1 , s = 2 ; cout << fixed << setprecision ( 9 ) << find_probability ( p , q , r , s ) ; return 0 ; }
Represent n as the sum of exactly k powers of two | Set 2 | C ++ implementation of the above approach ; Function to print k numbers which are powers of two and whose sum is equal to n ; Initialising the sum with k ; Initialising an array A with k elements and filling all elements with 1 ; Iterating A [ ] from k - 1 to 0 ; Update sum and A [ i ] till sum + A [ i ] is less than equal to n ; Impossible to find the combination ; Possible solution is stored in A [ ] ; Driver code
#include <iostream> NEW_LINE using namespace std ; void FindAllElements ( int n , int k ) { int sum = k ; int A [ k ] ; fill ( A , A + k , 1 ) ; for ( int i = k - 1 ; i >= 0 ; -- i ) { while ( sum + A [ i ] <= n ) { sum += A [ i ] ; A [ i ] *= 2 ; } } if ( sum != n ) { cout << " Impossible " ; } else { for ( int i = 0 ; i < k ; ++ i ) cout << A [ i ] << ' ▁ ' ; } } int main ( ) { int n = 12 ; int k = 6 ; FindAllElements ( n , k ) ; return 0 ; }
Check whether a + b = c or not after removing all zeroes from a , b and c | C ++ program to check the sum after Removing all zeroes is true or not ; Function to remove zeroes ; Initialize result to zero holds the Result after removing zeroes from no ; Initialize variable d to 1 that holds digits of no ; Loop while n is greater then zero ; Check if n mod 10 is not equal to zero ; store the result by removing zeroes And increment d by 10 ; Go to the next digit ; Return the result ; Function to check if sum is true after Removing all zeroes . ; Call removeZero ( ) for both sides and check whether they are equal After removing zeroes . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int removeZero ( int n ) { int res = 0 ; int d = 1 ; while ( n > 0 ) { if ( n % 10 != 0 ) { res += ( n % 10 ) * d ; d *= 10 ; } n /= 10 ; } return res ; } bool isEqual ( int a , int b ) { if ( removeZero ( a ) + removeZero ( b ) == removeZero ( a + b ) ) return true ; return false ; } int main ( ) { int a = 105 , b = 106 ; isEqual ( a , b ) ? cout << " Yes " : cout << " No " ; return 0 ; }
A Sum Array Puzzle | C ++ implementation of above approach ; Allocate memory for temporary arrays leftSum [ ] , rightSum [ ] and Sum [ ] ; Left most element of left array is always 0 ; Rightmost most element of right array is always 0 ; Construct the left array ; Construct the right array ; Construct the sum array using left [ ] and right [ ] ; print the constructed prod array ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumArray ( int arr [ ] , int n ) { int leftSum [ n ] , rightSum [ n ] , Sum [ n ] , i , j ; leftSum [ 0 ] = 0 ; rightSum [ n - 1 ] = 0 ; for ( i = 1 ; i < n ; i ++ ) leftSum [ i ] = arr [ i - 1 ] + leftSum [ i - 1 ] ; for ( j = n - 2 ; j >= 0 ; j -- ) rightSum [ j ] = arr [ j + 1 ] + rightSum [ j + 1 ] ; for ( i = 0 ; i < n ; i ++ ) Sum [ i ] = leftSum [ i ] + rightSum [ i ] ; for ( i = 0 ; i < n ; i ++ ) cout << Sum [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 3 , 6 , 4 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sumArray ( arr , n ) ; return 0 ; }
Find minimum x such that ( x % k ) * ( x / k ) == n | Set | CPP Program to find the minimum positive X such that the given equation holds true ; This function gives the required answer ; Iterate for all the factors ; Check if i is a factor ; Consider i to be A and n / i to be B ; Consider i to be B and n / i to be A ; Driver Code to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumX ( int n , int k ) { int mini = INT_MAX ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int fir = i ; int sec = n / i ; int num1 = fir * k + sec ; int res = ( num1 / k ) * ( num1 % k ) ; if ( res == n ) mini = min ( num1 , mini ) ; int num2 = sec * k + fir ; res = ( num2 / k ) * ( num2 % k ) ; if ( res == n ) mini = min ( num2 , mini ) ; } } return mini ; } int main ( ) { int n = 4 , k = 6 ; cout << minimumX ( n , k ) << endl ; n = 5 , k = 5 ; cout << minimumX ( n , k ) << endl ; return 0 ; }
Find minimum x such that ( x % k ) * ( x / k ) == n | CPP Program to find the minimum positive X such that the given equation holds true ; This function gives the required answer ; Iterate over all possible remainders ; it must divide n ; Driver Code to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumX ( int n , int k ) { int ans = INT_MAX ; for ( int rem = k - 1 ; rem > 0 ; rem -- ) { if ( n % rem == 0 ) ans = min ( ans , rem + ( n / rem ) * k ) ; } return ans ; } int main ( ) { int n = 4 , k = 6 ; cout << minimumX ( n , k ) << endl ; n = 5 , k = 5 ; cout << minimumX ( n , k ) << endl ; return 0 ; }
Find nth Hermite number | C ++ program to find nth Hermite number ; Function to return nth Hermite number ; Base conditions ; Driver Code ; Print nth Hermite number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getHermiteNumber ( int n ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return 0 ; else return -2 * ( n - 1 ) * getHermiteNumber ( n - 2 ) ; } int main ( ) { int n = 6 ; cout << getHermiteNumber ( n ) ; return 0 ; }
Find numbers a and b that satisfy the given conditions | C ++ implementation of the above approach ; Function to print the required numbers ; Suppose b = n and we want a % b = 0 and also ( a / b ) < n so a = b * ( n - 1 ) ; Special case if n = 1 we get a = 0 so ( a * b ) < n ; If no pair satisfies the conditions ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find ( int n ) { int b = n ; int a = b * ( n - 1 ) ; if ( a * b > n && a / b < n ) { cout << " a ▁ = ▁ " << a << " , ▁ b ▁ = ▁ " << b ; } else cout << -1 << endl ; } int main ( ) { int n = 10 ; find ( n ) ; return 0 ; }