codeparrot/xlcost-text-to-code huggingface dataset:
Sum of fourth powers of the first n natural numbers | CPP Program to find the sum of forth power of first n natural numbers ; Return the sum of forth power of first n natural numbers ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int fourthPowerSum ( int n ) { return ( ( 6 * n * n * n * n * n ) + ( 15 * n * n * n * n ) + ( 10 * n * n * n ) - n ) / 30 ; } int main ( ) { int n = 6 ; cout << fourthPowerSum ( n ) << endl ; return 0 ; }
Find unit digit of x raised to power y | C ++ code to find the unit digit of x raised to power y . ; find unit digit ; Get last digit of x ; Last cyclic modular value ; here we simply return the unit digit or the power of a number ; Driver code ; get unit digit number here we pass the unit digit of x and the last cyclicity number that is y % 4
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int unitnumber ( int x , int y ) { x = x % 10 ; if ( y != 0 ) y = y % 4 + 4 ; return ( ( ( int ) ( pow ( x , y ) ) ) % 10 ) ; } int main ( ) { int x = 133 , y = 5 ; cout << unitnumber ( x , y ) ; return 0 ; }
Aliquot sum | CPP program for aliquot sum ; Function to calculate sum of all proper divisors ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int aliquotSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( n % i == 0 ) sum += i ; return sum ; } int main ( ) { int n = 12 ; cout << aliquotSum ( n ) ; return 0 ; }
Average of Squares of Natural Numbers | C ++ program to get the Average of Square of first n natural numbers ; Function to get the average ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float AvgofSquareN ( int n ) { return ( float ) ( ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; } int main ( ) { int n = 10 ; cout << AvgofSquareN ( n ) ; return 0 ; }
Program to implement Simpson 's 3/8 rule | CPP program to implement Simpson 's rule ; Given function to be integrated ; Function to perform calculations ; Calculates value till integral limit ; Driver Code
#include <iostream> NEW_LINE using namespace std ; float func ( float x ) { return ( 1 / ( 1 + x * x ) ) ; } float calculate ( float lower_limit , float upper_limit , int interval_limit ) { float value ; float interval_size = ( upper_limit - lower_limit ) / interval_limit ; float sum = func ( lower_limit ) + func ( upper_limit ) ; for ( int i = 1 ; i < interval_limit ; i ++ ) { if ( i % 3 == 0 ) sum = sum + 2 * func ( lower_limit + i * interval_size ) ; else sum = sum + 3 * func ( lower_limit + i * interval_size ) ; } return ( 3 * interval_size / 8 ) * sum ; } int main ( ) { int interval_limit = 10 ; float lower_limit = 1 ; float upper_limit = 10 ; float integral_res = calculate ( lower_limit , upper_limit , interval_limit ) ; cout << integral_res ; return 0 ; }
Container with Most Water | C ++ code for Max Water Container ; Calculating the max area ; Driver code
#include <iostream> NEW_LINE using namespace std ; int maxArea ( int A [ ] , int len ) { int area = 0 ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { area = max ( area , min ( A [ j ] , A [ i ] ) * ( j - i ) ) ; } } return area ; } int main ( ) { int a [ ] = { 1 , 5 , 4 , 3 } ; int b [ ] = { 3 , 1 , 2 , 4 , 5 } ; int len1 = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maxArea ( a , len1 ) ; int len2 = sizeof ( b ) / sizeof ( b [ 0 ] ) ; cout << endl << maxArea ( b , len2 ) ; }
Program for focal length of a lens | C ++ program to determine the focal length of a lens ; Function to determine the focal length of a lens ; Driver function ; variable to store the distance between the lens and the image ; variable to store the distance between the lens and the object
#include <iostream> NEW_LINE using namespace std ; float focal_length ( float image_distance , float object_distance ) { return 1 / ( ( 1 / image_distance ) + ( 1 / object_distance ) ) ; } int main ( ) { float image_distance = 2 ; float object_distance = 50 ; cout << " Focal ▁ length ▁ of ▁ a ▁ lens ▁ is ▁ " << focal_length ( image_distance , object_distance ) << " ▁ units ▁ . " ; return 0 ; }
Count numbers in range L | C ++ program to Count numbers in range L - R that are divisible by all of its non - zero digits ; check if the number is divisible by the digits . ; function to calculate the number of numbers ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int n ) { int m = n ; while ( n ) { int r = n % 10 ; if ( r > 0 ) if ( ( m % r ) != 0 ) return false ; n /= 10 ; } return true ; } int count ( int l , int r ) { int ans = 0 ; for ( int i = l ; i <= r ; i ++ ) if ( check ( i ) ) ans += 1 ; return ans ; } int main ( ) { int l = 10 , r = 20 ; cout << count ( l , r ) ; return 0 ; }
Sum of the Series 1 / ( 1 * 2 ) + 1 / ( 2 * 3 ) + 1 / ( 3 * 4 ) + 1 / ( 4 * 5 ) + . . . . . | C ++ program to find the sum of given series ; function to find the sum of given series ; Computing sum term by term ; driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double sumOfTheSeries ( int n ) { double sum = 0.0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += 1.0 / ( i * ( i + 1 ) ) ; return sum ; } int main ( ) { int n = 10 ; cout << sumOfTheSeries ( n ) ; return 0 ; }
Sum of series ( n / 1 ) + ( n / 2 ) + ( n / 3 ) + ( n / 4 ) + ... ... . + ( n / n ) | CPP program to find sum of given series ; function to find sum of series ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int sum ( long long int n ) { long long int root = sqrt ( n ) ; long long int ans = 0 ; for ( int i = 1 ; i <= root ; i ++ ) ans += n / i ; ans = 2 * ans - ( root * root ) ; return ans ; } int main ( ) { long long int n = 35 ; cout << sum ( n ) ; return 0 ; }
Sum of the series 2 + ( 2 + 4 ) + ( 2 + 4 + 6 ) + ( 2 + 4 + 6 + 8 ) + â €¦ â €¦ + ( 2 + 4 + 6 + 8 + â €¦ . + 2 n ) | C ++ implementation to find the sum of the given series ; function to find the sum of the given series ; sum of 1 st n natural numbers ; sum of squares of 1 st n natural numbers ; required sum ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfTheSeries ( int n ) { int sum_n = ( n * ( n + 1 ) / 2 ) ; int sum_sq_n = ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; return ( sum_n + sum_sq_n ) ; } int main ( ) { int n = 5 ; cout << " Sum ▁ = ▁ " << sumOfTheSeries ( n ) ; return 0 ; }
Sum of squares of binomial coefficients | CPP Program to find the sum of square of binomial coefficient . ; Return the sum of square of binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Finding the sum of square of binomial coefficient . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofsquare ( int n ) { int C [ n + 1 ] [ n + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } int sum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) sum += ( C [ n ] [ i ] * C [ n ] [ i ] ) ; return sum ; } int main ( ) { int n = 4 ; cout << sumofsquare ( n ) << endl ; return 0 ; }
Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n | Program to find sum of series 1 + 2 + 2 + 3 + . . . + n ; Function that find sum of series . ; Driver function ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= i ; j ++ ) sum = sum + i ; return sum ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; }
Find sum of even index binomial coefficients | CPP Program to find sum of even index term ; Return the sum of even index term ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; finding sum of even index term . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int evenSum ( int n ) { int C [ n + 1 ] [ n + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } int sum = 0 ; for ( int i = 0 ; i <= n ; i += 2 ) sum += C [ n ] [ i ] ; return sum ; } int main ( ) { int n = 4 ; cout << evenSum ( n ) << endl ; return 0 ; }
Check if a number can be written as sum of three consecutive integers | CPP Program to check if a number can be written as sum of three consecutive integer . ; function to check if a number can be written as sum of three consecutive integers . ; if n is multiple of 3 ; else print " - 1" . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checksum ( int n ) { if ( n % 3 == 0 ) cout << n / 3 - 1 << " ▁ " << n / 3 << " ▁ " << n / 3 + 1 ; else cout << " - 1" ; } int main ( ) { int n = 6 ; checksum ( n ) ; return 0 ; }
Sum of all divisors from 1 to n | C ++ program to find sum of all divisor of number up to ' n ' ; Utility function to find sum of all divisor of number up to ' n ' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) sum += ( n / i ) * i ; return sum ; } int main ( ) { int n = 4 ; cout << " ▁ " << divisorSum ( n ) << endl ; n = 5 ; cout << " ▁ " << divisorSum ( n ) << endl ; return 0 ; }
Sum of all divisors from 1 to n |  ; long long t1 = i * ( num / i - i + 1 ) ; adding i every time it appears with numbers greater than or equal to itself long long t2 = ( ( ( num / i ) * ( num / i + 1 ) ) / 2 ) - ( ( i * ( i + 1 ) ) / 2 ) ; adding numbers that appear with i and are greater than i ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long sum_all_divisors ( long long num ) { long long sum = 0 ; for ( long long i = 1 ; i <= sqrt ( num ) ; i ++ ) { sum += t1 + t2 ; } return sum ; } int main ( ) { int n ; long long sum = sum_all_divisors ( n ) ; cout << sum << ' ' ; return 0 ; }
N | CPP program to find Nth polite number ; function to evaluate Nth polite number ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double polite ( double n ) { n += 1 ; double base = 2 ; return n + ( log ( ( n + ( log ( n ) / log ( base ) ) ) ) ) / log ( base ) ; } int main ( ) { double n = 7 ; cout << ( int ) polite ( n ) ; return 0 ; }
Find the number of stair steps | C ++ program to find the number of steps ; Modified Binary search function to solve the equation ; if mid is solution to equation ; if our solution to equation lies between mid and mid - 1 ; if solution to equation is greater than mid ; if solution to equation is less than mid ; driver function ; call binary search method to solve for limits 1 to T ; Because our pattern starts from 2 , 3 , 4 , 5. . . so , we subtract 1 from ans
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int low , int high , int T ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( ( mid * ( mid + 1 ) ) == T ) return mid ; if ( mid > 0 && ( mid * ( mid + 1 ) ) > T && ( mid * ( mid - 1 ) ) <= T ) return mid - 1 ; if ( ( mid * ( mid + 1 ) ) > T ) high = mid - 1 ; else low = mid + 1 ; } return -1 ; } int main ( ) { int T = 15 ; int ans = solve ( 1 , T , 2 * T ) ; if ( ans != -1 ) ans -- ; cout << " Number ▁ of ▁ stair ▁ steps ▁ = ▁ " << ans << endl ; return 0 ; }
Check for integer overflow on multiplication | CPP program to check for integer overflow on multiplication ; Function to check whether there is overflow in a * b or not . It returns true if there is overflow . ; Check if either of them is zero ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isOverflow ( long long a , long long b ) { if ( a == 0 b == 0 ) return false ; long long result = a * b ; if ( a == result / b ) return false ; else return true ; } int main ( ) { long long a = 10000000000 , b = -10000000000 ; if ( isOverflow ( a , b ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Sum of first n odd numbers in O ( 1 ) Complexity | A naive CPP program to find sum of first n odd numbers ; Returns the sum of first n odd numbers ; Driver function
#include <iostream> NEW_LINE using namespace std ; int oddSum ( int n ) { int sum = 0 , curr = 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += curr ; curr += 2 ; } return sum ; } int main ( ) { int n = 20 ; cout << " ▁ Sum ▁ of ▁ first ▁ " << n << " ▁ Odd ▁ Numbers ▁ is : ▁ " << oddSum ( n ) ; return 0 ; }
Sum of first n odd numbers in O ( 1 ) Complexity | Efficient program to find sum of first n odd numbers ; Returns the sum of first n odd numbers ; Driver function
#include <iostream> NEW_LINE using namespace std ; int oddSum ( int n ) { return ( n * n ) ; } int main ( ) { int n = 20 ; cout << " ▁ Sum ▁ of ▁ first ▁ " << n << " ▁ Odd ▁ Numbers ▁ is : ▁ " << oddSum ( n ) ; return 0 ; }
K | CPP program to count all those numbers in given range whose count of prime factors is k ; Returns the sum of first n odd numbers ; Count prime factors of all numbers till B . ; Print all numbers with k prime factors ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printKPFNums ( int A , int B , int K ) { bool prime [ B + 1 ] = { true } ; int p_factors [ B + 1 ] = { 0 } ; for ( int p = 2 ; p <= B ; p ++ ) if ( p_factors [ p ] == 0 ) for ( int i = p ; i <= B ; i += p ) p_factors [ i ] ++ ; for ( int i = A ; i <= B ; i ++ ) if ( p_factors [ i ] == K ) cout << i << " ▁ " ; } int main ( ) { int A = 14 , B = 18 , K = 2 ; printKPFNums ( A , B , K ) ; return 0 ; }
Queries for maximum difference between prime numbers in given ranges | CPP program to find maximum differences between two prime numbers in given ranges ; Precompute Sieve , Prefix array , Suffix array ; Sieve of Eratosthenes ; Precomputing Prefix array . ; Precompute Suffix array . ; Function to solve each query ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100005 NEW_LINE bool prime [ MAX ] ; int prefix [ MAX ] , suffix [ MAX ] ; void precompute ( int prefix [ ] , int suffix [ ] ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int i = 2 ; i * i < MAX ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j < MAX ; j += i ) prime [ j ] = false ; } } prefix [ 1 ] = 1 ; suffix [ MAX - 1 ] = 1e9 + 7 ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] ) prefix [ i ] = i ; else prefix [ i ] = prefix [ i - 1 ] ; } for ( int i = MAX - 1 ; i > 1 ; i -- ) { if ( prime [ i ] ) suffix [ i ] = i ; else suffix [ i ] = suffix [ i + 1 ] ; } } int query ( int prefix [ ] , int suffix [ ] , int L , int R ) { if ( prefix [ R ] < L suffix [ L ] > R ) return 0 ; else return prefix [ R ] - suffix [ L ] ; } int main ( ) { int q = 3 ; int L [ ] = { 2 , 2 , 24 } ; int R [ ] = { 5 , 2 , 28 } ; precompute ( prefix , suffix ) ; for ( int i = 0 ; i < q ; i ++ ) cout << query ( prefix , suffix , L [ i ] , R [ i ] ) << endl ; return 0 ; }
Sum of the Series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + . . + x ^ n / n | C ++ program to find sum of series 1 + x / 1 + x ^ 2 / 2 + x ^ 3 / 3 + ... . + x ^ n / n ; Funciton to print the sum of the series ; Driver code
#include <math.h> NEW_LINE #include <iostream> NEW_LINE #include <boost/format.hpp> NEW_LINE class gfg { public : double sum ( int x , int n ) { double i , total = 1.0 ; for ( i = 1 ; i <= n ; i ++ ) total = total + ( pow ( x , i ) / i ) ; return total ; } } ; int main ( ) { gfg g ; int x = 2 ; int n = 5 ; std :: cout << boost :: format ( " % .2f " ) % g . sum ( x , n ) ; return 0 ; }
Find if a number is part of AP whose first element and difference are given | C ++ program to check if x exist or not in the given AP . ; returns yes if exist else no . ; If difference is 0 , then x must be same as a . ; Else difference between x and a must be divisible by d . ; Driver code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isMember ( int a , int d , int x ) { if ( d == 0 ) return ( x == a ) ; return ( ( x - a ) % d == 0 && ( x - a ) / d >= 0 ) ; } int main ( ) { int a = 1 , x = 7 , d = 3 ; if ( isMember ( a , d , x ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Number of GP ( Geometric Progression ) subsequences of size 3 | C ++ program to count GP subsequences of size 3. ; to calculate nCr DP approach ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Returns count of G . P . subsequences with length 3 and common ratio r ; hashing to maintain left and right array elements to the main count ; stores the answer ; traverse through the elements ; IF RATIO IS ONE ; traverse the count in hash ; calculating nC3 , where ' n ' is the number of times each number is repeated in the input ; traverse through all elements and find out the number of elements as k1 * k2 ; keep the count of left and right elements left is a [ i ] / r and right a [ i ] * r ; if the current element is divisible by k , count elements in left hash . ; decrease the count in right hash ; number of right elements ; calculate the answer ; left count of a [ i ] ; returns answer ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ k + 1 ] ; memset ( C , 0 , sizeof ( C ) ) ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } long long subsequences ( int a [ ] , int n , int r ) { unordered_map < int , int > left , right ; long long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) right [ a [ i ] ] ++ ; if ( r == 1 ) { for ( auto i : right ) { ans += binomialCoeff ( i . second , 3 ) ; } return ans ; } for ( int i = 0 ; i < n ; i ++ ) { long long c1 = 0 , c2 = 0 ; if ( a [ i ] % r == 0 ) c1 = left [ a [ i ] / r ] ; right [ a [ i ] ] -- ; c2 = right [ a [ i ] * r ] ; ans += c1 * c2 ; left [ a [ i ] ] ++ ; } return ans ; } int main ( ) { int a [ ] = { 1 , 2 , 6 , 2 , 3 , 6 , 9 , 18 , 3 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int r = 3 ; cout << subsequences ( a , n , r ) ; return 0 ; }
Check whether a number can be represented by sum of two squares | A brute force approach based implementation to find if a number can be written as sum of two squares . ; function to check if there exist two numbers sum of whose squares is n . ; driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sumSquare ( int n ) { for ( long i = 1 ; i * i <= n ; i ++ ) for ( long j = 1 ; j * j <= n ; j ++ ) if ( i * i + j * j == n ) { cout << i << " ^ 2 ▁ + ▁ " << j << " ^ 2" << endl ; return true ; } return false ; } int main ( ) { int n = 25 ; if ( sumSquare ( n ) ) cout << " Yes " ; else cout << " No " ; }
Queries to count minimum flips required to fill a binary submatrix with 0 s only | C ++ program for the above approach ; Function to compute the matrix prefixCnt [ M ] [ N ] from mat [ M ] [ N ] such that prefixCnt [ i ] [ j ] stores the count of 0 's from (0, 0) to (i, j) ; Initialize prefixCnt [ i ] [ j ] with 1 if mat [ i ] [ j ] is 0 ; Otherwise , assign with 0 ; Calculate prefix sum for each row ; Calculate prefix sum for each column ; Function to compute count of 0 's in submatrix from (pi, pj) to (qi, qj) from prefixCnt[M][N] ; Initialize that count of 0 's  in the sub-matrix within  indices (0, 0) to (qi, qj) ; Subtract count of 0 's within  indices (0, 0) and (pi-1, qj) ; Subtract count of 0 's within  indices (0, 0) and (qi, pj-1) ; Add prefixCnt [ pi - 1 ] [ pj - 1 ] because its value has been added once but subtracted twice ; Function to count the 0 s in the each given submatrix ; Stores the prefix sum of each row and column ; Compute matrix prefixCnt [ ] [ ] ; Function Call for each query ; Driver Code ; Given matrix ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  6 NEW_LINE #define N  7 NEW_LINE void preCompute ( int mat [ M ] [ N ] , int prefixCnt [ M ] [ N ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] == 0 ) { prefixCnt [ i ] [ j ] = 1 ; } else { prefixCnt [ i ] [ j ] = 0 ; } } } for ( int i = 0 ; i < M ; i ++ ) for ( int j = 1 ; j < N ; j ++ ) prefixCnt [ i ] [ j ] += prefixCnt [ i ] [ j - 1 ] ; for ( int i = 1 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) prefixCnt [ i ] [ j ] += prefixCnt [ i - 1 ] [ j ] ; } int countQuery ( int prefixCnt [ M ] [ N ] , int pi , int pj , int qi , int qj ) { int cnt = prefixCnt [ qi ] [ qj ] ; if ( pi > 0 ) cnt -= prefixCnt [ pi - 1 ] [ qj ] ; if ( pj > 0 ) cnt -= prefixCnt [ qi ] [ pj - 1 ] ; if ( pi > 0 && pj > 0 ) cnt += prefixCnt [ pi - 1 ] [ pj - 1 ] ; return cnt ; } void count0s ( int mat [ M ] [ N ] , int Q [ ] [ 4 ] , int sizeQ ) { int prefixCnt [ M ] [ N ] ; preCompute ( mat , prefixCnt ) ; for ( int i = 0 ; i < sizeQ ; i ++ ) { cout << countQuery ( prefixCnt , Q [ i ] [ 0 ] , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , Q [ i ] [ 3 ] ) << ' ▁ ' ; } } int main ( ) { int mat [ M ] [ N ] = { { 0 , 1 , 0 , 1 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 , 1 , 0 , 1 } , { 1 , 1 , 0 , 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 1 , 1 , 0 , 1 } , { 0 , 0 , 1 , 0 , 1 , 1 , 1 } , { 1 , 1 , 0 , 1 , 1 , 0 , 1 } } ; int Q [ ] [ 4 ] = { { 0 , 1 , 3 , 2 } , { 2 , 2 , 4 , 5 } , { 4 , 3 , 5 , 6 } } ; int sizeQ = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; count0s ( mat , Q , sizeQ ) ; return 0 ; }
Smallest root of the equation x ^ 2 + s ( x ) * x | CPP program to find smallest value of root of an equation under given constraints . ; function to check if the sum of digits is equal to the summation assumed ; calculate the sum of digit ; function to find the largest root possible . ; iterate for all possible sum of digits . ; check if discriminent is a perfect square . ; check if discriminent is a perfect square and if it as perefect root of the equation ; function returns answer ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( long long a , long long b ) { long long int c = 0 ; while ( a != 0 ) { c = c + a % 10 ; a = a / 10 ; } return ( c == b ) ; } long long root ( long long n ) { bool found = 0 ; long long mx = 1e18 ; for ( long long i = 0 ; i <= 90 ; i ++ ) { long long s = i * i + 4 * n ; long long sq = sqrt ( s ) ; if ( sq * sq == s && check ( ( sq - i ) / 2 , i ) ) { found = 1 ; mx = min ( mx , ( sq - i ) / 2 ) ; } } if ( found ) return mx ; else return -1 ; } int main ( ) { long long n = 110 ; cout << root ( n ) ; }
Sum of digits of a given number to a given power | CPP program to illustrate the given problem ; Function to calculate sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate ( int n , int power ) { int sum = 0 ; int bp = ( int ) pow ( n , power ) ; while ( bp != 0 ) { int d = bp % 10 ; sum += d ; bp /= 10 ; } return sum ; } int main ( ) { int n = 5 ; int power = 4 ; cout << calculate ( n , power ) ; }
Co | CPP program to represent a number as sum of a co - prime pair such that difference between them is minimum ; function to check if pair is co - prime or not ; function to find and print co - prime pair ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool coprime ( int a , int b ) { return ( __gcd ( a , b ) == 1 ) ; } void pairSum ( int n ) { int mid = n / 2 ; for ( int i = mid ; i >= 1 ; i -- ) { if ( coprime ( i , n - i ) == 1 ) { cout << i << " ▁ " << n - i ; break ; } } } int main ( ) { int n = 11 ; pairSum ( n ) ; return 0 ; }