codeparrot/xlcost-text-to-code huggingface dataset:
Nth positive number whose absolute difference of adjacent digits is at most 1 | C ++ Program to find Nth number with absolute difference between all adjacent digits at most 1. ; Return Nth number with absolute difference between all adjacent digits at most 1. ; To store all such numbers ; Enqueue all integers from 1 to 9 in increasing order . ; Perform the operation N times so that we can get all such N numbers . ; Store the front element of queue , in array and pop it from queue . ; If the last digit of dequeued integer is not 0 , then enqueue the next such number . ; Enqueue the next such number ; If the last digit of dequeued integer is not 9 , then enqueue the next such number . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNthNumber ( int N ) { long long arr [ N + 1 ] ; queue < long long > q ; for ( int i = 1 ; i <= 9 ; i ++ ) q . push ( i ) ; for ( int i = 1 ; i <= N ; i ++ ) { arr [ i ] = q . front ( ) ; q . pop ( ) ; if ( arr [ i ] % 10 != 0 ) q . push ( arr [ i ] * 10 + arr [ i ] % 10 - 1 ) ; q . push ( arr [ i ] * 10 + arr [ i ] % 10 ) ; if ( arr [ i ] % 10 != 9 ) q . push ( arr [ i ] * 10 + arr [ i ] % 10 + 1 ) ; } cout << arr [ N ] << endl ; } int main ( ) { int N = 21 ; findNthNumber ( N ) ; return 0 ; }
Unique element in an array where all elements occur K times except one | Set 2 | C ++ program for the above approach ; Function that find the unique element in the array arr [ ] ; Store all unique element in set ; Sum of all element of the array ; Sum of element in the set ; Print the unique element using formula ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findUniqueElements ( int arr [ ] , int N , int K ) { unordered_set < int > s ( arr , arr + N ) ; int arr_sum = accumulate ( arr , arr + N , 0 ) ; int set_sum = accumulate ( s . begin ( ) , s . end ( ) , 0 ) ; cout << ( K * set_sum - arr_sum ) / ( K - 1 ) ; } int main ( ) { int arr [ ] = { 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; findUniqueElements ( arr , N , K ) ; return 0 ; }
Form the Cubic equation from the given roots | C ++ program for the approach ; Function to find the cubic equation whose roots are a , b and c ; Find the value of coefficient ; Print the equation as per the above coefficients ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findEquation ( int a , int b , int c ) { int X = ( a + b + c ) ; int Y = ( a * b ) + ( b * c ) + ( c * a ) ; int Z = a * b * c ; cout << " x ^ 3 ▁ - ▁ " << X << " x ^ 2 ▁ + ▁ " << Y << " x ▁ - ▁ " << Z << " ▁ = ▁ 0" ; } int main ( ) { int a = 5 , b = 2 , c = 3 ; findEquation ( a , b , c ) ; return 0 ; }
Gill 's 4th Order Method to solve Differential Equations | C ++ program to implement Gill 's method ; A sample differential equation " dy / dx ▁ = ▁ ( x ▁ - ▁ y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 ; Count number of iterations using step size or height h ; Value of K_i ; Initial value of y ( 0 ) ; Iterate for number of iteration ; Value of K1 ; Value of K2 ; Value of K3 ; Value of K4 ; Find the next value of y ( n + 1 ) using y ( n ) and values of K in the above steps ; Update next value of x ; Return the final value of dy / dx ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float dydx ( float x , float y ) { return ( x - y ) / 2 ; } float Gill ( float x0 , float y0 , float x , float h ) { int n = ( int ) ( ( x - x0 ) / h ) ; float k1 , k2 , k3 , k4 ; float y = y0 ; for ( int i = 1 ; i <= n ; i ++ ) { k1 = h * dydx ( x0 , y ) ; k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; k3 = h * dydx ( x0 + 0.5 * h , y + 0.5 * ( -1 + sqrt ( 2 ) ) * k1 + k2 * ( 1 - 0.5 * sqrt ( 2 ) ) ) ; k4 = h * dydx ( x0 + h , y - ( 0.5 * sqrt ( 2 ) ) * k2 + k3 * ( 1 + 0.5 * sqrt ( 2 ) ) ) ; y = y + ( 1.0 / 6 ) * ( k1 + ( 2 - sqrt ( 2 ) ) * k2 + ( 2 + sqrt ( 2 ) ) * k3 + k4 ) ; x0 = x0 + h ; } return y ; } int main ( ) { float x0 = 0 , y = 3.0 , x = 5.0 , h = 0.2 ; printf ( " y ( x ) ▁ = ▁ % .6f " , Gill ( x0 , y , x , h ) ) ; return 0 ; }
Program to print numbers from N to 1 in reverse order | C ++ program to print all numbers between 1 to N in reverse order ; Recursive function to print from N to 1 ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PrintReverseOrder ( int N ) { for ( int i = N ; i > 0 ; i -- ) cout << i << " ▁ " ; } int main ( ) { int N = 5 ; PrintReverseOrder ( N ) ; return 0 ; }
Find count of numbers from 0 to n which satisfies the given equation for a value K | C ++ implementation to Find the total count of all the numbers from 0 to n which satisfies the given equation for a value K ; Function to find the values ; Calculate the LCM ; Calculate the multiples of lcm ; Find the values which satisfies the given condition ; Subtract the extra values ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findAns ( int a , int b , int n ) { int lcm = ( a * b ) / __gcd ( a , b ) ; int multiples = ( n / lcm ) + 1 ; int answer = max ( a , b ) * multiples ; int lastvalue = lcm * ( n / lcm ) + max ( a , b ) ; if ( lastvalue > n ) answer = answer - ( lastvalue - n - 1 ) ; return answer ; } int main ( ) { int a = 1 , b = 13 , n = 500 ; cout << findAns ( a , b , n ) << endl ; }
Program to find if two numbers and their AM and HM are present in an array using STL | C ++ program to check if two numbers are present in an array then their AM and HM are also present . Finally , find the GM of the numbers ; Function to find the Arithmetic Mean of 2 numbers ; Function to find the Harmonic Mean of 2 numbers ; Following function checks and computes the desired results based on the means ; Calculate means ; Hash container ( Set ) to store elements ; Insertion of array elements in the Set ; Conditionals to check if numbers are present in array by Hashing ; Conditionals to check if the AM and HM of the numbers are present in array ; If all conditions are satisfied , the Geometric Mean is calculated ; If numbers are found but the respective AM and HM are not found in the array ; If none of the conditions are satisfied ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float ArithmeticMean ( float A , float B ) { return ( A + B ) / 2 ; } float HarmonicMean ( float A , float B ) { return ( 2 * A * B ) / ( A + B ) ; } void CheckArithmeticHarmonic ( float arr [ ] , float A , float B , int N ) { float AM = ArithmeticMean ( A , B ) ; float HM = HarmonicMean ( A , B ) ; unordered_set < float > Hash ; for ( int i = 0 ; i < N ; i ++ ) { Hash . insert ( arr [ i ] ) ; } if ( Hash . find ( A ) != Hash . end ( ) && Hash . find ( B ) != Hash . end ( ) ) { if ( Hash . find ( AM ) != Hash . end ( ) && Hash . find ( HM ) != Hash . end ( ) ) { cout << " GM ▁ = ▁ " ; printf ( " % 0.2f " , sqrt ( AM * HM ) ) ; } else { cout << " AM ▁ and ▁ HM ▁ not ▁ found " ; } } else { cout << " Numbers ▁ not ▁ found " ; } } int main ( ) { float arr [ ] = { 1.0 , 2.0 , 2.5 , 3.0 , 4.0 , 4.5 , 5.0 , 6.0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; float A = 3.0 ; float B = 6.0 ; CheckArithmeticHarmonic ( arr , A , B , N ) ; return 0 ; }
Minimum decrements to make integer A divisible by integer B | C ++ implementation to count Total numbers moves to make integer A divisible by integer B ; Function that print number of moves required ; calculate modulo ; print the required answer ; Driver Code ; initialise A and B
#include <bits/stdc++.h> NEW_LINE using namespace std ; void movesRequired ( int a , int b ) { int total_moves = a % b ; cout << total_moves << " STRNEWLINE " ; } int main ( ) { int A = 10 , B = 3 ; movesRequired ( A , B ) ; return 0 ; }
Pythagorean Triplet with given sum using single loop | C ++ program to find the Pythagorean Triplet with given sum ; Function to calculate the Pythagorean triplet in O ( n ) ; Iterate a from 1 to N - 1. ; Calculate value of b ; The value of c = n - a - b ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PythagoreanTriplet ( int n ) { int flag = 0 ; for ( int a = 1 ; a < n ; a ++ ) { int b = ( n * n - 2 * n * a ) / ( 2 * n - 2 * a ) ; int c = n - a - b ; if ( a * a + b * b == c * c && b > 0 && c > 0 ) { cout << a << " ▁ " << b << " ▁ " << c ; flag = 1 ; break ; } } if ( flag == 0 ) { cout << " - 1" ; } return ; } int main ( ) { int N = 12 ; PythagoreanTriplet ( N ) ; return 0 ; }
Check if there exists a number with X factors out of which exactly K are prime | C ++ program to check if there exists a number with X factors out of which exactly K are prime ; Function to check if such number exists ; To store the sum of powers of prime factors of X which determines the maximum count of numbers whose product can form X ; Determining the prime factors of X ; To check if the number is prime ; If X is 1 , then we cannot form a number with 1 factor and K prime factor ( as K is atleast 1 ) ; If X itself is prime then it can be represented as a power of only 1 prime factor which is X itself so we return true ; If sum of the powers of prime factors of X is greater than or equal to K , which means X can be represented as a product of K numbers , we return true ; In any other case , we return false as we cannot form a number with X factors and K prime factors ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int X , int K ) { int prime , temp , sqr , i ; prime = 0 ; temp = X ; sqr = sqrt ( X ) ; for ( i = 2 ; i <= sqr ; i ++ ) { while ( temp % i == 0 ) { temp = temp / i ; prime ++ ; } } if ( temp > 2 ) prime ++ ; if ( X == 1 ) return false ; if ( prime == 1 && K == 1 ) return true ; else if ( prime >= K ) return true ; else return false ; } int main ( ) { int X , K ; X = 4 ; K = 2 ; if ( check ( X , K ) ) cout << " Yes " ; else cout << " No " ; }
Number of subsets with same AND , OR and XOR values in an Array | C ++ implementation to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Traverse through all the subsets ; Finding the subsets with the bits of ' i ' which are set ; Computing the bitwise AND ; Computing the bitwise OR ; Computing the bitwise XOR ; Comparing all the three values ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1000000007 ; int countSubsets ( int a [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int bitwiseAND = -1 ; int bitwiseOR = 0 ; int bitwiseXOR = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( i & ( 1 << j ) ) { if ( bitwiseAND == -1 ) bitwiseAND = a [ j ] ; else bitwiseAND &= a [ j ] ; bitwiseOR |= a [ j ] ; bitwiseXOR ^= a [ j ] ; } } if ( bitwiseAND == bitwiseOR && bitwiseOR == bitwiseXOR ) answer = ( answer + 1 ) % mod ; } return answer ; } int main ( ) { int N = 6 ; int A [ N ] = { 1 , 3 , 2 , 1 , 2 , 1 } ; cout << countSubsets ( A , N ) ; return 0 ; }
Count of Subsets containing only the given value K | C ++ implementation to find the number of subsets formed by the given value K ; Function to find the number of subsets formed by the given value K ; Count is used to maintain the number of continuous K 's ; Iterating through the array ; If the element in the array is equal to K ; count * ( count + 1 ) / 2 is the total number of subsets with only K as their element ; Change count to 0 because other element apart from K has been found ; To handle the last set of K 's ; Driver code
#include <iostream> NEW_LINE using namespace std ; int count ( int arr [ ] , int N , int K ) { int count = 0 , ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == K ) { count = count + 1 ; } else { ans += ( count * ( count + 1 ) ) / 2 ; count = 0 ; } } ans = ans + ( count * ( count + 1 ) ) / 2 ; return ans ; } int main ( ) { int arr [ ] = { 1 , 0 , 0 , 1 , 1 , 0 , 0 } ; int N = sizeof ( arr ) / sizeof ( int ) ; int K = 0 ; cout << count ( arr , N , K ) ; }
Ternary number system or Base 3 numbers | C ++ program to convert decimal number to ternary number ; Function to convert a decimal number to a ternary number ; Base case ; Finding the remainder when N is divided by 3 ; Recursive function to call the function for the integer division of the value N / 3 ; Handling the negative cases ; Function to convert the decimal to ternary ; If the number is greater than 0 , compute the ternary representation of the number ; Driver code
#include <cstdio> NEW_LINE #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void convertToTernary ( int N ) { if ( N == 0 ) return ; int x = N % 3 ; N /= 3 ; if ( x < 0 ) N += 1 ; convertToTernary ( N ) ; if ( x < 0 ) cout << x + ( 3 * -1 ) ; else cout << x ; } void convert ( int Decimal ) { cout << " Ternary ▁ number ▁ of ▁ " << Decimal << " ▁ is : ▁ " ; if ( Decimal != 0 ) { convertToTernary ( Decimal ) ; } else cout << "0" << endl ; } int main ( ) { int Decimal = 2747 ; convert ( Decimal ) ; return 0 ; }
Largest number less than or equal to Z that leaves a remainder X when divided by Y | C ++ implementation to Find the largest non - negative number that is less than or equal to integer Z and leaves a remainder X when divided by Y ; Function to get the number ; remainder can ' t ▁ be ▁ larger ▁ ▁ than ▁ the ▁ largest ▁ number , ▁ ▁ if ▁ so ▁ then ▁ answer ▁ doesn ' t exist . ; reduce number by x ; finding the possible number that is divisible by y ; this number is always <= x as we calculated over z - x ; Driver Code ; initialise the three integers
#include <bits/stdc++.h> NEW_LINE using namespace std ; int get ( int x , int y , int z ) { if ( x > z ) return -1 ; int val = z - x ; int div = ( z - x ) / y ; int ans = div * y + x ; return ans ; } int main ( ) { int x = 1 , y = 5 , z = 8 ; cout << get ( x , y , z ) << " STRNEWLINE " ; return 0 ; }
Find the conjugate of a Complex number | C ++ implementation to Find the conjugate of a complex number ; Function to find conjugate of a complex number ; store index of ' + ' ; store index of ' - ' ; print the result ; Driver code ; initialise the complex number
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( string s ) { string z = s ; int l = s . length ( ) ; int i ; if ( s . find ( ' + ' ) < l ) { i = s . find ( ' + ' ) ; replace ( s . begin ( ) , s . end ( ) , ' + ' , ' - ' ) ; } else { i = s . find ( ' - ' ) ; replace ( s . begin ( ) , s . end ( ) , ' - ' , ' + ' ) ; } cout << " Conjugate ▁ of ▁ " << z << " ▁ = ▁ " << s << endl ; } int main ( ) { string s = "3-4i " ; solve ( s ) ; return 0 ; }
Count of greater elements for each element in the Array | C ++ implementation of the above approach ; Store the frequency of the array elements ; Store the sum of frequency of elements greater than the current element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countOfGreaterElements ( int arr [ ] , int n ) { map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] ++ ; } int x = 0 ; for ( auto it = mp . rbegin ( ) ; it != mp . rend ( ) ; it ++ ) { int temp = it -> second ; mp [ it -> first ] = x ; x += temp ; } for ( int i = 0 ; i < n ; i ++ ) cout << mp [ arr [ i ] ] << " ▁ " ; } int main ( ) { int arr [ ] = { 7 , 9 , 5 , 2 , 1 , 3 , 4 , 8 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countOfGreaterElements ( arr , n ) ; return 0 ; }
Minimum operations required to make two numbers equal | C ++ program to find minimum operations required to make two numbers equal ; Function to return the minimum operations required ; Keeping B always greater ; Reduce B such that gcd ( A , B ) becomes 1. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int minOperations ( long long int A , long long int B ) { if ( A > B ) swap ( A , B ) ; B = B / __gcd ( A , B ) ; return B - 1 ; } int main ( ) { long long int A = 7 , B = 15 ; cout << minOperations ( A , B ) << endl ; return 0 ; }
Program to determine the Quadrant of a Complex number | C ++ program to determine the quadrant of a complex number ; Function to determine the quadrant of a complex number ; Storing the index of ' + ' ; Storing the index of ' - ' ; Finding the real part of the complex number ; Finding the imaginary part of the complex number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void quadrant ( string s ) { int l = s . length ( ) ; int i ; if ( s . find ( ' + ' ) < l ) { i = s . find ( ' + ' ) ; } else { i = s . find ( ' - ' ) ; } string real = s . substr ( 0 , i ) ; string imaginary = s . substr ( i + 1 , l - 1 ) ; int x = stoi ( real ) ; int y = stoi ( imaginary ) ; if ( x > 0 and y > 0 ) cout << " Quadrant ▁ 1" ; else if ( x < 0 and y > 0 ) cout << " Quadrant ▁ 2" ; else if ( x < 0 and y < 0 ) cout << " Quadrant ▁ 3" ; else if ( x > 0 and y < 0 ) cout << " Quadrant ▁ 4" ; else if ( x == 0 and y > 0 ) cout << " Lies ▁ on ▁ positive " << " ▁ Imaginary ▁ axis " ; else if ( x == 0 and y < 0 ) cout << " Lies ▁ on ▁ negative " << " ▁ Imaginary ▁ axis " ; else if ( y == 0 and x < 0 ) cout << " Lies ▁ on ▁ negative " << " ▁ X - axis " ; else if ( y == 0 and x > 0 ) cout << " Lies ▁ on ▁ positive " << " ▁ X - axis " ; else cout << " Lies ▁ on ▁ the ▁ Origin " ; } int main ( ) { string s = "5 + 3i " ; quadrant ( s ) ; return 0 ; }
Sum and Product of all Fibonacci Nodes of a Singly Linked List | C ++ implementation to find the sum and product of all of the Fibonacci nodes in a singly linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Allocate new node ; Insert the data ; Link the old list to the new node ; Move the head to point the new node ; Function that returns the largest element from the linked list . ; Declare a max variable and initialize with INT_MIN ; Check loop while head not equal to NULL ; If max is less then head -> data then assign value of head -> data to max otherwise node points to next node . ; Function to create a hash table to check Fibonacci numbers ; Inserting the first two numbers in the hash ; Loop to add Fibonacci numbers upto the maximum element present in the linked list ; Function to find the required sum and product ; Find the largest node value in Singly Linked List ; Creating a set containing all the fibonacci numbers upto the maximum data value in the Singly Linked List ; Traverse the linked list ; If current node is fibonacci ; Find the sum and the product ; Driver code ; Create the linked list 15 -> 16 -> 8 -> 6 -> 13
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = ( Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int largestElement ( struct Node * head_ref ) { int max = INT_MIN ; Node * head = head_ref ; while ( head != NULL ) { if ( max < head -> data ) max = head -> data ; head = head -> next ; } return max ; } void createHash ( set < int > & hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . insert ( prev ) ; hash . insert ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . insert ( temp ) ; prev = curr ; curr = temp ; } } void sumAndProduct ( Node * head_ref ) { int maxEle = largestElement ( head_ref ) ; set < int > hash ; createHash ( hash , maxEle ) ; int prod = 1 ; int sum = 0 ; Node * ptr = head_ref ; while ( ptr != NULL ) { if ( hash . find ( ptr -> data ) != hash . end ( ) ) { prod *= ptr -> data ; sum += ptr -> data ; } ptr = ptr -> next ; } cout << " Sum ▁ = ▁ " << sum << endl ; cout << " Product ▁ = ▁ " << prod ; } int main ( ) { Node * head = NULL ; push ( & head , 13 ) ; push ( & head , 6 ) ; push ( & head , 8 ) ; push ( & head , 16 ) ; push ( & head , 15 ) ; sumAndProduct ( head ) ; return 0 ; }
Product of all Subarrays of an Array | C ++ program to find product of all subarray of an array ; Function to find product of all subarrays ; Variable to store the product ; Compute the product while traversing for subarrays ; Printing product of all subarray ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void product_subarrays ( int arr [ ] , int n ) { int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { for ( int k = i ; k <= j ; k ++ ) product *= arr [ k ] ; } } cout << product << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 10 , 3 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; product_subarrays ( arr , n ) ; return 0 ; }
Check if a N base number is Even or Odd | C ++ code to check if a Octal number is Even or Odd ; To return value of a char . ; Function to convert a number from N base to decimal ; power of base ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 1 ] * base + str [ len - 1 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Returns true if n is even , else odd ; Driver code
#include <iostream> NEW_LINE using namespace std ; int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } int toDeci ( string str , int base ) { int len = str . length ( ) ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base ) { cout << " Invalid ▁ Number " ; return -1 ; } num += val ( str [ i ] ) * power ; power = power * base ; } return num ; } bool isEven ( string num , int N ) { int deci = toDeci ( num , N ) ; return ( deci % 2 == 0 ) ; } int main ( ) { string num = "11A " ; int N = 16 ; if ( isEven ( num , N ) ) { cout << " Even " ; } else { cout << " Odd " ; } }
Find the next Factorial greater than N | C ++ implementation of the above approach ; Array that stores the factorial till 20 ; Function to pre - compute the factorial till 20 ; Precomputing factorials ; Function to return the next factorial number greater than N ; Traverse the factorial array ; Find the next just greater factorial than N ; Driver Code ; Function to precalculate the factorial till 20 ; Function call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; long long fact [ 21 ] ; void preCompute ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < 18 ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) ; } void nextFactorial ( int N ) { for ( int i = 0 ; i < 21 ; i ++ ) { if ( N < fact [ i ] ) { cout << fact [ i ] ; break ; } } } int main ( ) { preCompute ( ) ; int N = 120 ; nextFactorial ( N ) ; return 0 ; }
Find K distinct positive odd integers with sum N | C ++ implementation to find K odd positive integers such that their sum is equal to given number ; Function to find K odd positive integers such that their sum is N ; Condition to check if there are enough values to check ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE void findDistinctOddSum ( ll n , ll k ) { if ( ( k * k ) <= n && ( n + k ) % 2 == 0 ) { int val = 1 ; int sum = 0 ; for ( int i = 1 ; i < k ; i ++ ) { cout << val << " ▁ " ; sum += val ; val += 2 ; } cout << n - sum << endl ; } else cout << " NO ▁ STRNEWLINE " ; } int main ( ) { ll n = 100 ; ll k = 4 ; findDistinctOddSum ( n , k ) ; return 0 ; }