codeparrot/xlcost-text-to-code huggingface dataset:
Find the value of N when F ( N ) = f ( a ) + f ( b ) where a + b is the minimum possible and a * b = N | C ++ implementation of the approach ; Function to return the value of F ( N ) ; Base cases ; Count the number of times a number if divisible by 2 ; Return the summation ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getValueOfF ( int n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt += 1 ; n /= 2 ; } return 2 * cnt ; } int main ( ) { int n = 20 ; cout << getValueOfF ( n ) ; return 0 ; }
Find the sum of the number of divisors |  ; To store the number of divisors ; Function to find the number of divisors of all numbers in the range 1 to n ; For every number 1 to n ; Increase divisors count for every number ; Function to find the sum of divisors ; To store sum ; Count the divisors ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE #define mod  1000000007 NEW_LINE int cnt [ N ] ; void Divisors ( ) { memset ( cnt , 0 , sizeof cnt ) ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * i < N ; j ++ ) cnt [ i * j ] ++ ; } } int Sumofdivisors ( int A , int B , int C ) { int sum = 0 ; Divisors ( ) ; for ( int i = 1 ; i <= A ; i ++ ) { for ( int j = 1 ; j <= B ; j ++ ) { for ( int k = 1 ; k <= C ; k ++ ) { int x = i * j * k ; sum += cnt [ x ] ; if ( sum >= mod ) sum -= mod ; } } } return sum ; } int main ( ) { int A = 5 , B = 6 , C = 7 ; cout << Sumofdivisors ( A , B , C ) ; return 0 ; }
Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | C ++ implementation of the approach ; Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int A_mod_B ( string N , int a ) { int len = N . size ( ) ; int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) ans = ( ans * 10 + ( int ) N [ i ] - '0' ) % a ; return ans % a ; } int findMod ( string N ) { int mod = A_mod_B ( N , 4 ) ; int ans = ( 1 + pow ( 2 , mod ) + pow ( 3 , mod ) + pow ( 4 , mod ) ) ; return ( ans % 5 ) ; } int main ( ) { string N = "4" ; cout << findMod ( N ) ; return 0 ; }
Elements greater than the previous and next element in an Array | C ++ program to print elements greater than the previous and next element in an Array ; Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printElements ( int arr [ ] , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > arr [ i + 1 ] ) cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printElements ( arr , n ) ; return 0 ; }
Sum of the series Kn + ( K ( n | CPP program of above approach ; Recursive C program to compute modular power ; Base cases ; If B is even ; If B is odd ; Function to return sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int exponent ( int A , int B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long y ; if ( B % 2 == 0 ) { y = exponent ( A , B / 2 ) ; y = ( y * y ) ; } else { y = A ; y = ( y * exponent ( A , B - 1 ) ) ; } return y ; } int sum ( int k , int n ) { int sum = exponent ( k , n + 1 ) - exponent ( k - 1 , n + 1 ) ; return sum ; } int main ( ) { int n = 3 ; int K = 3 ; cout << sum ( K , n ) ; }
Minimize the cost to split a number | C ++ implementation of the approach ; check if a number is prime or not ; run a loop upto square root of x ; Function to return the minimized cost ; If n is prime ; If n is odd and can be split into ( prime + 2 ) then cost will be 1 + 1 = 2 ; Every non - prime even number can be expressed as the sum of two primes ; n is odd so n can be split into ( 3 + even ) further even part can be split into ( prime + prime ) ( 3 + prime + prime ) will cost 3 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int x ) { for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) return 0 ; } return 1 ; } int minimumCost ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 1 && isPrime ( n - 2 ) ) return 2 ; if ( n % 2 == 0 ) return 2 ; return 3 ; } int main ( ) { int n = 6 ; cout << minimumCost ( n ) ; return 0 ; }
Find amount of water wasted after filling the tank | C ++ program to find the volume of water wasted ; Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; Driver code
#include <iostream> NEW_LINE using namespace std ; double wastedWater ( double V , double M , double N ) { double wasted_amt , amt_per_min , time_to_fill ; amt_per_min = M - N ; time_to_fill = V / amt_per_min ; wasted_amt = N * time_to_fill ; return wasted_amt ; } int main ( ) { double V , M , N ; V = 700 ; M = 10 ; N = 3 ; cout << wastedWater ( V , M , N ) << endl ; V = 1000 ; M = 100 ; N = 50 ; cout << wastedWater ( V , M , N ) << endl ; return 0 ; }
Smallest and Largest N | C ++ implementation of the approach ; Function to print the largest and the smallest n - digit perfect cube ; Smallest n - digit perfect cube ; Largest n - digit perfect cube ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nDigitPerfectCubes ( int n ) { cout << pow ( ceil ( cbrt ( pow ( 10 , ( n - 1 ) ) ) ) , 3 ) << " ▁ " ; cout << ( int ) pow ( ceil ( cbrt ( pow ( 10 , ( n ) ) ) ) - 1 , 3 ) ; } int main ( ) { int n = 3 ; nDigitPerfectCubes ( n ) ; return 0 ; }
Count numbers which are divisible by all the numbers from 2 to 10 | C ++ implementation of the approach ; Function to return the count of numbers from 1 to n which are divisible by all the numbers from 2 to 10 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int n ) { return ( n / 2520 ) ; } int main ( ) { int n = 3000 ; cout << countNumbers ( n ) ; return 0 ; }
Sum of all odd factors of numbers in the range [ l , r ] | C ++ implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE const int MAX = 100001 ; ll prefix [ MAX ] ; void sieve_modified ( ) { for ( int i = 1 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( int i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } ll sumOddFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } int main ( ) { sieve_modified ( ) ; int l = 6 , r = 10 ; cout << sumOddFactors ( l , r ) ; return 0 ; }
Number of ways in which an item returns back to its initial position in N swaps in array of size K | C ++ program to find the number of ways in which an item returns back to its initial position in N swaps in an array of size K ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; 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 / 2 ; Function to return the number of ways ; Base case ; Recursive case F ( n ) = ( k - 1 ) ^ ( n - 1 ) - F ( n - 1 ) . ; Drivers code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod  1000000007 NEW_LINE long long power ( long x , long y ) { long p = mod ; long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } long long solve ( int n , int k ) { if ( n == 1 ) return 0LL ; return ( power ( ( k - 1 ) , n - 1 ) % mod - solve ( n - 1 , k ) + mod ) % mod ; } int main ( ) { int n = 4 , k = 5 ; cout << solve ( n , k ) ; return 0 ; }
XOR of a submatrix queries | C ++ implementation of the approach ; Function to pre - compute the xor ; Left to right prefix xor for each row ; Top to bottom prefix xor for each column ; Function to process the queries x1 , x2 , y1 , y2 represent the positions of the top - left and bottom right corners ; To store the xor values ; Finding the values we need to xor with value at ( x2 , y2 ) in prefix - xor matrix ; Return the required prefix xor ; Driver code ; To store pre - computed xor ; Pre - computing xor ; Queries
#include <iostream> NEW_LINE #define n  3 NEW_LINE using namespace std ; void preComputeXor ( int arr [ ] [ n ] , int prefix_xor [ ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( j == 0 ) prefix_xor [ i ] [ j ] = arr [ i ] [ j ] ; else prefix_xor [ i ] [ j ] = ( prefix_xor [ i ] [ j - 1 ] ^ arr [ i ] [ j ] ) ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) prefix_xor [ j ] [ i ] = ( prefix_xor [ j - 1 ] [ i ] ^ prefix_xor [ j ] [ i ] ) ; } int ansQuerie ( int prefix_xor [ ] [ n ] , int x1 , int y1 , int x2 , int y2 ) { int xor_1 = 0 , xor_2 = 0 , xor_3 = 0 ; if ( x1 != 0 ) xor_1 = prefix_xor [ x1 - 1 ] [ y2 ] ; if ( y1 != 0 ) xor_2 = prefix_xor [ x2 ] [ y1 - 1 ] ; if ( x1 != 0 and y1 != 0 ) xor_3 = prefix_xor [ x1 - 1 ] [ y1 - 1 ] ; return ( ( prefix_xor [ x2 ] [ y2 ] ^ xor_1 ) ^ ( xor_2 ^ xor_3 ) ) ; } int main ( ) { int arr [ ] [ n ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int prefix_xor [ n ] [ n ] ; preComputeXor ( arr , prefix_xor ) ; cout << ansQuerie ( prefix_xor , 1 , 1 , 2 , 2 ) << endl ; cout << ansQuerie ( prefix_xor , 1 , 2 , 2 , 2 ) << endl ; return 0 ; }
Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1 | C ++ implementation of the approach ; Function that returns true if the eventual digit sum of number nm is 1 ; if reminder will 1 then eventual sum is 1 ; Function to print the required numbers less than n ; If it is the required perfect cube ; Driver code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; bool isDigitSumOne ( int nm ) { if ( nm % 9 == 1 ) return true ; else return false ; } void printValidNums ( int n ) { int cbrt_n = ( int ) cbrt ( n ) ; for ( int i = 1 ; i <= cbrt_n ; i ++ ) { int cube = pow ( i , 3 ) ; if ( cube >= 1 && cube <= n && isDigitSumOne ( cube ) ) cout << cube << " ▁ " ; } } int main ( ) { int n = 1000 ; printValidNums ( n ) ; return 0 ; }
Count the number of rhombi possible inside a rectangle of given size | C ++ implementation of the approach ; Function to return the count of rhombi possible ; All possible diagonal lengths ; Update rhombi possible with the current diagonal lengths ; Return the total count of rhombi possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long countRhombi ( int h , int w ) { long long ct = 0 ; for ( int i = 2 ; i <= h ; i += 2 ) for ( int j = 2 ; j <= w ; j += 2 ) ct += ( h - i + 1 ) * ( w - j + 1 ) ; return ct ; } int main ( ) { int h = 2 , w = 2 ; cout << countRhombi ( h , w ) ; return 0 ; }
Program to calculate the area between two Concentric Circles | C ++ program to find area between the two given concentric circles ; Function to find area between the two given concentric circles ; Declare value of pi ; Calculate area of outer circle ; Calculate area of inner circle ; Difference in areas ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; double calculateArea ( int x , int y ) { double pi = 3.1415926536 ; double arx = pi * x * x ; double ary = pi * y * y ; return arx - ary ; } int main ( ) { int x = 2 ; int y = 1 ; cout << calculateArea ( x , y ) ; return 0 ; }
Find the winner by adding Pairwise difference of elements in the array until Possible | C ++ implementation of the approach ; Function to return the winner of the game ; To store the gcd of the original array ; To store the maximum element from the original array ; If number of moves are odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char getWinner ( int arr [ ] , int n ) { int gcd = arr [ 0 ] ; int maxEle = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { gcd = __gcd ( gcd , arr [ i ] ) ; maxEle = max ( maxEle , arr [ i ] ) ; } int totalMoves = ( maxEle / gcd ) - n ; if ( totalMoves % 2 == 1 ) return ' A ' ; return ' B ' ; } int main ( ) { int arr [ ] = { 5 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getWinner ( arr , n ) ; return 0 ; }
Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | CPP implementation of the approach ; Function to return the count of required pairs ; Number which will give the max value for ( ( n % i ) % j ) % n ; To store the maximum possible value of ( ( n % i ) % j ) % n ; To store the count of possible pairs ; Check all possible pairs ; Calculating the value of ( ( n % i ) % j ) % n ; If value is equal to maximum ; Return the number of possible pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int n ) { int num = ( ( n / 2 ) + 1 ) ; int max = n % num ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { int val = ( ( n % i ) % j ) % n ; if ( val == max ) count ++ ; } } return count ; } int main ( ) { int n = 5 ; cout << ( countPairs ( n ) ) ; }
Program to check if a number is divisible by any of its digits | C ++ implementation of above approach ; Function to check if given number is divisible by any of its digits ; check if any of digit divides n ; check if K divides N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isDivisible ( long long int n ) { long long int temp = n ; while ( n ) { int k = n % 10 ; if ( temp % k == 0 ) return " YES " ; n /= 10 ; } return " NO " ; } int main ( ) { long long int n = 9876543 ; cout << isDivisible ( n ) ; return 0 ; }
Program to find sum of harmonic series | C ++ program to find sum of harmonic series ; Function to return sum of harmonic series ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double sum ( int n ) { double i , s = 0.0 ; for ( i = 1 ; i <= n ; i ++ ) s = s + 1 / i ; return s ; } int main ( ) { int n = 5 ; cout << " Sum ▁ is ▁ " << sum ( n ) ; return 0 ; }
Check if a number can be expressed as sum two abundant numbers | CPP program to check if number n is expressed as sum of two abundant numbers ; Function to return all abundant numbers This function will be helpful for multiple queries ; To store abundant numbers ; to store sum of the divisors include 1 in the sum ; if j is proper divisor ; if i is not a perfect square ; if sum is greater than i then i is a abundant number ; Check if number n is expressed as sum of two abundant numbers ; if both i and n - i are abundant numbers ; can not be expressed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE set < int > ABUNDANT ( ) { set < int > v ; for ( int i = 1 ; i < N ; i ++ ) { int sum = 1 ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { sum += j ; if ( i / j != j ) sum += i / j ; } } if ( sum > i ) v . insert ( i ) ; } return v ; } void SumOfAbundant ( int n ) { set < int > v = ABUNDANT ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( v . count ( i ) and v . count ( n - i ) ) { cout << i << " ▁ " << n - i ; return ; } } cout << -1 ; } int main ( ) { int n = 24 ; SumOfAbundant ( n ) ; return 0 ; }
Find nth term of the series 5 2 13 41 | C ++ program to find nth term of the series 5 2 13 41 ; function to calculate nth term of the series ; to store the nth term of series ; if n is even number ; if n is odd number ; return nth term ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthTermOfTheSeries ( int n ) { int nthTerm ; if ( n % 2 == 0 ) nthTerm = pow ( n - 1 , 2 ) + n ; else nthTerm = pow ( n + 1 , 2 ) + n ; return nthTerm ; } int main ( ) { int n ; n = 8 ; cout << nthTermOfTheSeries ( n ) << endl ; n = 12 ; cout << nthTermOfTheSeries ( n ) << endl ; n = 102 ; cout << nthTermOfTheSeries ( n ) << endl ; n = 999 ; cout << nthTermOfTheSeries ( n ) << endl ; n = 9999 ; cout << nthTermOfTheSeries ( n ) << endl ; return 0 ; }
Find cost price from given selling price and profit or loss percentage | C ++ implementation to find Cost price ; Function to calculate cost price with profit ; required formula to calculate CP with profit ; Function to calculate cost price with loss ; required formula to calculate CP with loss ; Driver code
#include <iostream> NEW_LINE using namespace std ; float CPwithProfit ( int sellingPrice , int profit ) { float costPrice ; costPrice = ( sellingPrice * 100.0 ) / ( 100 + profit ) ; return costPrice ; } float CPwithLoss ( int sellingPrice , int loss ) { float costPrice ; costPrice = ( sellingPrice * 100.0 ) / ( 100 - loss ) ; return costPrice ; } int main ( ) { int SP , profit , loss ; SP = 1020 ; profit = 20 ; cout << " Cost ▁ Price ▁ = ▁ " << CPwithProfit ( SP , profit ) << endl ; SP = 900 ; loss = 10 ; cout << " Cost ▁ Price ▁ = ▁ " << CPwithLoss ( SP , loss ) << endl ; SP = 42039 ; profit = 8 ; cout << " Cost ▁ Price ▁ = ▁ " << CPwithProfit ( SP , profit ) << endl ; return 0 ; }
Check whether a number is Non | CPP program to check if a given number is Non - Hypotenuse number or not . ; Function to find prime factor and check if it is of the form 4 k + 1 or not ; 2 is a prime number but not of the form 4 k + 1 so , keep Dividing n by 2 until n is divisible by 2 ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; if i divides n check if i is of the form 4 k + 1 or not ; while i divides n divide n by i and update n ; This condition is to handle the case when n is a prime number greater than 2 ; Test function ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isNonHypotenuse ( int n ) { while ( n % 2 == 0 ) { n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { if ( ( i - 1 ) % 4 == 0 ) return false ; while ( n % i == 0 ) { n = n / i ; } } } if ( n > 2 && ( n - 1 ) % 4 == 0 ) return false ; else return true ; } void test ( int n ) { cout << " Testing ▁ for ▁ " << n << " ▁ : ▁ " ; if ( isNonHypotenuse ( n ) ) cout << " YES " << " STRNEWLINE " ; else cout << " NO " << " STRNEWLINE " ; } int main ( ) { int n = 11 ; test ( n ) ; n = 10 ; test ( n ) ; return 0 ; }
Find the n | C ++ implementation of the above approach ; Function to return the nth string in the required sequence ; Length of the resultant string ; Relative index ; Initial string of length len consists of all a 's since the list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE string obtain_str ( ll n ) { ll len = ( int ) log2 ( n + 1 ) ; ll rel_ind = n + 1 - pow ( 2 , len ) ; ll i = 0 ; string str = " " ; for ( i = 0 ; i < len ; i ++ ) { str += ' a ' ; } i = 0 ; while ( rel_ind > 0 ) { if ( rel_ind % 2 == 1 ) str [ i ] = ' b ' ; rel_ind /= 2 ; i ++ ; } reverse ( str . begin ( ) , str . end ( ) ) ; return str ; } int main ( ) { ll n = 11 ; cout << obtain_str ( n ) ; return 0 ; }
Program to find the Nth term of the series 0 , 3 / 1 , 8 / 3 , 15 / 5. . ... ... | C ++ implementation of the approach ; Function to return the nth term of the given series ; nth term ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Nthterm ( int n ) { int numerator = pow ( n , 2 ) - 1 ; int denomenator = 2 * n - 3 ; cout << numerator << " / " << denomenator ; } int main ( ) { int n = 3 ; Nthterm ( n ) ; return 0 ; }
Sum of each element raised to ( prime | C ++ implementation of the approach ; Function to return the required sum ; Driver code
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; int getSum ( vector < int > arr , int p ) { return arr . size ( ) ; } int main ( ) { vector < int > arr = { 5 , 6 , 8 } ; int p = 7 ; cout << getSum ( arr , p ) << endl ; return 0 ; }
Count numbers upto N which are both perfect square and perfect cube | C ++ implementation of the above approach ; Function to return required count ; Driver code ; function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SquareCube ( long long int N ) { int cnt = 0 , i = 1 ; while ( int ( pow ( i , 6 ) ) <= N ) { ++ cnt ; ++ i ; } return cnt ; } int main ( ) { long long int N = 100000 ; cout << SquareCube ( N ) ; return 0 ; }
Sum of integers upto N with given unit digit | C ++ implementation of the approach ; Function to return the required sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE ll getSum ( int n , int d ) { ll sum = 0 ; while ( d <= n ) { sum += d ; d += 10 ; } return sum ; } int main ( ) { int n = 30 , d = 3 ; cout << getSum ( n , d ) ; return 0 ; }
Summing the sum series | C ++ program to calculate the terms of summing of sum series ; Function to calculate twice of sum of first N natural numbers ; Function to calculate the terms of summing of sum series ; Driver Code
#include <iostream> NEW_LINE using namespace std ; # define MOD  1000000007 NEW_LINE long sum ( long N ) { long val = N * ( N + 1 ) ; val = val % MOD ; return val ; } int sumX ( int N , int M , int K ) { for ( int i = 0 ; i < M ; i ++ ) { N = ( int ) sum ( K + N ) ; } N = N % MOD ; return N ; } int main ( ) { int N = 1 , M = 2 , K = 3 ; cout << sumX ( N , M , K ) << endl ; return 0 ; }
Logarithm | C ++ program to find log ( n ) using Recursion ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int Log2n ( unsigned int n ) { return ( n > 1 ) ? 1 + Log2n ( n / 2 ) : 0 ; } int main ( ) { unsigned int n = 32 ; cout << Log2n ( n ) << " STRNEWLINE " ; return 0 ; }
Mode | C ++ Program for Mode using Counting Sort technique ; Function that sort input array a [ ] and calculate mode and median using counting sort . ; The output array b [ ] will have sorted array ; variable to store max of input array which will to have size of count array ; auxiliary ( count ) array to store count . Initialize count array as 0. Size of count array will be equal to ( max + 1 ) . ; Store count of each element of input array ; mode is the index with maximum count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMode ( int a [ ] , int n ) { int b [ n ] ; int max = * max_element ( a , a + n ) ; int t = max + 1 ; int count [ t ] ; for ( int i = 0 ; i < t ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; int mode = 0 ; int k = count [ 0 ] ; for ( int i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } cout << " mode ▁ = ▁ " << mode ; } int main ( ) { int a [ ] = { 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printMode ( a , n ) ; return 0 ; }
Harmonic Progression | CPP program to check if a given array can form harmonic progression ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . , first Sort the reciprocal array , then check difference between consecutive elements ; Driver Code ; series to check whether it is in H . P ; Checking a series is in H . P or not
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkIsHP ( vector < double > & arr ) { int n = arr . size ( ) ; if ( n == 1 ) { return true ; } vector < int > rec ; for ( int i = 0 ; i < n ; i ++ ) { rec . push_back ( ( 1 / arr [ i ] ) ) ; } sort ( rec . begin ( ) , rec . end ( ) ) ; int d = ( rec [ 1 ] ) - ( rec [ 0 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( rec [ i ] - rec [ i - 1 ] != d ) { return false ; } } return true ; } int main ( ) { vector < double > arr = { 1 / 5 , 1 / 10 , 1 / 15 , 1 / 20 , 1 / 25 } ; if ( checkIsHP ( arr ) ) { cout << " Yes " << std :: endl ; } else { cout << " No " << endl ; } return 0 ; }