codeparrot/xlcost-text-to-code huggingface dataset:
Maximum profit after buying and selling stocks with transaction fees | C ++ implementation of above approach ; b [ 0 ] will contain the maximum profit ; b [ 1 ] will contain the day on which we are getting the maximum profit ; here finding the max profit ; if we get less then or equal to zero it means we are not getting the profit ; check if sum is greater then maximum then store the new maximum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_profit ( int a [ ] , int b [ ] , int n , int fee ) { int i , j , profit ; int l , r , diff_day = 1 , sum = 0 ; b [ 0 ] = 0 ; b [ 1 ] = diff_day ; for ( i = 1 ; i < n ; i ++ ) { l = 0 ; r = diff_day ; sum = 0 ; for ( j = n - 1 ; j >= i ; j -- ) { profit = ( a [ r ] - a [ l ] ) - fee ; if ( profit > 0 ) { sum = sum + profit ; } l ++ ; r ++ ; } if ( b [ 0 ] < sum ) { b [ 0 ] = sum ; b [ 1 ] = diff_day ; } diff_day ++ ; } return 0 ; } int main ( ) { int arr [ ] = { 6 , 1 , 7 , 2 , 8 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int b [ 2 ] ; int tranFee = 2 ; max_profit ( arr , b , n , tranFee ) ; cout << b [ 0 ] << " , ▁ " << b [ 1 ] << endl ; return 0 ; }
Eggs dropping puzzle ( Binomial Coefficient and Binary Search Solution ) | C ++ program to find minimum number of trials in worst case . ; Find sum of binomial coefficients xCi ( where i varies from 1 to n ) . ; Do binary search to find minimum number of trials in worst case . ; Initialize low and high as 1 st and last floors ; Do binary search , for every mid , find sum of binomial coefficients and check if the sum is greater than k or not . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int x , int n , int k ) { int sum = 0 , term = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { term *= x - i + 1 ; term /= i ; sum += term ; if ( sum > k ) return sum ; } return sum ; } int minTrials ( int n , int k ) { int low = 1 , high = k ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( binomialCoeff ( mid , n , k ) < k ) low = mid + 1 ; else high = mid ; } return low ; } int main ( ) { cout << minTrials ( 2 , 10 ) ; return 0 ; }
Find next palindrome prime | CPP program to find next palindromic prime for a given number . ; if ( 8 <= N <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= N and it is a prime number then return it . ; Driver code
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool isPrime ( int num ) { if ( num < 2 num % 2 == 0 ) return num == 2 ; for ( int i = 3 ; i * i <= num ; i += 2 ) if ( num % i == 0 ) return false ; return true ; } int primePalindrome ( int N ) { if ( 8 <= N && N <= 11 ) return 11 ; for ( int x = 1 ; x < 100000 ; ++ x ) { string s = to_string ( x ) , r ( s . rbegin ( ) , s . rend ( ) ) ; int y = stoi ( s + r . substr ( 1 ) ) ; if ( y >= N && isPrime ( y ) ) return y ; } return -1 ; } int main ( ) { cout << primePalindrome ( 112 ) ; return 0 ; }
Number of integral solutions for equation x = b * ( sumofdigits ( x ) ^ a ) + c | C ++ program to find the numbers of values that satisfy the equation ; This function returns the sum of the digits of a number ; This function creates the array of valid numbers ; this computes s ( x ) ^ a ; this gives the result of equation ; checking if the sum same as i ; counter to keep track of numbers ; resultant array ; prints the number ; Driver Code ; calculate which value of x are possible
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getsum ( int a ) { int r = 0 , sum = 0 ; while ( a > 0 ) { r = a % 10 ; sum = sum + r ; a = a / 10 ; } return sum ; } void value ( int a , int b , int c ) { int co = 0 , p = 0 ; int no , r = 0 , x = 0 , q = 0 , w = 0 ; vector < int > v ; for ( int i = 1 ; i < 82 ; i ++ ) { no = pow ( ( double ) i , double ( a ) ) ; no = b * no + c ; if ( no > 0 && no < 1000000000 ) { x = getsum ( no ) ; if ( x == i ) { q ++ ; v . push_back ( no ) ; w ++ ; } } } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { cout << v [ i ] << " ▁ " ; } } int main ( ) { int a = 2 , b = 2 , c = -1 ; value ( a , b , c ) ; return 0 ; }
Cunningham chain | C ++ program for cunningham chain Function to print the series of second kind ; Function to print Cunningham chain of the second kind ; Iterate till all elements are printed ; check prime or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print ( int p0 ) { int p1 , i = 0 , x , flag , k ; while ( 1 ) { flag = 1 ; x = ( int ) ( pow ( 2 , i ) ) ; p1 = x * p0 - ( x - 1 ) ; for ( k = 2 ; k < p1 ; k ++ ) { if ( p1 % k == 0 ) { flag = 0 ; break ; } } if ( flag == 0 ) break ; printf ( " % d ▁ " , p1 ) ; i ++ ; } } int main ( ) { int p0 = 19 ; print ( p0 ) ; return 0 ; }
Count number of right triangles possible with a given perimeter | C ++ program to find the number of right triangles with given perimeter ; Function to return the count ; making a list to store ( a , b ) pairs ; no triangle if p is odd ; make ( a , b ) pair in sorted order ; check to avoid duplicates ; store the new pair ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriangles ( int p ) { vector < pair < int , int > > store ; if ( p % 2 != 0 ) return 0 ; else { int count = 1 ; for ( int b = 1 ; b < p / 2 ; b ++ ) { float a = ( float ) p / 2.0f * ( ( float ) ( ( float ) p - 2.0 * ( float ) b ) / ( ( float ) p - ( float ) b ) ) ; int inta = ( int ) ( a ) ; if ( a == inta ) { pair < int , int > ab ; if ( inta < b ) { ab = { inta , b } ; } else { ab = { b , inta } ; } if ( find ( store . begin ( ) , store . end ( ) , ab ) == store . end ( ) ) { count += 1 ; store . push_back ( ab ) ; } } } return count ; } } int main ( ) { int p = 840 ; cout << " number ▁ of ▁ right ▁ triangles ▁ = ▁ " << countTriangles ( p ) ; return 0 ; }
Count pairs with Bitwise AND as ODD number | C ++ program to count pairs with Odd AND ; Count total odd numbers in ; return count of even pair ; Driver main ; calling function findOddPair and print number of odd pair
#include <iostream> NEW_LINE using namespace std ; int findOddPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( ( A [ i ] % 2 == 1 ) ) count ++ ; return count * ( count - 1 ) / 2 ; } int main ( ) { int a [ ] = { 5 , 1 , 3 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findOddPair ( a , n ) << endl ; return 0 ; }
Surd number | CPP program to find if a number is Surds or not ; Returns true if x is Surd number ; Try all powers of i ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSurd ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { int j = i ; while ( j < n ) j = j * i ; if ( j == n ) return false ; } return true ; } int main ( ) { int n = 15 ; if ( isSurd ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Program to find last two digits of 2 ^ n | C ++ code to find last 2 digits of 2 ^ n ; Find the first digit ; Get the last digit from the number ; Remove last digit from number ; Get the last digit from the number ( last second of num ) ; Take last digit to ten 's position  i.e. last second digit ; Add the value of ones and tens to make it complete 2 digit number ; return the first digit ; Driver program ; pow function used
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LastTwoDigit ( long long int num ) { int one = num % 10 ; num /= 10 ; int tens = num % 10 ; tens *= 10 ; num = tens + one ; return num ; } int main ( ) { int n = 10 ; long long int num = 1 ; num = pow ( 2 , n ) ; cout << " Last ▁ " << 2 ; cout << " ▁ digits ▁ of ▁ " << 2 ; cout << " ^ " << n << " ▁ = ▁ " ; cout << LastTwoDigit ( num ) << endl ; return 0 ; }
Program to find last two digits of 2 ^ n | C ++ code to find last 2 digits of 2 ^ n ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; C ++ function to calculate number of digits in x ; C ++ function to print last 2 digits of 2 ^ n ; Generating 10 ^ 2 ; Calling modular exponentiation ; Printing leftmost zeros . Since ( 2 ^ n ) % 2 can have digits less then 2. In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver program to test above functions
#include <iostream> NEW_LINE using namespace std ; int power ( long long int x , long long int y , long long int p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int numberOfDigits ( int x ) { int i = 0 ; while ( x ) { x /= 10 ; i ++ ; } return i ; } void LastTwoDigit ( int n ) { cout << " Last ▁ " << 2 ; cout << " ▁ digits ▁ of ▁ " << 2 ; cout << " ^ " << n << " ▁ = ▁ " ; int temp = 1 ; for ( int i = 1 ; i <= 2 ; i ++ ) temp *= 10 ; temp = power ( 2 , n , temp ) ; for ( int i = 0 ; i < 2 - numberOfDigits ( temp ) ; i ++ ) cout << 0 ; if ( temp ) cout << temp ; } int main ( ) { int n = 72 ; LastTwoDigit ( n ) ; return 0 ; }
Find gcd ( a ^ n , c ) where a , n and c can vary from 1 to 10 ^ 9 | CPP program to find GCD of a ^ n and b . ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Finds GCD of a and b ; Finds GCD of a ^ n and c ; check if c is a divisor of a ; First compute ( a ^ n ) % c ; Now simply return GCD of modulo power and c . ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll modPower ( ll x , ll y , ll p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } ll gcd ( ll a , ll b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } ll gcdPow ( ll a , ll n , ll c ) { if ( a % c == 0 ) return c ; ll modexpo = modPower ( a , n , c ) ; return gcd ( modexpo , c ) ; } int main ( ) { ll a = 10248585 , n = 1000000 , c = 12564 ; cout << gcdPow ( a , n , c ) ; return 0 ; }
Number of sub arrays with odd sum | C ++ code to find count of sub - arrays with odd sum ; Find sum of all subarrays and increment result if sum is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOddSum ( int ar [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int val = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { val = val + ar [ j ] ; if ( val % 2 != 0 ) result ++ ; } } return ( result ) ; } int main ( ) { int ar [ ] = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = sizeof ( ar ) / sizeof ( ar [ 0 ] ) ; cout << " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ odd " " ▁ sum ▁ is ▁ " << countOddSum ( ar , n ) ; return ( 0 ) ; }
Number of sub arrays with odd sum | C ++ proggram to find count of sub - arrays with odd sum ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single odd element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ of ▁ arr [ 0 . . i ] ▁ ▁ under ▁ modulo ▁ 2 ▁ and ▁ increments ▁ even / odd ▁ count ▁ ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even / odd count ; An odd can be formed by even - odd pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOddSum ( int ar [ ] , int n ) { int temp [ 2 ] = { 1 , 0 } ; int result = 0 , val = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { val = ( ( val + ar [ i ] ) % 2 + 2 ) % 2 ; temp [ val ] ++ ; } result = ( temp [ 0 ] * temp [ 1 ] ) ; return ( result ) ; } int main ( ) { int ar [ ] = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = sizeof ( ar ) / sizeof ( ar [ 0 ] ) ; cout << " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ odd " " ▁ sum ▁ is ▁ " << countOddSum ( ar , n ) ; return ( 0 ) ; }
Program to print factors of a number in pairs | CPP program to print prime factors in pairs . ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printPFsInPairs ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 ) cout << i << " * " << n / i << endl ; } int main ( ) { int n = 24 ; printPFsInPairs ( n ) ; return 0 ; }
Sum of elements in range L | C ++ program to find the sum between L - R range by creating the array Naive Approach ; Function to find the sum between L and R ; array created ; fill the first half of array ; fill the second half of array ; find the sum between range ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int rangesum ( int n , int l , int r ) { int arr [ n ] ; int c = 1 , i = 0 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } c = 2 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } int sum = 0 ; for ( i = l - 1 ; i < r ; i ++ ) { sum += arr [ i ] ; } return sum ; } int main ( ) { int n = 12 ; int l = 1 , r = 11 ; cout << ( rangesum ( n , l , r ) ) ; }
Sum of elements in range L | C ++ program to find the sum between L - R range by creating the array Naive Approach ; Function to calculate the sum if n is even ; both l and r are to the left of mid ; first and last element ; Total number of terms in the sequence is r - l + 1 ; use of formula derived ; both l and r are to the right of mid ; first and last element ; Use of formula derived ; left is to the left of mid and right is to the right of mid ; Take two sums i . e left and right differently and add ; first and last element ; total terms ; no of terms ; The first even number is 2 ; The last element is given by 2 * ( r - n / 2 ) ; formula applied ; Function to calculate the sum if n is odd ; take ceil value if n is odd ; both l and r are to the left of mid ; first and last element ; number of terms ; formula ; both l and r are to the right of mid ; first and last term , ; no of terms ; formula used ; If l is on left and r on right ; calculate separate sums ; first half ; calculate terms ; second half ; add both halves ; Function to find the sum between L and R ; If n is even ; If n is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumeven ( int n , int l , int r ) { int sum = 0 ; int mid = n / 2 ; if ( r <= mid ) { int first = ( 2 * l - 1 ) ; int last = ( 2 * r - 1 ) ; int no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else if ( l >= mid ) { int first = ( 2 * ( l - n / 2 ) ) ; int last = ( 2 * ( r - n / 2 ) ) ; int no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else { int sumleft = 0 , sumright = 0 ; int first_term1 = ( 2 * l - 1 ) ; int last_term1 = ( 2 * ( n / 2 ) - 1 ) ; int no_of_terms1 = n / 2 - l + 1 ; sumleft = ( ( no_of_terms1 ) * ( ( first_term1 + last_term1 ) ) ) / 2 ; int first_term2 = 2 ; int last_term2 = ( 2 * ( r - n / 2 ) ) ; int no_of_terms2 = r - mid ; sumright = ( ( no_of_terms2 ) * ( ( first_term2 + last_term2 ) ) ) / 2 ; sum = ( sumleft + sumright ) ; } return sum ; } int sumodd ( int n , int l , int r ) { int mid = n / 2 + 1 ; int sum = 0 ; if ( r <= mid ) { int first = ( 2 * l - 1 ) ; int last = ( 2 * r - 1 ) ; int no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else if ( l > mid ) { int first = ( 2 * ( l - mid ) ) ; int last = ( 2 * ( r - mid ) ) ; int no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else { int sumleft = 0 , sumright = 0 ; int first_term1 = ( 2 * l - 1 ) ; int last_term1 = ( 2 * mid - 1 ) ; int no_of_terms1 = mid - l + 1 ; sumleft = ( ( no_of_terms1 ) * ( ( first_term1 + last_term1 ) ) ) / 2 ; int first_term2 = 2 ; int last_term2 = ( 2 * ( r - mid ) ) ; int no_of_terms2 = r - mid ; sumright = ( ( no_of_terms2 ) * ( ( first_term2 + last_term2 ) ) ) / 2 ; sum = ( sumleft + sumright ) ; } return sum ; } int rangesum ( int n , int l , int r ) { int sum = 0 ; if ( n % 2 == 0 ) return sumeven ( n , l , r ) ; else return sumodd ( n , l , r ) ; } int main ( ) { int n = 12 ; int l = 1 , r = 11 ; cout << ( rangesum ( n , l , r ) ) ; }
Program to find the Interior and Exterior Angle of a Regular Polygon | CPP program to find the interior and exterior angle of a given polygon ; function to find the interior and exterior angle ; formula to find the interior angle ; formula to find the exterior angle ; Displaying the output ; Driver code ; Function calling
#include <iostream> NEW_LINE using namespace std ; void findAngle ( int n ) { int interiorAngle , exteriorAngle ; interiorAngle = ( n - 2 ) * 180 / n ; exteriorAngle = 360 / n ; cout << " Interior ▁ angle : ▁ " << interiorAngle << endl ; cout << " Exterior ▁ angle : ▁ " << exteriorAngle ; } int main ( ) { int n = 10 ; findAngle ( n ) ; return 0 ; }
Program to calculate distance between two points in 3 D | C ++ program to find distance between two points in 3 D . ; function to print distance ; Driver Code ; function call for distance
#include <bits/stdc++.h> NEW_LINE #include <iomanip> NEW_LINE #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void distance ( float x1 , float y1 , float z1 , float x2 , float y2 , float z2 ) { float d = sqrt ( pow ( x2 - x1 , 2 ) + pow ( y2 - y1 , 2 ) + pow ( z2 - z1 , 2 ) * 1.0 ) ; std :: cout << std :: fixed ; std :: cout << std :: setprecision ( 2 ) ; cout << " ▁ Distance ▁ is ▁ " << d ; return ; } int main ( ) { float x1 = 2 ; float y1 = -5 ; float z1 = 7 ; float x2 = 3 ; float y2 = 4 ; float z2 = 5 ; distance ( x1 , y1 , z1 , x2 , y2 , z2 ) ; return 0 ; }
Check if the large number formed is divisible by 41 or not | C ++ program to check a large number divisible by 41 or not ; Check if a number is divisible by 41 or not ; array to store all the digits ; base values ; calculate remaining digits ; calculate answer ; check for divisibility ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool DivisibleBy41 ( int first , int second , int c , int n ) { int digit [ n ] ; digit [ 0 ] = first ; digit [ 1 ] = second ; for ( int i = 2 ; i < n ; i ++ ) digit [ i ] = ( digit [ i - 1 ] * c + digit [ i - 2 ] ) % 10 ; int ans = digit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) ans = ( ans * 10 + digit [ i ] ) % 41 ; if ( ans % 41 == 0 ) return true ; else return false ; } int main ( ) { int first = 1 , second = 2 , c = 1 , n = 3 ; if ( DivisibleBy41 ( first , second , c , n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Program to print pentatope numbers upto Nth term | C ++ program to generate Pentatope Number series ; Function to generate nth tetrahedral number ; Function to print pentatope number series upto nth term . ; Initialize prev as 0. It store the sum of all previously generated pentatope numbers ; Loop to print pentatope series ; Find ith tetrahedral number ; Add ith tetrahedral number to sum of all previously generated tetrahedral number to get ith pentatope number ; Update sum of all previously generated tetrahedral number ; Driver code ; Function call to print pentatope number series
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findTetrahedralNumber ( int n ) { return ( ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ) ; } void printSeries ( int n ) { int prev = 0 ; int curr ; for ( int i = 1 ; i <= n ; i ++ ) { curr = findTetrahedralNumber ( i ) ; curr = curr + prev ; cout << curr << " ▁ " ; prev = curr ; } } int main ( ) { int n = 10 ; printSeries ( n ) ; return 0 ; }
Program to print pentatope numbers upto Nth term | C ++ program to print Pentatope number series . ; Function to print pentatope series up to nth term ; Loop to print pentatope number series ; calculate and print ith pentatope number ; Driver code ; Function call to print pentatope number series
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int num = ( i * ( i + 1 ) * ( i + 2 ) * ( i + 3 ) / 24 ) ; cout << num << " ▁ " ; } } int main ( ) { int n = 10 ; printSeries ( n ) ; return 0 ; }
Program to print tetrahedral numbers upto Nth term | C ++ program to generate tetrahedral number series ; function to generate nth triangular number ; function to print tetrahedral number series up to n ; Initialize prev as 0. It stores the sum of all previously generated triangular number ; Loop to print series ; Find ith triangular number ; Add ith triangular number to sum of all previously generated triangular number to get ith tetrahedral number ; Update sum of all previously generated triangular number ; Driver code ; function call to print series
#include <bits/stdc++.h> NEW_LINE using namespace std ; long findTriangularNumber ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } void printSeries ( int n ) { int prev = 0 ; int curr ; for ( int i = 1 ; i <= n ; i ++ ) { curr = findTriangularNumber ( i ) ; curr = curr + prev ; cout << curr << " ▁ " ; prev = curr ; } } int main ( ) { int n = 10 ; printSeries ( n ) ; return 0 ; }
Program to print tetrahedral numbers upto Nth term | C ++ program to generate series of tetrahedral numbers ; function to print tetrahedral number series up to n ; loop to print series ; Calculate and print ith tetrahedral number ; Driver code ; function call to print series
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int num = i * ( i + 1 ) * ( i + 2 ) / 6 ; cout << num << " ▁ " ; } } int main ( ) { int n = 10 ; printSeries ( n ) ; return 0 ; }
Number of odd and even results for every value of x in range [ min , max ] after performing N steps | C ++ program to print Number of odd / even results for every value of x in range [ min , end ] after performing N steps ; Function that prints the number of odd and even results ; If constant at layer i is even , beven is true , otherwise false . If the coefficient of x at layer i is even , aeven is true , otherwise false . ; If any of the coefficients at any layer is found to be even , then the product of all the coefficients will always be even . ; Checking whether the constant added after all layers is even or odd . ; Assuming input x is even . ; Assuming input x is odd . ; Displaying the counts . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void count_even_odd ( int min , int max , int steps [ ] [ 2 ] ) { int a , b , even , odd ; bool beven = true , aeven = false ; int n = 2 ; for ( int i = 0 ; i < n ; i ++ ) { a = steps [ i ] [ 0 ] , b = steps [ i ] [ 1 ] ; if ( ! ( aeven a & 1 ) ) aeven = true ; if ( beven ) { if ( b & 1 ) beven = false ; } else if ( ! ( a & 1 ) ) { if ( ! ( b & 1 ) ) beven = true ; } else { if ( b & 1 ) beven = true ; } } if ( beven ) { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } else { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } if ( ! ( beven ^ aeven ) ) even += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; else odd += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; cout << " even ▁ = ▁ " << even << " , ▁ odd ▁ = ▁ " << odd << endl ; } int main ( ) { int min = 1 , max = 4 ; int steps [ ] [ 2 ] = { { 1 , 2 } , { 3 , 4 } } ; count_even_odd ( min , max , steps ) ; return 0 ; }
Maximum number of ones in a N * N matrix with given constraints | C ++ program to get Maximum Number of ones in a matrix with given constraints ; Function that returns the maximum number of ones ; Minimum number of zeroes ; Totol cells = square of the size of the matrices ; Initialising the answer ; Driver code ; Initialising the variables
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMaxOnes ( int n , int x ) { int zeroes = ( n / x ) ; zeroes = zeroes * zeroes ; int total = n * n ; int ans = total - zeroes ; return ans ; } int main ( ) { int n = 5 ; int x = 2 ; cout << getMaxOnes ( n , x ) ; return 0 ; }