text
stringlengths 17
4.49k
| code
stringlengths 49
5.46k
|
---|---|
Find the last digit when factorial of A divides factorial of B | CPP program to find last digit of a number obtained by dividing factorial of a number with factorial of another number . ; Function which computes the last digit of resultant of B ! / A ! ; if ( A == B ) If A = B , B ! = A ! and B ! / A ! = 1 ; If difference ( B - A ) >= 5 , answer = 0 ; If non of the conditions are true , we iterate from A + 1 to B and multiply them . We are only concerned for the last digit , thus we take modulus of 10 ; driver function | #include <iostream> NEW_LINE using namespace std ; int computeLastDigit ( long long int A , long long int B ) { int variable = 1 ; return 1 ; else if ( ( B - A ) >= 5 ) return 0 ; else { for ( long long int i = A + 1 ; i <= B ; i ++ ) variable = ( variable * ( i % 10 ) ) ; return variable % 10 ; } } int main ( ) { cout << computeLastDigit ( 2632 , 2634 ) ; return 0 ; } |
Program for sum of arithmetic series | CPP Program to find the sum of arithmetic series . ; Function to find sum of series . ; Driver function | #include <bits/stdc++.h> NEW_LINE using namespace std ; float sumOfAP ( float a , float d , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + a ; a = a + d ; } return sum ; } int main ( ) { int n = 20 ; float a = 2.5 , d = 1.5 ; cout << sumOfAP ( a , d , n ) ; return 0 ; } |
Product of factors of number | C ++ program to calculate product of factors of number ; function to product the factors ; If factors are equal , multiply only once ; Otherwise multiply both ; Driver code | #include <bits/stdc++.h> NEW_LINE #define M 1000000007 NEW_LINE using namespace std ; long long multiplyFactors ( int n ) { long long prod = 1 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) prod = ( prod * i ) % M ; else { prod = ( prod * i ) % M ; prod = ( prod * n / i ) % M ; } } } return prod ; } int main ( ) { int n = 12 ; cout << multiplyFactors ( n ) << endl ; return 0 ; } |
Product of factors of number | C ++ program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver code | #include <bits/stdc++.h> NEW_LINE #define M 1000000007 NEW_LINE using namespace std ; long long power ( long long x , long long y ) { long long res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % M ; y = ( y >> 1 ) % M ; x = ( x * x ) % M ; } return res ; } int countFactors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count ++ ; else count += 2 ; } } return count ; } long long multiplyFactors ( int n ) { int numFactor = countFactors ( n ) ; long long product = power ( n , numFactor / 2 ) ; if ( numFactor & 1 ) product = ( product * ( int ) sqrt ( n ) ) % M ; return product ; } int main ( ) { int n = 12 ; cout << multiplyFactors ( n ) << endl ; return 0 ; } |
Decimal representation of given binary string is divisible by 10 or not | C ++ implementation to check whether decimal representation of given binary number is divisible by 10 or not ; function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position from the right ; according to the digit 's position, obtain the last digit of the applicable perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; Driver program to test above | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisibleBy10 ( string bin ) { int n = bin . size ( ) ; if ( bin [ n - 1 ] == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } int main ( ) { string bin = "11000111001110" ; if ( isDivisibleBy10 ( bin ) ) cout << " Yes " ; else cout << " No " ; return 0 ; } |
Tribonacci Numbers | A space optimized based CPP program to print first n Tribonacci numbers . ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code | #include <iostream> NEW_LINE using namespace std ; void printTrib ( int n ) { if ( n < 1 ) return ; int first = 0 , second = 0 ; int third = 1 ; cout << first << " ▁ " ; if ( n > 1 ) cout << second << " ▁ " ; if ( n > 2 ) cout << second << " ▁ " ; for ( int i = 3 ; i < n ; i ++ ) { int curr = first + second + third ; first = second ; second = third ; third = curr ; cout << curr << " ▁ " ; } } int main ( ) { int n = 10 ; printTrib ( n ) ; return 0 ; } |
Prime Number of Set Bits in Binary Representation | Set 2 | C ++ code to find count of numbers having prime number of set bits in their binary representation in the range [ L , R ] ; Function to create an array of prime numbers upto number ' n ' ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as false . A value in prime [ i ] will finally be true if i is Not a prime , else false . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Append all the prime numbers to the list ; Utility function to count the number of set bits ; Driver code ; Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself . | #include <bits/stdc++.h> NEW_LINE using namespace std ; #include <cmath> NEW_LINE vector < int > SieveOfEratosthenes ( int n ) { bool prime [ n + 1 ] ; memset ( prime , false , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) for ( int i = p * 2 ; i < n + 1 ; i += p ) prime [ i ] = true ; } vector < int > lis ; for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] == false ) lis . push_back ( p ) ; return lis ; } int setBits ( int n ) { return __builtin_popcount ( n ) ; } int main ( ) { int x = 4 , y = 8 ; int count = 0 ; vector < int > primeArr = SieveOfEratosthenes ( ceil ( log2 ( y ) ) ) ; for ( int i = x ; i < y + 1 ; i ++ ) { int temp = setBits ( i ) ; for ( int j = 0 ; j < primeArr . size ( ) ; j ++ ) { if ( temp == primeArr [ j ] ) { count += 1 ; break ; } } } cout << count << endl ; return 0 ; } |
Count trailing zeroes present in binary representation of a given number using XOR | C ++ implementation of the above approach ; Function to print count of trailing zeroes present in binary representation of N ; Count set bits in ( N ^ ( N - 1 ) ) ; If res < 0 , return 0 ; Driver Code ; Function call to print the count of trailing zeroes in the binary representation of N | #include <bits/stdc++.h> NEW_LINE using namespace std ; int countTrailingZeroes ( int N ) { int res = log2 ( N ^ ( N - 1 ) ) ; return res >= 0 ? res : 0 ; } int main ( ) { int N = 12 ; cout << countTrailingZeroes ( N ) ; return 0 ; } |
Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | C ++ program for the above approach ; Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the final score ; Function to generate all the possible permutation and get the max score ; If arr [ ] length is equal to N process the permutation ; Generating the permutations ; If the current element is chosen ; Mark the current element as true ; Recursively call for next possible permutation ; Backtracking ; Return the ans ; Driver Code ; Stores the permutation ; To display the result | #include <bits/stdc++.h> NEW_LINE using namespace std ; int calcScr ( vector < int > arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) ans += ( i ^ arr [ i ] ) ; return ans ; } int getMax ( vector < int > arr , int ans , vector < bool > chosen , int N ) { if ( arr . size ( ) == N ) { ans = max ( ans , calcScr ( arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) continue ; chosen [ i ] = true ; arr . push_back ( i ) ; ans = getMax ( arr , ans , chosen , N ) ; chosen [ i ] = false ; arr . pop_back ( ) ; } return ans ; } int main ( ) { int N = 2 ; vector < int > arr ; int ans = -1 ; vector < bool > chosen ( N , false ) ; ans = getMax ( arr , ans , chosen , N ) ; cout << ans << endl ; } |
Additive Congruence method for generating Pseudo Random Numbers | C ++ implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the additive congruential method ; Driver Code ; seed value ; modulus parameter ; increment term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers | #include <bits/stdc++.h> NEW_LINE using namespace std ; void additiveCongruentialMethod ( int Xo , int m , int c , vector < int > & randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] + c ) % m ; } } int main ( ) { int Xo = 3 ; int m = 15 ; int c = 2 ; int noOfRandomNums = 20 ; vector < int > randomNums ( noOfRandomNums ) ; additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { cout << randomNums [ i ] << " ▁ " ; } return 0 ; } |
Number of ways to change the Array such that largest element is LCM of array | C ++ implementation to find the Number of ways to change the array such that maximum element of the array is the LCM of the array ; Modulo ; Fenwick tree to find number of indexes greater than x ; Function to compute x ^ y % MOD ; Loop to compute the x ^ y % MOD ; Function to update the binary indexed tree ; Function to find the prefix sum upto the current index ; Function to find the number of ways to change the array such that the LCM of array is maximum element of the array ; Updating BIT with the frequency of elements ; Maximum element in the array ; 1 is for every element is 1 in the array ; ; Vector for storing the factors ; finding factors of i ; Sorting in descending order ; for storing number of indexex greater than the i - 1 element ; Number of remaining factors ; Number of indexes in the array with element factor [ j ] and above ; Multiplying count with remFcators ^ ( indexes - prev ) ; Remove those counts which have lcm as i but i is not present ; Loop to find the count which have lcm as i but i is not present ; Adding cnt - toSubtract to answer ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 1e9 + 7 ; const int N = 1e5 + 5 ; vector < int > BIT ( N , 0 ) ; int power ( int x , int y ) { if ( x == 0 ) return 0 ; int ans = 1 ; while ( y > 0 ) { if ( y & 1 ) ans = ( 1LL * ans * x ) % MOD ; x = ( 1LL * x * x ) % MOD ; y >>= 1 ; } return ans ; } void updateBIT ( int idx , int val ) { assert ( idx > 0 ) ; while ( idx < N ) { BIT [ idx ] += val ; idx += idx & - idx ; } } int queryBIT ( int idx ) { int ans = 0 ; while ( idx > 0 ) { ans += BIT [ idx ] ; idx -= idx & - idx ; } return ans ; } int numWays ( int arr [ ] , int n ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { updateBIT ( arr [ i ] , 1 ) ; mx = max ( mx , arr [ i ] ) ; } int ans = 1 ; for ( int i = 2 ; i <= mx ; i ++ ) { vector < int > factors ; for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { factors . push_back ( j ) ; if ( i / j != j ) factors . push_back ( i / j ) ; } } sort ( factors . rbegin ( ) , factors . rend ( ) ) ; int cnt = 1 ; int prev = 0 ; for ( int j = 0 ; j < factors . size ( ) ; j ++ ) { int remFactors = int ( factors . size ( ) ) - j ; int indexes = n - queryBIT ( factors [ j ] - 1 ) ; cnt = ( 1LL * cnt * power ( remFactors , indexes - prev ) ) % MOD ; prev = max ( prev , indexes ) ; } factors . erase ( factors . begin ( ) ) ; int toSubtract = 1 ; prev = 0 ; for ( int j = 0 ; j < factors . size ( ) ; j ++ ) { int remFactors = int ( factors . size ( ) ) - j ; int indexes = n - queryBIT ( factors [ j ] - 1 ) ; toSubtract = ( 1LL * toSubtract * power ( remFactors , indexes - prev ) ) ; prev = max ( prev , indexes ) ; } ans = ( 1LL * ans + cnt - toSubtract + MOD ) % MOD ; } return ans ; } int main ( ) { int arr [ ] = { 6 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int ans = numWays ( arr , n ) ; cout << ans << endl ; return 0 ; } |
Second decagonal numbers | C ++ implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver Code | #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void findNthTerm ( int n ) { cout << n * ( 4 * n + 3 ) << endl ; } int main ( ) { int N = 4 ; findNthTerm ( N ) ; return 0 ; } |
65537 | C ++ implementation for above approach ; Function to find the nth 65537 - gon Number ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int gonNum65537 ( int n ) { return ( 65535 * n * n - 65533 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << gonNum65537 ( n ) ; return 0 ; } |
Hexacontatetragon numbers | C ++ implementation for above approach ; Function to Find the Nth Hexacontatetragon Number ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int HexacontatetragonNum ( int n ) { return ( 62 * n * n - 60 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << HexacontatetragonNum ( n ) ; return 0 ; } |
Icosikaipentagon Number | C ++ program to find the N - th Icosikaipentagon Number ; Function to find the N - th icosikaipentagon Number ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int icosikaipentagonNum ( int N ) { return ( 23 * N * N - 21 * N ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ icosikaipentagon ▁ Number ▁ is ▁ " << icosikaipentagonNum ( n ) ; return 0 ; } |
Chiliagon Number | C ++ program for above approach ; Finding the nth chiliagon Number ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int chiliagonNum ( int n ) { return ( 998 * n * n - 996 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ chiliagon ▁ Number ▁ is ▁ = ▁ " << chiliagonNum ( n ) ; return 0 ; } |
Pentacontagon number | C ++ program for above approach ; Finding the nth pentacontagon Number ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int pentacontagonNum ( int n ) { return ( 48 * n * n - 46 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ pentacontagon ▁ Number ▁ is ▁ = ▁ " << pentacontagonNum ( n ) ; return 0 ; } |
Number formed by adding product of its max and min digit K times | C ++ Code for the above approach ; function that returns the product of maximum and minimum digit of N number . ; finds the last digit . ; Moves to next digit ; Function to find the formed number ; K -- ; M ( 1 ) = N ; check if minimum digit is 0 ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int prod_of_max_min ( int n ) { int largest = 0 ; int smallest = 10 ; while ( n ) { int r = n % 10 ; largest = max ( r , largest ) ; smallest = min ( r , smallest ) ; n = n / 10 ; } return largest * smallest ; } int formed_no ( int N , int K ) { if ( K == 1 ) { return N ; } int answer = N ; while ( K -- ) { int a_current = prod_of_max_min ( answer ) ; if ( a_current == 0 ) break ; answer += a_current ; } return answer ; } int main ( ) { int N = 487 , K = 100000000 ; cout << formed_no ( N , K ) << endl ; return 0 ; } |
Logarithm tricks for Competitive Programming | C ++ implementation count the number of digits in a number ; Function to count the number of digits in a number ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int countDigit ( long long n ) { return floor ( log10 ( n ) + 1 ) ; } int main ( ) { double N = 80 ; cout << countDigit ( N ) ; return 0 ; } |
Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | C ++ implementation to find sum of series of 1 + x ^ 2 + x ^ 3 + ... . + x ^ n ; Function to find the sum of the series and print N terms of the given series ; First Term ; Loop to print the N terms of the series and find their sum ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; double sum ( int x , int n ) { double i , total = 1.0 , multi = x ; cout << total << " ▁ " ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; cout << multi << " ▁ " ; multi = multi * x ; } cout << " STRNEWLINE " ; return total ; } int main ( ) { int x = 2 ; int n = 5 ; cout << fixed << setprecision ( 2 ) << sum ( x , n ) ; return 0 ; } |
Find the remainder when N is divided by 4 using Bitwise AND operator | C ++ implementation to find N modulo 4 using Bitwise AND operator ; Function to find the remainder ; Bitwise AND with 3 ; Return x ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int findRemainder ( int n ) { int x = n & 3 ; return x ; } int main ( ) { int N = 43 ; int ans = findRemainder ( N ) ; cout << ans << endl ; return 0 ; } |
Find all Autobiographical Numbers with given number of digits | C ++ implementation to find Autobiographical numbers with length N ; Function to return if the number is autobiographical or not ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; Initialise count as 0 ; Check if it is equal to the index i if true then increment the count ; It is an Autobiographical number ; Return false if the count and the index number are not equal ; Function to print autobiographical number with given number of digits ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAutoBio ( int num ) { string autoStr ; int index , number , i , j , cnt ; autoStr = to_string ( num ) ; for ( int i = 0 ; i < autoStr . size ( ) ; i ++ ) { index = autoStr . at ( i ) - '0' ; cnt = 0 ; for ( j = 0 ; j < autoStr . size ( ) ; j ++ ) { number = autoStr . at ( j ) - '0' ; if ( number == i ) cnt ++ ; } if ( index != cnt ) return false ; } return true ; } void findAutoBios ( int n ) { int high , low , i , flag = 0 ; low = pow ( 10 , n - 1 ) ; high = pow ( 10 , n ) - 1 ; for ( i = low ; i <= high ; i ++ ) { if ( isAutoBio ( i ) ) { flag = 1 ; cout << i << " , ▁ " ; } } if ( ! flag ) cout << " There ▁ is ▁ no ▁ " << " Autobiographical ▁ number " << " ▁ with ▁ " << n << " ▁ digits STRNEWLINE " ; } int main ( ) { int N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; return 0 ; } |
Check whether the number can be made palindromic after adding K | C ++ program to check whether the number can be made palindrome number after adding K ; Function to check whether a number is a palindrome or not ; Convert num to string ; Comparing kth character from the beginning and N - kth character from the end . If all the characters match , then the number is a palindrome ; If all the above conditions satisfy , it means that the number is a palindrome ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void checkPalindrome ( int num ) { string str = to_string ( num ) ; int l = 0 , r = str . length ( ) - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { cout << " No " ; return ; } l ++ ; r -- ; } cout << " Yes " ; return ; } int main ( ) { int n = 19 , k = 3 ; checkPalindrome ( n + k ) ; return 0 ; } |
Count of subsets with sum equal to X using Recursion | C ++ program to print the count of subsets with sum equal to the given value X ; Recursive function to return the count of subsets with sum equal to the given value ; The recursion is stopped at N - th level where all the subsets of the given array have been checked ; Incrementing the count if sum is equal to 0 and returning the count ; Recursively calling the function for two cases Either the element can be counted in the subset If the element is counted , then the remaining sum to be checked is sum - the selected element If the element is not included , then the remaining sum to be checked is the total sum ; Driver code | #include <iostream> NEW_LINE using namespace std ; int subsetSum ( int arr [ ] , int n , int i , int sum , int count ) { if ( i == n ) { if ( sum == 0 ) { count ++ ; } return count ; } count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) ; count = subsetSum ( arr , n , i + 1 , sum , count ) ; return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int sum = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << subsetSum ( arr , n , 0 , sum , 0 ) ; } |
Distinct Prime Factors of an Array | cppimplementation of the above approach ; Function to return an array of prime numbers upto n using Sieve of Eratosthenes ; Function to return distinct prime factors from the given array ; Creating an empty array to store distinct prime factors ; Iterating through all the prime numbers and check if any of the prime numbers is a factor of the given input array ; Driver code ; Finding prime numbers upto 10000 using Sieve of Eratosthenes | #include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > sieve ( int n ) { vector < int > prime ( n + 1 , 0 ) ; int p = 2 ; while ( p * p <= n ) { if ( prime [ p ] == 0 ) { for ( int i = 2 * p ; i < n + 1 ; i += p ) prime [ i ] = 1 ; } p += 1 ; } vector < int > allPrimes ; for ( int i = 2 ; i < n ; i ++ ) if ( prime [ i ] == 0 ) allPrimes . push_back ( i ) ; return allPrimes ; } vector < int > distPrime ( vector < int > arr , vector < int > allPrimes ) { vector < int > list1 ; for ( int i : allPrimes ) { for ( int j : arr ) { if ( j % i == 0 ) { list1 . push_back ( i ) ; break ; } } } return list1 ; } int main ( ) { vector < int > allPrimes = sieve ( 10000 ) ; vector < int > arr = { 15 , 30 , 60 } ; vector < int > ans = distPrime ( arr , allPrimes ) ; cout << " [ " ; for ( int i : ans ) cout << i << " ▁ " ; cout << " ] " ; } |
Sum of the count of number of adjacent squares in an M X N grid | C ++ implementation of the above approach ; function to calculate the sum of all cells adjacent value ; Driver program to test above | #include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int m , int n ) { return 8 * m * n - 6 * m - 6 * n + 4 ; } int main ( ) { int m = 3 , n = 2 ; cout << sum ( m , n ) ; return 0 ; } |
Count pairs in an array such that the absolute difference between them is ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ à …¡¬ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ à …¾¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬¦¡¬°¥ K | C ++ implementation of the approach ; Function to return the count of required pairs ; Sort the given array ; To store the required count ; Update j such that it is always > i ; Find the first element arr [ j ] such that ( arr [ j ] - arr [ i ] ) >= K This is because after this element , all the elements will have absolute difference with arr [ i ] >= k and the count of valid pairs will be ( n - j ) ; Update the count of valid pairs ; Get to the next element to repeat the steps ; Return the count ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int cnt = 0 ; int i = 0 , j = 1 ; while ( i < n && j < n ) { j = ( j <= i ) ? ( i + 1 ) : j ; while ( j < n && ( arr [ j ] - arr [ i ] ) < k ) j ++ ; cnt += ( n - j ) ; i ++ ; } return cnt ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << count ( arr , n , k ) ; return 0 ; } |
Find the volume of rectangular right wedge | CPP program to find volume of rectangular right wedge ; function to return volume of rectangular right wedge ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; double volumeRec ( double a , double b , double e , double h ) { return ( ( ( b * h ) / 6 ) * ( 2 * a + e ) ) ; } int main ( ) { double a = 2 ; double b = 5 ; double e = 5 ; double h = 6 ; printf ( " Volume ▁ = ▁ % .1f " , volumeRec ( a , b , e , h ) ) ; return 0 ; } |
Count squares with odd side length in Chessboard | C ++ implementation of the approach ; Function to return the count of odd length squares possible ; To store the required count ; For all odd values of i ; Add the count of possible squares of length i ; Return the required count ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int count_square ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { int k = n - i + 1 ; count += ( k * k ) ; } return count ; } int main ( ) { int N = 8 ; cout << count_square ( N ) ; return 0 ; } |
Count of elements whose absolute difference with the sum of all the other elements is greater than k | C ++ implementation of the approach ; Function to return the number of anomalies ; To store the count of anomalies ; To store the sum of the array elements ; Find the sum of the array elements ; Count the anomalies ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; static int countAnomalies ( int arr [ ] , int n , int k ) { int cnt = 0 ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( abs ( arr [ i ] - ( sum - arr [ i ] ) ) > k ) cnt ++ ; return cnt ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 1 ; cout << countAnomalies ( arr , n , k ) ; } |
Find the number of integers x in range ( 1 , N ) for which x and x + 1 have same number of divisors | C ++ implementation of the approach ; To store number of divisors and Prefix sum of such numbers ; Function to find the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors ; Count the number of divisors ; Run a loop upto sqrt ( i ) ; If j is divisor of i ; If it is perfect square ; x and x + 1 have same number of positive divisors ; Driver code ; Function call ; Required answer | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define N 100005 NEW_LINE int d [ N ] , pre [ N ] ; void Positive_Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) d [ i ] ++ ; else d [ i ] += 2 ; } } } int ans = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( d [ i ] == d [ i - 1 ] ) ans ++ ; pre [ i ] = ans ; } } int main ( ) { Positive_Divisors ( ) ; int n = 15 ; cout << pre [ n ] << endl ; return 0 ; } |
Length of the smallest number which is divisible by K and formed by using 1 's only | C ++ implementation of the approach ; Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Generate all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's ; If number is divisible by k then return the length ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) return -1 ; int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = number * 10 + 1 ; if ( ( number % K == 0 ) ) return len ; } return -1 ; } int main ( ) { int K = 7 ; cout << numLen ( K ) ; return 0 ; } |
Find if the given number is present in the infinite sequence or not | C ++ implementation of the approach ; Function that returns true if the sequence will contain B ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool doesContainB ( int a , int b , int c ) { if ( a == b ) return true ; if ( ( b - a ) * c > 0 && ( b - a ) % c == 0 ) return true ; return false ; } int main ( ) { int a = 1 , b = 7 , c = 3 ; if ( doesContainB ( a , b , c ) ) cout << " Yes " ; else cout << " No " ; return 0 ; } |
Find a permutation of 2 N numbers such that the result of given expression is exactly 2 K | C ++ program to find the required permutation of first 2 * N natural numbers ; Function to find the required permutation of first 2 * N natural numbers ; Iterate in blocks of 2 ; We need more increments , so print in reverse order ; We have enough increments , so print in same order ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void printPermutation ( int n , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { int x = 2 * i - 1 ; int y = 2 * i ; if ( i <= k ) cout << y << " ▁ " << x << " ▁ " ; else cout << x << " ▁ " << y << " ▁ " ; } } int main ( ) { int n = 2 , k = 1 ; printPermutation ( n , k ) ; return 0 ; } |
Maximize the sum of products of the degrees between any two vertices of the tree | C ++ implementation of above approach ; Function to return the maximum possible sum ; Initialize degree for node u to 2 ; If u is the leaf node or the root node ; Initialize degree for node v to 2 ; If v is the leaf node or the root node ; Update the sum ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll long long int NEW_LINE ll maxSum ( int N ) { ll ans = 0 ; for ( int u = 1 ; u <= N ; u ++ ) { for ( int v = 1 ; v <= N ; v ++ ) { if ( u == v ) continue ; int degreeU = 2 ; if ( u == 1 u == N ) degreeU = 1 ; int degreeV = 2 ; if ( v == 1 v == N ) degreeV = 1 ; ans += ( degreeU * degreeV ) ; } } return ans ; } int main ( ) { int N = 6 ; cout << maxSum ( N ) ; } |
Find integers that divides maximum number of elements of the array | CPP implementation of the approach ; Function to print the integers that divide the maximum number of elements from the array ; Initialize two lists to store rank and factors ; Start from 2 till the maximum element in arr ; Initialize a variable to count the number of elements it is a factor of ; Maximum rank in the rank list ; Print all the elements with rank m ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumFactor ( vector < int > arr ) { int n = arr . size ( ) ; vector < int > rank ; vector < int > factors ; int max = * max_element ( arr . begin ( ) , arr . end ( ) ) ; for ( int i = 2 ; i <= max ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] % i == 0 ) count += 1 ; rank . push_back ( count ) ; factors . push_back ( i ) ; } } int m = * max_element ( rank . begin ( ) , rank . end ( ) ) ; for ( int i = 0 ; i < rank . size ( ) ; i ++ ) { if ( rank [ i ] == m ) cout << factors [ i ] << " ▁ " ; } } int main ( ) { vector < int > arr = { 120 , 15 , 24 , 63 , 18 } ; maximumFactor ( arr ) ; } |
Natural Numbers | CPP program to find sum of first n natural numbers . ; Returns sum of first n natural numbers ; Driver code | #include <iostream> NEW_LINE using namespace std ; int findSum ( int n ) { int sum = 0 ; for ( int x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } int main ( ) { int n = 5 ; cout << findSum ( n ) ; return 0 ; } |
Median | CPP program to find median ; Function for calculating median ; First we sort the array ; check for even case ; Driver program | #include <bits/stdc++.h> NEW_LINE using namespace std ; double findMedian ( int a [ ] , int n ) { sort ( a , a + n ) ; if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " Median ▁ = ▁ " << findMedian ( a , n ) << endl ; return 0 ; } |
Mean | CPP program to find mean ; Function for calculating mean ; Driver program | #include <bits/stdc++.h> NEW_LINE using namespace std ; double findMean ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " Mean ▁ = ▁ " << findMean ( a , n ) << endl ; return 0 ; } |
Check if the array has an element which is equal to product of remaining elements | C ++ implementation of the above approach ; Function to Check if the array has an element which is equal to product of all the remaining elements ; Storing frequency in map ; Calculate the product of all the elements ; If the prod is a perfect square ; then check if its square root exist in the array or not ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool CheckArray ( int arr [ ] , int n ) { int prod = 1 ; unordered_set < int > freq ; for ( int i = 0 ; i < n ; ++ i ) { freq . insert ( arr [ i ] ) ; prod *= arr [ i ] ; } int root = sqrt ( prod ) ; if ( root * root == prod ) if ( freq . find ( root ) != freq . end ( ) ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 12 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( CheckArray ( arr , n ) ) cout << " YES " ; else cout << " NO " ; return 0 ; } |
Find minimum area of rectangle with given set of coordinates | C ++ Implementation of above approach ; function to find minimum area of Rectangle ; creating empty columns ; fill columns with coordinates ; check if rectangle can be formed ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int minAreaRect ( vector < vector < int > > A ) { map < int , vector < int > > columns ; for ( auto i : A ) columns [ i [ 0 ] ] . push_back ( i [ 1 ] ) ; map < pair < int , int > , int > lastx ; int ans = INT_MAX ; for ( auto x : columns ) { vector < int > column = x . second ; sort ( column . begin ( ) , column . end ( ) ) ; for ( int j = 0 ; j < column . size ( ) ; j ++ ) { for ( int i = 0 ; i < j ; i ++ ) { int y1 = column [ i ] ; if ( lastx . find ( { y1 , column [ j ] } ) != lastx . end ( ) ) { ans = min ( ans , ( x . first - lastx [ { y1 , column [ j ] } ] ) * ( column [ j ] - column [ i ] ) ) ; } lastx [ { y1 , column [ j ] } ] = x . first ; } } } if ( ans < INT_MAX ) return ans ; else return 0 ; } int main ( ) { vector < vector < int > > A = { { 1 , 1 } , { 1 , 3 } , { 3 , 1 } , { 3 , 3 } , { 2 , 2 } } ; cout << ( minAreaRect ( A ) ) ; return 0 ; } |
Check whether a number has consecutive 0 's in the given base or not | C ++ implementation of the above approach ; Function to convert N into base K ; Weight of each digit ; Function to check for consecutive 0 ; Flag to check if there are consecutive zero or not ; If there are two consecutive zero then returning False ; We first convert to given base , then check if the converted number has two consecutive 0 s or not ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int toK ( int N , int K ) { int w = 1 ; int s = 0 ; while ( N != 0 ) { int r = N % K ; N = N / K ; s = r * w + s ; w *= 10 ; } return s ; } bool check ( int N ) { bool fl = false ; while ( N != 0 ) { int r = N % 10 ; N = N / 10 ; if ( fl == true and r == 0 ) return false ; if ( r > 0 ) { fl = false ; continue ; } fl = true ; } return true ; } void hasConsecutiveZeroes ( int N , int K ) { int z = toK ( N , K ) ; if ( check ( z ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int N = 15 ; int K = 8 ; hasConsecutiveZeroes ( N , K ) ; } |
Sum of every Kâ €™ th prime number in an array | C ++ implementation of the approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all the entries as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; sum of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; Driver code ; create the sieve | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX 1000000 NEW_LINE bool prime [ MAX + 1 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } void SumOfKthPrimes ( int arr [ ] , int n , int k ) { int c = 0 ; long long int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { sum += arr [ i ] ; c = 0 ; } } } cout << sum << endl ; } int main ( ) { SieveOfEratosthenes ( ) ; int arr [ ] = { 2 , 3 , 5 , 7 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; SumOfKthPrimes ( arr , n , k ) ; return 0 ; } |
Find the super power of a given Number | CPP for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; Driver program | #include <bits/stdc++.h> NEW_LINE #define MAX 100000 NEW_LINE using namespace std ; bool prime [ 100002 ] ; void SieveOfEratosthenes ( ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= MAX ; p ++ ) if ( prime [ p ] == true ) for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } int superpower ( int n ) { SieveOfEratosthenes ( ) ; int superPower = 0 , factor = 0 ; int i = 2 ; while ( n > 1 && i <= MAX ) { if ( prime [ i ] ) { factor = 0 ; while ( n % i == 0 && n > 1 ) { factor ++ ; n = n / i ; } if ( superPower < factor ) superPower = factor ; } i ++ ; } return superPower ; } int main ( ) { int n = 256 ; cout << superpower ( n ) ; return 0 ; } |
Count of Prime Nodes of a Singly Linked List | C ++ implementation to find count of prime numbers in the singly linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Function to check if a number is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find count of prime nodes in a linked list ; If current node is prime ; Update count ; Driver program ; start with the empty list ; create the linked list 15 -> 5 -> 6 -> 10 -> 17 ; Function call to print require answer | #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 = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } int countPrime ( Node * * head_ref ) { int count = 0 ; Node * ptr = * head_ref ; while ( ptr != NULL ) { if ( isPrime ( ptr -> data ) ) { count ++ ; } ptr = ptr -> next ; } return count ; } int main ( ) { Node * head = NULL ; push ( & head , 17 ) ; push ( & head , 10 ) ; push ( & head , 6 ) ; push ( & head , 5 ) ; push ( & head , 15 ) ; cout << " Count ▁ of ▁ prime ▁ nodes ▁ = ▁ " << countPrime ( & head ) ; return 0 ; } |
Smallest prime divisor of a number | C ++ program to count the number of subarrays that having 1 ; Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestDivisor ( int n ) { if ( n % 2 == 0 ) return 2 ; for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) return i ; } return n ; } int main ( ) { int n = 31 ; cout << smallestDivisor ( n ) ; return 0 ; } |
Count Number of animals in a zoo from given number of head and legs | C ++ implementation of above approach ; Function that calculates Rabbits ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int countRabbits ( int Heads , int Legs ) { int count = 0 ; count = ( Legs ) -2 * ( Heads ) ; count = count / 2 ; return count ; } int main ( ) { int Heads = 100 , Legs = 300 ; int Rabbits = countRabbits ( Heads , Legs ) ; cout << " Rabbits ▁ = ▁ " << Rabbits << endl ; cout << " Pigeons ▁ = ▁ " << Heads - Rabbits << endl ; return 0 ; } |
Program to evaluate the expression ( ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ à …¡¬ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ à …¾¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬¹ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ à …¡¬ ÃƒÆ ’ à ¢ â ‚¬¦¡¬ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬¦¡ X + 1 ) ^ 6 + ( ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ à …¡¬ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ à …¾¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬¹ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â € š ¬ à …¡¬ ÃƒÆ ’ à ¢ â ‚¬¦¡¬ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬ à ¢ à ¢ â € š ¬ à ¢ â € ž ¢ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ à ¢ à ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬¦¡ X | CPP program to evaluate the given expression ; Function to find the sum ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; float calculateSum ( float n ) { int a = int ( n ) ; return 2 * ( pow ( n , 6 ) + 15 * pow ( n , 4 ) + 15 * pow ( n , 2 ) + 1 ) ; } int main ( ) { float n = 1.4142 ; cout << ceil ( calculateSum ( n ) ) << endl ; return 0 ; } |
Find the sum of series 3 , | C ++ program to find sum upto N term of the series : 3 , - 6 , 12 , - 24 , ... . . ; calculate sum upto N term of series ; Driver code | #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; class gfg { public : int Sum_upto_nth_Term ( int n ) { return ( 1 - pow ( -2 , n ) ) ; } } ; int main ( ) { gfg g ; int N = 5 ; cout << g . Sum_upto_nth_Term ( N ) ; } |
Count numbers whose XOR with N is equal to OR with N | C ++ program to find the XOR equals OR count ; Function to calculate count of numbers with XOR equals OR ; variable to store count of unset bits ; Driver code | #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; class gfg { public : int xorEqualsOrCount ( int N ) { int count = 0 ; int bit ; while ( N > 0 ) { bit = N % 2 ; if ( bit == 0 ) count ++ ; N = N / 2 ; } return ( int ) pow ( 2 , count ) ; } } ; int main ( ) { gfg g ; int N = 7 ; cout << g . xorEqualsOrCount ( N ) ; return 0 ; } |
Program to find sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! | C ++ Program to compute sum of 1 + x / 2 ! + x ^ 2 / 3 ! + ... + x ^ n / ( n + 1 ) ! ; Method to find the factorial of a number ; Method to compute the sum ; Iterate the loop till n and compute the formula ; Driver code ; Get x and n ; Print output | #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int fact ( int n ) { if ( n == 1 ) return 1 ; return n * fact ( n - 1 ) ; } double sum ( int x , int n ) { double i , total = 1.0 ; for ( i = 1 ; i <= n ; i ++ ) { total = total + ( pow ( x , i ) / fact ( i + 1 ) ) ; } return total ; } int main ( ) { int x = 5 , n = 4 ; cout << " Sum ▁ is : ▁ " << sum ( x , n ) ; return 0 ; } |
Find Sum of Series 1 ^ 2 | C ++ program to find sum of series 1 ^ 2 - 2 ^ 2 + 3 ^ 3 - 4 ^ 4 + ... ; Function to find sum of series ; If i is even ; If i is odd ; return the result ; Driver Code ; Get n ; Find the sum ; Get n ; Find the sum | #include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_of_series ( int n ) { int result = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) result = result - pow ( i , 2 ) ; else result = result + pow ( i , 2 ) ; } return result ; } int main ( void ) { int n = 3 ; cout << sum_of_series ( n ) << endl ; n = 10 ; cout << sum_of_series ( n ) << endl ; } |
Program to find the sum of the series 23 + 45 + 75 + ... . . upto N terms | CPP program to find sum upto N - th term of the series : 23 , 45 , 75 , 113. . . ; calculate Nth term of series ; Driver Function ; Get the value of N ; Get the sum of the series | #include <iostream> NEW_LINE using namespace std ; int findSum ( int N ) { return ( 2 * N * ( N + 1 ) * ( 4 * N + 17 ) + 54 * N ) / 6 ; } int main ( ) { int N = 4 ; cout << findSum ( N ) << endl ; return 0 ; } |
Program to find the value of sin ( nÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬¦½ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬¹ ÃƒÆ ’ à ¢ â ‚¬¦ à ¢ à ¢ â € š ¬ à … â € œ ) | C ++ Program to find the value of sin ( n ? ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; find cosTheta from sinTheta ; store required answer ; use to toggle sign in sequence . ; Driver code . | #include <bits/stdc++.h> NEW_LINE #define ll long long int NEW_LINE #define MAX 16 NEW_LINE using namespace std ; ll nCr [ MAX ] [ MAX ] = { 0 } ; void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } double findCosNTheta ( double sinTheta , ll n ) { double cosTheta = sqrt ( 1 - sinTheta * sinTheta ) ; double ans = 0 ; ll toggle = 1 ; for ( int i = 1 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * pow ( cosTheta , n - i ) * pow ( sinTheta , i ) * toggle ; toggle = toggle * -1 ; } return ans ; } int main ( ) { binomial ( ) ; double sinTheta = 0.5 ; ll n = 10 ; cout << findCosNTheta ( sinTheta , n ) << endl ; return 0 ; } |
Program to find Nth term of series 9 , 23 , 45 , 75 , 113. . . | CPP program to find N - th term of the series : 9 , 23 , 45 , 75 , 113. . . ; calculate Nth term of series ; Driver Function ; Get the value of N ; Find the Nth term and print it | #include <iostream> NEW_LINE using namespace std ; int nthTerm ( int N ) { return ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ; } int main ( ) { int N = 4 ; cout << nthTerm ( N ) ; return 0 ; } |
Program to Find the value of cos ( nÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢ ÃƒÆ ’ à ¢ â ‚¬¦½ ÃƒÆ ’ à † â €™ à ¢ à ¢ â € š ¬¹ ÃƒÆ ’ à ¢ â ‚¬¦ à ¢ à ¢ â € š ¬ à … â € œ ) | CPP program to find the value of cos ( n - theta ) ; Function to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of cos ( n - theta ) ; find sinTheta from cosTheta ; to store required answer ; use to toggle sign in sequence . ; Driver code | #include <bits/stdc++.h> NEW_LINE #define MAX 16 NEW_LINE using namespace std ; int nCr [ MAX ] [ MAX ] = { 0 } ; void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } double findCosnTheta ( double cosTheta , int n ) { double sinTheta = sqrt ( 1 - cosTheta * cosTheta ) ; double ans = 0 ; int toggle = 1 ; for ( int i = 0 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * pow ( cosTheta , n - i ) * pow ( sinTheta , i ) * toggle ; toggle = toggle * -1 ; } return ans ; } int main ( ) { binomial ( ) ; double cosTheta = 0.5 ; int n = 10 ; cout << findCosnTheta ( cosTheta , n ) << endl ; return 0 ; } |
Find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | CPP program to find the sum of given series ; Function to calculate sum ; Return sum ; Driver code | #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int findSum ( int n ) { return ( pow ( 10 , n + 1 ) * ( 9 * n - 1 ) + 10 ) / pow ( 9 , 3 ) - n * ( n + 1 ) / 18 ; } int main ( ) { int n = 3 ; cout << findSum ( n ) ; return 0 ; } |
Find sum of the series 1 | C ++ program to find the sum of series 1 - 2 + 3 - 4 + ... ... ; Function to calculate sum ; when n is odd ; when n is not odd ; Driver code | #include <iostream> NEW_LINE using namespace std ; int solve_sum ( int n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } int main ( ) { int n = 8 ; cout << solve_sum ( n ) ; return 0 ; } |
Check if a number can be expressed as a ^ b | Set 2 | CPP program to check if a number can be expressed as a ^ b . ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = log ( a ) / log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } int main ( ) { int n = 16 ; cout << ( isPower ( n ) ? " Yes " : " No " ) ; return 0 ; } |
Program to calculate Root Mean Square | CPP program to calculate Root Mean Square ; Function that Calculate Root Mean Square ; Calculate square . ; Calculate Mean . ; Calculate Root . ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; float rmsValue ( int arr [ ] , int n ) { int square = 0 ; float mean = 0.0 , root = 0.0 ; for ( int i = 0 ; i < n ; i ++ ) { square += pow ( arr [ i ] , 2 ) ; } mean = ( square / ( float ) ( n ) ) ; root = sqrt ( mean ) ; return root ; } int main ( ) { int arr [ ] = { 10 , 4 , 6 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << rmsValue ( arr , n ) ; return 0 ; } |
Program to find the quantity after mixture replacement | C ++ implementation using above formula ; Function to calculate the Remaining amount . ; calculate Right hand Side ( RHS ) . ; calculate Amount left by multiply it with original value . ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; float Mixture ( int X , int Y , int Z ) { float result = 0.0 , result1 = 0.0 ; result1 = ( ( X - Y ) / ( float ) X ) ; result = pow ( result1 , Z ) ; result = result * X ; return result ; } int main ( ) { int X = 10 , Y = 2 , Z = 2 ; cout << Mixture ( X , Y , Z ) << " ▁ litres " ; return 0 ; } |
Sum of sum of all subsets of a set formed by first N natural numbers | C ++ program to find Sum of all subsets of a set formed by first N natural numbers | Set - 2 ; modulo value ; 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 the result ; y must be even now y = y >> 1 ; y = y / 2 ; function to find ff ( n ) ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver code ; function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod (int)(1e9 + 7) NEW_LINE int power ( int x , int y , int p ) { while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int check ( int n ) { n -- ; int ans = n * n ; if ( ans >= mod ) ans %= mod ; ans += n + 2 ; if ( ans >= mod ) ans %= mod ; ans = ( power ( 2 , n , mod ) % mod * ans % mod ) % mod ; ans = ( ans - 1 + mod ) % mod ; return ans ; } int main ( ) { int n = 4 ; cout << check ( n ) << endl ; return 0 ; } |
Program to find LCM of 2 numbers without using GCD | C ++ program to find LCM of 2 numbers without using GCD ; Function to return LCM of two numbers ; Driver program to test above function | #include <bits/stdc++.h> NEW_LINE using namespace std ; int findLCM ( int a , int b ) { int lar = max ( a , b ) ; int small = min ( a , b ) ; for ( int i = lar ; ; i += lar ) { if ( i % small == 0 ) return i ; } } int main ( ) { int a = 5 , b = 7 ; cout << " LCM ▁ of ▁ " << a << " ▁ and ▁ " << b << " ▁ is ▁ " << findLCM ( a , b ) ; return 0 ; } |
Smarandache | C ++ program to print the first ' n ' terms of the Smarandache - Wellin Sequence ; Function to collect first ' n ' prime numbers ; List to store first ' n ' primes ; Function to generate Smarandache - Wellin Sequence ; Storing the first ' n ' prime numbers in a list ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void primes ( int n ) { int i = 2 ; int j = 0 ; int result [ n ] ; int z = 0 ; while ( j < n ) { bool flag = true ; for ( int item = 2 ; item <= ( int ) ( i * 1 / 2 ) ; item ++ ) if ( i % item == 0 && i != item ) { flag = false ; break ; } if ( flag ) { result [ z ++ ] = i ; j += 1 ; } i += 1 ; } for ( i = 0 ; i < 5 ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) cout << result [ j ] ; cout << " ▁ " ; } } void smar_wln ( int n ) { primes ( n ) ; } int main ( ) { int n = 5 ; cout << " First ▁ " << n << " ▁ terms ▁ of ▁ the ▁ Sequence ▁ are " << endl ; smar_wln ( n ) ; } |
Pentatope number | CPP Program to find the nth Pentatope number ; function for Pentatope number ; formula for find Pentatope nth term ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Pentatope_number ( int n ) { return n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 24 ; } int main ( ) { int n = 7 ; cout << n << " th ▁ Pentatope ▁ number ▁ : " << Pentatope_number ( n ) << endl ; n = 12 ; cout << n << " th ▁ Pentatope ▁ number ▁ : " << Pentatope_number ( n ) << endl ; return 0 ; } |
Program for Centered Icosahedral Number | C ++ Program to find nth Centered icosahedral number ; Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int centeredIcosahedralNum ( int n ) { return ( 2 * n + 1 ) * ( 5 * n * n + 5 * n + 3 ) / 3 ; } int main ( ) { int n = 10 ; cout << centeredIcosahedralNum ( n ) << endl ; n = 12 ; cout << centeredIcosahedralNum ( n ) << endl ; return 0 ; } |
Centered Square Number | C ++ program to find nth Centered square number . ; Function to calculate Centered square number function ; Formula to calculate nth Centered square number ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int centered_square_num ( int n ) { return n * n + ( ( n - 1 ) * ( n - 1 ) ) ; } int main ( ) { int n = 7 ; cout << n << " th ▁ Centered ▁ square ▁ number : ▁ " ; cout << centered_square_num ( n ) ; return 0 ; } |
Sum of first n natural numbers | CPP program to find sum series 1 , 3 , 6 , 10 , 15 , 21. . . and then find its sum ; Function to find the sum of series ; Driver code | #include <iostream> NEW_LINE using namespace std ; int seriesSum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } int main ( ) { int n = 4 ; cout << seriesSum ( n ) ; return 0 ; } |
Dodecagonal number | CPP Program to find the nth Dodecagonal number ; function for Dodecagonal number ; formula for find Dodecagonal nth term ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Dodecagonal_number ( int n ) { return 5 * n * n - 4 * n ; } int main ( ) { int n = 7 ; cout << Dodecagonal_number ( n ) << endl ; n = 12 ; cout << Dodecagonal_number ( n ) << endl ; return 0 ; } |
Arithmetic Number | CPP program to check if a number is Arithmetic number or not ; Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; cnt = cnt + 1 ; incrementing power ; Calculating number of divisors If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; return ans ; Total divisors ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driven Program | #include <bits/stdc++.h> NEW_LINE using namespace std ; void SieveOfEratosthenes ( int n , bool prime [ ] , bool primesquare [ ] , int a [ ] ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int i = 0 ; i <= ( n * n + 1 ) ; i ++ ) primesquare [ i ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } int j = 0 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { a [ j ] = p ; primesquare [ p * p ] = true ; j ++ ; } } } int countDivisors ( int n ) { if ( n == 1 ) return 1 ; bool prime [ n + 1 ] , primesquare [ n * n + 1 ] ; SieveOfEratosthenes ( n , prime , primesquare , a ) ; int ans = 1 ; for ( int i = 0 ; ; i ++ ) { if ( a [ i ] * a [ i ] * a [ i ] > n ) break ; int cnt = 1 ; while ( n % a [ i ] == 0 ) { n = n / a [ i ] ; } ans = ans * cnt ; } if ( prime [ n ] ) ans = ans * 2 ; else if ( primesquare [ n ] ) ans = ans * 3 ; else if ( n != 1 ) ans = ans * 4 ; } int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } bool checkArithmetic ( int n ) { int count = countDivisors ( n ) ; int sum = sumofFactors ( n ) ; return ( sum % count == 0 ) ; } int main ( ) { int n = 6 ; ( checkArithmetic ( n ) ) ? ( cout << " Yes " ) : ( cout << " No " ) ; return 0 ; } |
Finding n | A formula based C ++ program to find sum of series with cubes of first n natural numbers ; Driver Function | #include <iostream> NEW_LINE using namespace std ; int magicOfSequence ( int N ) { return ( N * ( N + 1 ) / 2 ) + 2 * N ; } int main ( ) { int N = 6 ; cout << magicOfSequence ( N ) ; return 0 ; } |
Form a number using corner digits of powers | C ++ program to find number formed by corner digits of powers . ; Find next power by multiplying N with current power ; Store digits of Power one by one . ; Calculate carry . ; Store carry in Power array . ; Prints number formed by corner digits of powers of N . ; Storing N raised to power 0 ; Initializing empty result ; One by one compute next powers and add their corner digits . ; Call Function that store power in Power array . ; Store unit and last digits of power in res . ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void nextPower ( int N , vector < int > & power ) { int carry = 0 ; for ( int i = 0 ; i < power . size ( ) ; i ++ ) { int prod = ( power [ i ] * N ) + carry ; power [ i ] = prod % 10 ; carry = prod / 10 ; } while ( carry ) { power . push_back ( carry % 10 ) ; carry = carry / 10 ; } } void printPowerNumber ( int X , int N ) { vector < int > power ; power . push_back ( 1 ) ; vector < int > res ; for ( int i = 1 ; i <= X ; i ++ ) { nextPower ( N , power ) ; res . push_back ( power . back ( ) ) ; res . push_back ( power . front ( ) ) ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] ; } int main ( ) { int N = 19 , X = 4 ; printPowerNumber ( X , N ) ; return 0 ; } |
First digit in factorial of a number | A C ++ program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; loop for divide the fact until it become the single digit and return the fact ; derive main | #include <bits/stdc++.h> NEW_LINE using namespace std ; int firstDigit ( int n ) { long long int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact = fact * i ; while ( fact % 10 == 0 ) fact = fact / 10 ; } while ( fact >= 10 ) fact = fact / 10 ; return fact ; } int main ( ) { int n = 5 ; cout << firstDigit ( n ) ; return 0 ; } |
Sum of the series 1.2 . 3 + 2.3 . 4 + ... + n ( n + 1 ) ( n + 2 ) | CPP program to find sum of the series 1.2 . 3 + 2.3 . 4 + 3.4 . 5 + ... ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int sumofseries ( int n ) { int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) res += ( i ) * ( i + 1 ) * ( i + 2 ) ; return res ; } int main ( ) { cout << sumofseries ( 3 ) << endl ; return 0 ; } |
Find N Geometric Means between A and B | C ++ program to find n geometric means between A and B ; Prints N geometric means between A and B . ; calculate common ratio ( R ) ; for finding N the Geometric mean between A and B ; Driver code to test above | #include <bits/stdc++.h> NEW_LINE using namespace std ; void printGMeans ( int A , int B , int N ) { float R = ( float ) pow ( float ( B / A ) , 1.0 / ( float ) ( N + 1 ) ) ; for ( int i = 1 ; i <= N ; i ++ ) cout << A * pow ( R , i ) << " ▁ " ; } int main ( ) { int A = 3 , B = 81 , N = 2 ; printGMeans ( A , B , N ) ; return 0 ; } |
Numbers having difference with digit sum more than s | Program to find number of integer such that integer - digSum > s ; function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; driver program | #include <bits/stdc++.h> NEW_LINE using namespace std ; int digitSum ( long long int n ) { int digSum = 0 ; while ( n ) { digSum += n % 10 ; n /= 10 ; } return digSum ; } long long int countInteger ( long long int n , long long int s ) { if ( n < s ) return 0 ; for ( long long int i = s ; i <= min ( n , s + 163 ) ; i ++ ) if ( ( i - digitSum ( i ) ) > s ) return ( n - i + 1 ) ; return 0 ; } int main ( ) { long long int n = 1000 , s = 100 ; cout << countInteger ( n , s ) ; return 0 ; } |
Division without using ' / ' operator | CPP program to divide a number by other without using / operator ; Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver program | #include <bits/stdc++.h> NEW_LINE using namespace std ; int division ( int num1 , int num2 ) { if ( num1 == 0 ) return 0 ; if ( num2 == 0 ) return INT_MAX ; bool negResult = false ; if ( num1 < 0 ) { num1 = - num1 ; if ( num2 < 0 ) num2 = - num2 ; else negResult = true ; } else if ( num2 < 0 ) { num2 = - num2 ; negResult = true ; } int quotient = 0 ; while ( num1 >= num2 ) { num1 = num1 - num2 ; quotient ++ ; } if ( negResult ) quotient = - quotient ; return quotient ; } int main ( ) { int num1 = 13 , num2 = 2 ; cout << division ( num1 , num2 ) ; ; return 0 ; } |
Nonagonal number | CPP Program find first n nonagonal number . ; Function to find nonagonal number series . ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Nonagonal ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { cout << i * ( 7 * i - 5 ) / 2 ; cout << " ▁ " ; } } int main ( ) { int n = 10 ; Nonagonal ( n ) ; return 0 ; } |
Find n | Program to calculate nth term of a series ; func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int seriesFunc ( int n ) { int sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sumNatural = ( n * ( n + 1 ) / 2 ) ; return ( sumSquare + sumNatural + 1 ) ; } int main ( ) { int n = 8 ; cout << seriesFunc ( n ) << endl ; n = 13 ; cout << seriesFunc ( 13 ) ; return 0 ; } |
Program to check Plus Perfect Number | CPP implementation to check if the number is plus perfect or not ; function to check plus perfect number ; calculating number of digits ; calculating plus perfect number ; checking whether number is plus perfect or not ; driver program | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkplusperfect ( int x ) { int temp = x ; int n = 0 ; while ( x != 0 ) { x /= 10 ; n ++ ; } x = temp ; int sum = 0 ; while ( x != 0 ) { sum += pow ( x % 10 , n ) ; x /= 10 ; } return ( sum == temp ) ; } int main ( ) { int x = 9474 ; if ( checkplusperfect ( x ) ) cout << " Yes " ; else cout << " No " ; return 0 ; } |
Number of distinct subsets of a set | CPP program to count number of distinct subsets in an array of distinct numbers ; Returns 2 ^ n ; Driver program to test above function | #include <bits/stdc++.h> NEW_LINE using namespace std ; int subsetCount ( int arr [ ] , int n ) { return 1 << n ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << subsetCount ( A , n ) ; return 0 ; } |
Program to calculate GST from original and net prices | CPP Program to compute GST from original and net prices . ; return value after calculate GST % ; Driver program to test above functions | #include <iostream> NEW_LINE using namespace std ; float Calculate_GST ( float org_cost , float N_price ) { return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; } int main ( ) { float org_cost = 100 ; float N_price = 120 ; cout << " GST ▁ = ▁ " << Calculate_GST ( org_cost , N_price ) << " ▁ % ▁ " ; return 0 ; } |
Centered hexagonal number | Program to find nth centered hexadecimal number . ; Function to find centered hexadecimal number . ; Formula to calculate nth centered hexadecimal number and return it into main function . ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int centeredHexagonalNumber ( int n ) { return 3 * n * ( n - 1 ) + 1 ; } int main ( ) { int n = 10 ; cout << n << " th ▁ centered ▁ hexagonal ▁ number : ▁ " ; cout << centeredHexagonalNumber ( n ) ; return 0 ; } |
Find the distance covered to collect items at equal distances | C ++ program to calculate the distance for given problem ; function to calculate the distance ; Driver program | #include <bits/stdc++.h> NEW_LINE using namespace std ; int find_distance ( int n ) { return n * ( ( 3 * n ) + 7 ) ; } int main ( ) { int n = 5 ; cout << " Distance ▁ = ▁ " << find_distance ( n ) ; return 0 ; } |
Twin Prime Numbers | CPP program to check twin prime ; Please refer below post for details of this function https : goo . gl / Wv3fGv ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Returns true if n1 and n2 are twin primes ; Driver code | #include <iostream> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } bool twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && abs ( n1 - n2 ) == 2 ) ; } int main ( ) { int n1 = 11 , n2 = 13 ; if ( twinPrime ( n1 , n2 ) ) cout << " Twin ▁ Prime " << endl ; else cout << endl << " Not ▁ Twin ▁ Prime " << endl ; return 0 ; } |
Sum of the sequence 2 , 22 , 222 , ... ... ... | CPP program to find sum of series 2 , 22 , 222 , . . ; function which return the the sum of series ; driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; float sumOfSeries ( int n ) { return 0.0246 * ( pow ( 10 , n ) - 1 - ( 9 * n ) ) ; } int main ( ) { int n = 3 ; cout << sumOfSeries ( n ) ; return 0 ; } |
Find sum of even index binomial coefficients | CPP Program to find sum even indexed Binomial Coefficient . ; Returns value of even indexed Binomial Coefficient Sum which is 2 raised to power n - 1. ; Driver program to test above function | #include <bits/stdc++.h> NEW_LINE using namespace std ; int evenbinomialCoeffSum ( int n ) { return ( 1 << ( n - 1 ) ) ; } int main ( ) { int n = 4 ; printf ( " % d " , evenbinomialCoeffSum ( n ) ) ; return 0 ; } |
Sum of the series 1 + ( 1 + 3 ) + ( 1 + 3 + 5 ) + ( 1 + 3 + 5 + 7 ) + à ¢ â ‚¬¦ à ¢ â ‚¬¦ + ( 1 + 3 + 5 + 7 + à ¢ â ‚¬¦ + ( 2 n | C ++ implementation to find the sum of the given series ; functionn to find the sum of the given series ; required sum ; Driver program to test above | #include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfTheSeries ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } int main ( ) { int n = 5 ; cout << " Sum ▁ = ▁ " << sumOfTheSeries ( n ) ; return 0 ; } |
Sum of the series 1 + ( 1 + 2 ) + ( 1 + 2 + 3 ) + ( 1 + 2 + 3 + 4 ) + ... ... + ( 1 + 2 + 3 + 4 + ... + n ) | CPP program to find sum of given series ; Function to find sum of given series ; Driver Function | #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 += j ; return sum ; } int main ( ) { int n = 10 ; cout << sumOfSeries ( n ) ; return 0 ; } |
Number of triangles after N moves | C ++ program to calculate the number of equilateral triangles ; function to calculate number of triangles in Nth step ; driver program | #include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfTriangles ( int n ) { int ans = 2 * ( pow ( 3 , n ) ) - 1 ; return ans ; } int main ( ) { int n = 2 ; cout << numberOfTriangles ( n ) ; return 0 ; } |
Motzkin number | CPP Program to find Nth Motzkin Number . ; Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; Driven Program | #include <bits/stdc++.h> NEW_LINE using namespace std ; int motzkin ( int n ) { int dp [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = ( ( 2 * i + 1 ) * dp [ i - 1 ] + ( 3 * i - 3 ) * dp [ i - 2 ] ) / ( i + 2 ) ; return dp [ n ] ; } int main ( ) { int n = 8 ; cout << motzkin ( n ) << endl ; return 0 ; } |
NicomachusÃƒÆ ’ à † â €™ Ã⠀ à ¢ â ‚¬ â „¢¢ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¡¬ ÃƒÆ ’ à † â €™¢ ÃƒÆ ’¢ à ¢ à ¢ â ‚¬ Å ¡¬ Ã⠀¦¾¢ s Theorem ( Sum of k | Efficient CPP program to find sum of k - th group of positive odd integers . ; Return the sum of kth group of positive odd integer . ; Driven Program | #include <bits/stdc++.h> NEW_LINE using namespace std ; int kthgroupsum ( int k ) { return k * k * k ; } int main ( ) { int k = 3 ; cout << kthgroupsum ( k ) << endl ; return 0 ; } |
Find x , y , z that satisfy 2 / n = 1 / x + 1 / y + 1 / z | CPP program to find x y z that satisfies 2 / n = 1 / x + 1 / y + 1 / z ... ; function to find x y and z that satisfy given equation . ; driver program to test the above function | #include <bits/stdc++.h> NEW_LINE using namespace std ; void printXYZ ( int n ) { if ( n == 1 ) cout << -1 ; else cout << " x ▁ is ▁ " << n << " STRNEWLINE y ▁ is ▁ " << n + 1 << " STRNEWLINE z ▁ is ▁ " << n * ( n + 1 ) ; } int main ( ) { int n = 7 ; printXYZ ( n ) ; return 0 ; } |
Find n | CPP program to find the n - th term in series 1 3 6 10 . . . ; Function to find nth term ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int term ( int n ) { return n * ( n + 1 ) / 2 ; } int main ( ) { int n = 4 ; cout << term ( n ) ; return 0 ; } |
Find Harmonic mean using Arithmetic mean and Geometric mean | C ++ implementation of compution of arithmetic mean , geometric mean and harmonic mean ; Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver function | #include <bits/stdc++.h> NEW_LINE using namespace std ; double compute ( int a , int b ) { double AM , GM , HM ; AM = ( a + b ) / 2 ; GM = sqrt ( a * b ) ; HM = ( GM * GM ) / AM ; return HM ; } int main ( ) { int a = 5 , b = 15 ; double HM = compute ( a , b ) ; cout << " Harmonic ▁ Mean ▁ between ▁ " << a << " ▁ and ▁ " << b << " ▁ is ▁ " << HM ; return 0 ; } |
Find n | Program to find n - th element in the series 9 , 33 , 73 , 128. . ; Returns n - th element of the series ; driver program to test the above function | #include <bits/stdc++.h> NEW_LINE using namespace std ; int series ( int n ) { return ( 8 * n * n ) + 1 ; } int main ( ) { int n = 5 ; cout << series ( n ) ; return 0 ; } |
Check if a number is divisible by all prime divisors of another number | CPP program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisible ( int x , int y ) { if ( y == 1 ) return true ; if ( __gcd ( x , y ) == 1 ) return false ; return isDivisible ( x , y / gcd ) ; } int main ( ) { int x = 18 , y = 12 ; if ( isDivisible ( x , y ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; } |
Program to find Sum of a Series a ^ 1 / 1 ! + a ^ 2 / 2 ! + a ^ 3 / 3 ! + a ^ 4 / 4 ! + à ¢ â ‚¬¦ à ¢ â ‚¬¦ . + a ^ n / n ! | CPP program to print the sum of series ; function to calculate sum of given series ; multiply ( a / i ) to previous term ; store result in res ; Driver Function | #include <bits/stdc++.h> NEW_LINE using namespace std ; double sumOfSeries ( double a , double num ) { double res = 0 , prev = 1 ; for ( int i = 1 ; i <= num ; i ++ ) { prev *= ( a / i ) ; res = res + prev ; } return ( res ) ; } int main ( ) { double n = 5 , a = 2 ; cout << sumOfSeries ( a , n ) ; return 0 ; } |
Program for Celsius To Fahrenheit conversion | CPP program to convert Celsius scale to Fahrenheit scale ; function to convert Celsius scale to Fahrenheit scale ; driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; float Cel_To_Fah ( float n ) { return ( ( n * 9.0 / 5.0 ) + 32.0 ) ; } int main ( ) { float n = 20.0 ; cout << Cel_To_Fah ( n ) ; return 0 ; } |
Series with largest GCD and sum equals to n | CPP program to find the series with largest GCD and sum equals to n ; function to generate and print the sequence ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) d , ; computes the last element of the sequence n - s . ; prints the last element ; driver program to test the above function | #include <bits/stdc++.h> NEW_LINE using namespace std ; void print_sequence ( int n , int k ) { int b = n / ( k * ( k + 1 ) / 2 ) ; if ( b == 0 ) { cout << -1 << endl ; } else { int r = 1 ; for ( int x = 1 ; x * x <= n ; x ++ ) { if ( n % x != 0 ) continue ; if ( x <= b && x > r ) r = x ; if ( n / x <= b && n / x > r ) r = n / x ; } for ( int i = 1 ; i < k ; i ++ ) cout << r * i << " ▁ " ; int res = n - ( r * ( k * ( k - 1 ) / 2 ) ) ; cout << res << endl ; } } int main ( ) { int n = 24 ; int k = 4 ; print_sequence ( n , k ) ; n = 24 , k = 5 ; print_sequence ( n , k ) ; n = 6 , k = 4 ; print_sequence ( n , k ) ; } |
Subsets and Splits