text
stringlengths
17
4.49k
code
stringlengths
49
5.46k
Place Value of a given digit in a number | C ++ implementation to find place value of a number ; Function to find place value ; Driver Code ; Digit , which we want to find place value . ; Number from where we want to find place value .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int placeValue ( int N , int num ) { int total = 1 , value = 0 , rem = 0 ; while ( true ) { rem = N % 10 ; N = N / 10 ; if ( rem == num ) { value = total * rem ; break ; } total = total * 10 ; } return value ; } int main ( ) { int D = 5 ; int N = 85932 ; cout << ( placeValue ( N , D ) ) ; }
Beatty sequence | C ++ implementation of the above approach ; Function to print the first N terms of the Beatty sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void BeattySequence ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { double ans = floor ( i * sqrt ( 2 ) ) ; cout << ans << " , ▁ " ; } } int main ( ) { int n = 5 ; BeattySequence ( n ) ; return 0 ; }
Primitive Abundant Number | C ++ implementation of the above approach ; Function to sum of divisors ; Note that this loop runs till square root of N ; If divisors are equal , take only one of them ; else Otherwise take both ; Function to check Abundant Number ; Return true if sum of divisors is greater than N . ; Function to check Deficient Number ; Check if sum ( n ) < 2 * n ; Function to check all proper divisors of N is deficient number or not ; if number itself is not abundant return false ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } bool checkAbundant ( int n ) { return ( getSum ( n ) - n > n ) ; } bool isDeficient ( int n ) { return ( getSum ( n ) < ( 2 * n ) ) ; } bool checkPrimitiveAbundant ( int num ) { if ( ! checkAbundant ( num ) ) { return false ; } for ( int i = 2 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 && i != num ) { if ( i * i == num ) { if ( ! isDeficient ( i ) ) { return false ; } } else if ( ! isDeficient ( i ) || ! isDeficient ( num / i ) ) { return false ; } } } return true ; } int main ( ) { int n = 20 ; if ( checkPrimitiveAbundant ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Sum of the first N Pronic Numbers | C ++ implementation to find sum of first N terms ; Function to calculate the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateSum ( int N ) { return N * ( N - 1 ) / 2 + N * ( N - 1 ) * ( 2 * N - 1 ) / 6 ; } int main ( ) { int N = 3 ; cout << calculateSum ( N ) ; return 0 ; }
Canada Numbers | C ++ implementation for the above approach ; Function to calculate sum of all trivial divisors of given natural number ; Final result of summation of trivial divisors ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Function to return sum of squares of digits of N ; Function to check if N is a Canada number ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int num ) { int result = 0 ; for ( int i = 1 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return ( result - 1 - num ) ; } int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r * r ; n = n / 10 ; } return sum ; } bool isCanada ( int n ) { return divSum ( n ) == getSum ( n ) ; } int main ( ) { int n = 125 ; if ( isCanada ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Self Numbers | C ++ implementation to check if the number is a self number or not ; Function to find the sum of digits of a number N ; Function to check for Self number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } bool isSelfNum ( int n ) { for ( int m = 1 ; m <= n ; m ++ ) { if ( m + getSum ( m ) == n ) return false ; } return true ; } int main ( ) { int n = 20 ; if ( isSelfNum ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Central binomial coefficient | C ++ implementation to find the Nth Central Binomial Coefficient ; Function to find the value of Nth Central Binomial Coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int main ( ) { int n = 3 ; int k = n ; n = 2 * n ; cout << binomialCoeff ( n , k ) ; }
Multiply perfect number | C ++ implementation of the above approach ; Function to find the sum of divisors ; Note that this loop runs till square root of N ; If divisors are equal , take only one of them ; Otherwise take both ; Function to check Multiply - perfect number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } bool MultiplyPerfectNumber ( int n ) { if ( getSum ( n ) % n == 0 ) return true ; else return false ; } int main ( ) { int n = 28 ; if ( MultiplyPerfectNumber ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Perfect totient number | C ++ implementation to find the number of digits in a Nth fibonacci number ; Function to find the Totient number of the given value ; Initialize result as n ; Consider all prime factors of n and subtract their multiples from result ; Check if p is a prime factor . ; If yes , then update N and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Function to check if the number is a perfect totient number ; store original value of n ; loop to calculate sum of iterated totients ; condition for Perfect Totient Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int phi ( int n ) { int result = n ; for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) n /= p ; result -= result / p ; } } if ( n > 1 ) result -= result / n ; return result ; } int isPerfectTotientNum ( int n ) { int temp = n ; int sum = 0 ; while ( n > 1 ) { sum = sum + phi ( n ) ; n = phi ( n ) ; } if ( sum == temp ) return true ; return false ; } int main ( ) { int n = 9 ; if ( isPerfectTotientNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Cunningham Numbers | C ++ implementation for the above approach ; Function to check if a number can be expressed as a ^ b . ; Function to check if N is a Cunningham number ; Driver Code ; Given Number ; Function Call
#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 ; } bool isCunningham ( int n ) { return isPower ( n - 1 ) || isPower ( n + 1 ) ; } int main ( ) { int n = 126 ; if ( isCunningham ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Product of all Subarrays of an Array | Set 2 | C ++ program for the above approach ; Function to find the product of elements of all subarray ; Initialize the result ; Computing the product of subarray using formula ; Return the product of all elements of each subarray ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long int SubArrayProdct ( int arr [ ] , int n ) { long int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result *= pow ( arr [ i ] , ( i + 1 ) * ( n - i ) ) ; return result ; } int main ( ) { int arr [ ] = { 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << SubArrayProdct ( arr , N ) << endl ; return 0 ; }
Number of ways to reach ( M , N ) in a matrix starting from the origin without visiting ( X , Y ) | C ++ program from the above approach ; Function for computing nCr ; Function to find factorial of a number ; Function for counting the number of ways to reach ( m , n ) without visiting ( x , y ) ; Driver Code ; Given Dimensions of Matrix ; Cell not to be visited ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) ; int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int countWays ( int m , int n , int x , int y ) { return nCr ( m + n , m ) - nCr ( x + y , x ) * nCr ( m + n - x - y , m - x ) ; } int main ( ) { int m = 5 ; int n = 4 ; int x = 3 ; int y = 2 ; cout << countWays ( m , n , x , y ) ; return 0 ; }
Smallest number greater than or equal to N using only digits 1 to K | C ++ Program to find the smallest number greater than or equal to N which is made up of first K digits ; Function to count the digits greater than K ; Function to print the list ; Function to find the number greater than or equal to n , which is only made of first k digits ; If the number itself satisfy the conditions ; Check digit from back ; If digit > K is present previously and current digit is less than K ; If current digit is greater than K ; If an extra digit needs to be added ; Print the number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountGreater ( int n , int k ) { int a = 0 ; while ( n ) { if ( ( n % 10 ) > k ) { a ++ ; } n = n / 10 ; } return a ; } int PrintList ( list < int > ans ) { for ( auto it = ans . begin ( ) ; it != ans . end ( ) ; it ++ ) cout << * it ; } void getNumber ( int n , int k ) { int count = CountGreater ( n , k ) ; if ( count == 0 ) { cout << n ; return ; } list < int > ans ; bool changed = false ; while ( n > 0 ) { int digit = n % 10 ; if ( changed == true ) { ans . push_front ( digit ) ; } else { if ( count == 0 && digit < k ) { ans . push_front ( digit + 1 ) ; changed = true ; } else { ans . push_front ( 1 ) ; if ( digit > k ) { count -- ; } } } n = n / 10 ; } if ( changed == false ) { ans . push_front ( 1 ) ; } PrintList ( ans ) ; return ; } int main ( ) { int N = 51234 ; int K = 4 ; getNumber ( N , K ) ; return 0 ; }
Find the Batting Average of a batsman | C ++ program to calculate the average of a batsman ; Function to find the average of a batsman ; Calculate number of dismissals ; check for 0 times out ; Calculate batting average ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; double averageRuns ( int runs , int matches , int notout ) { int out = matches - notout ; if ( out == 0 ) return -1 ; double avg = double ( runs ) / out ; return avg ; } int main ( ) { int runs = 10000 ; int matches = 250 ; int notout = 50 ; double avg = averageRuns ( runs , matches , notout ) ; if ( avg == -1 ) cout << " NA " ; else cout << avg ; return 0 ; }
Sum of series formed by difference between product and sum of N natural numbers | C ++ program to calculate the sum upto the Nth term of the given series ; Recursive Function to calculate the sum upto Nth term ; If N - th term is calculated ; Update multi to store product upto K ; Update add to store sum upto K ; Update prevSum to store sum upto K ; Proceed to next K ; Function to calculate and return the Sum upto Nth term ; Recursive Function ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int seriesSumUtil ( int k , int n , int prevSum , int multi , int add ) { if ( k == n + 1 ) { return prevSum ; } multi = multi * k ; add = add + k ; prevSum = prevSum + multi - add ; return seriesSumUtil ( k + 1 , n , prevSum , multi , add ) ; } int seriesSum ( int n ) { if ( n == 1 ) return 0 ; int prevSum = 0 ; int multi = 1 ; int add = 1 ; return seriesSumUtil ( 2 , n , prevSum , multi , add ) ; } int main ( ) { int N = 5 ; cout << seriesSum ( N ) << " ▁ " ; }
Count of total bits toggled / flipped in binary representation of 0 to N | C ++ program to count the number of toggles required to generate all numbers from 0 to N ; Function to count and print the required number of toggles ; Store the count of toggles ; Add the contribution of the current LSB ; Update N ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; void solve ( ll N ) { ll ans = 0 ; while ( N != 0 ) { ans += N ; N /= 2 ; } cout << ans << endl ; } int main ( ) { ll N = 5 ; solve ( N ) ; return 0 ; }
Maximum Bitwise AND pair ( X , Y ) from given range such that X and Y can be same | C ++ implementation to find the Maximum Bitwise AND pair ( X , Y ) from given range such that X and Y can be same ; Function to return the maximum bitwise AND ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumAND ( int L , int R ) { return R ; } int main ( ) { int l = 3 ; int r = 7 ; cout << maximumAND ( l , r ) ; return 0 ; }
Check if K distinct array elements form an odd sum | C ++ program for above approach ; Function to return if odd sum is possible or not ; Stores distinct odd elements ; Stores distinct even elements ; Iterating through given array ; If element is even ; If element is odd ; If atleast K elements in the array are odd ; Check for all odd frequencies of odd elements whether sufficient even numbers are present or not ; Count of even numbers required ; If required even numbers are present in the array ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool oddSum ( vector < int > & A , int N , int K ) { set < int > Odd ; set < int > Even ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { Even . insert ( A [ i ] ) ; } else { Odd . insert ( A [ i ] ) ; } } if ( Odd . size ( ) >= K ) return true ; bool flag = false ; for ( int i = 1 ; i < K ; i += 2 ) { int needed = K - i ; if ( needed <= Even . size ( ) ) { return true ; } } return flag ; } int main ( ) { int K = 5 ; vector < int > A = { 12 , 1 , 7 , 7 , 26 , 18 } ; int N = 3 ; if ( oddSum ( A , N , K ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Count of elements not divisible by any other elements of Array | C ++ program for the above approach ; Function to count the number of elements of array which are not divisible by any other element of same array ; Length for boolean array ; Hash map for storing the element and it 's frequency ; Update the maximum element ; Boolean array of size of the max element + 1 ; Marking the multiples as false ; To store the final count ; Traverse boolean array ; Check if i is not divisible by any other array elements and appears in the array only once ; Return the final Count ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countEle ( int a [ ] , int n ) { int len = 0 ; unordered_map < int , int > hmap ; for ( int i = 0 ; i < n ; i ++ ) { len = max ( len , a [ i ] ) ; hmap [ a [ i ] ] ++ ; } bool v [ len + 1 ] ; for ( int i = 0 ; i <= len ; i ++ ) { v [ i ] = true ; } for ( int i = 0 ; i < n ; i ++ ) { if ( v [ a [ i ] ] == false ) continue ; for ( int j = 2 * a [ i ] ; j <= len ; j += a [ i ] ) { v [ j ] = false ; } } int count = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { if ( v [ i ] == true && hmap . count ( i ) == 1 && hmap [ i ] == 1 ) { count += 1 ; } } return count ; } int main ( ) { int arr [ ] = { 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << countEle ( arr , n ) ; return 0 ; }
Smallest N digit number divisible by N | C ++ program for the above approach ; Function to find the smallest N - digit number divisible by N ; Find largest n digit number ; Find smallest n digit number ; If i is divisible by N , then print i and return ; ; Driver Code ; Given Number ; Function Call
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void smallestNumber ( int N ) { int L = pow ( 10 , N ) - 1 ; int S = pow ( 10 , N - 1 ) ; for ( int i = S ; i <= L ; i ++ ) { if ( i % N == 0 ) { cout << i ; return ; } } } int main ( ) { int N = 2 ; smallestNumber ( N ) ; return 0 ; }
Number less than equals to N with maximum product of prime factors | C ++ program for the above approach ; Function to find the smallest number having a maximum product of prime factors ; Declare the array arr [ ] ; Initialise array with 1 ; Iterate from [ 2 , N ] ; If value at index i is 1 , then i is prime and make update array at index for all multiples of i ; Initialise maxValue ; Find number having maximum product of prime factor <= N ; Find the maximum value ; Driven Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPrimefactorNum ( int N ) { int arr [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) arr [ i ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] == 1 ) { for ( int j = i ; j <= N ; j += i ) { arr [ j ] *= i ; } } } int maxValue = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] > maxValue ) { maxValue = i ; } } return maxValue ; } int main ( ) { int N = 20 ; cout << maxPrimefactorNum ( N ) ; return 0 ; }
Count of elements to be multiplied with integers to make each pair of Array a perfect square | C ++ program to find the minimum number of steps to modify the array such that the product of any two numbers in the array is a perfect square ; Function to find the smallest prime factor of the elements ; Initializing the first element of the array ; Loop to add the remaining elements to the array ; Marking the smallest prime factor for every number to be itself ; Separately marking spf for every even number as 2 ; Checking if i is prime ; Marking SPF for all the numbers divisible by i ; Marking spf [ j ] if it is not previously marked ; Function to find the minimum number of steps to modify the array such that the product of any two numbers in the array is a perfect square ; Map created to store the unique prime numbers ; Variable to store the minimum number of operations ; Loop to store every unique prime number ; Erasing 1 as a key because it is not a prime number ; Iterating through the hash ; Two variables used for counting the frequency of prime is even or odd ; First prime number ; Iterating the number D ; check if prime is a factor of the element in the array ; Loop for calculating the frequency of the element ; Check for frequency odd or even ; If it is not a factor of the element , then it is automatically even ; Storing the minimum of two variable even or odd ; Driver code ; Input array ; Creating shortest prime factorisation array
#include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; void spf_array ( int spf [ ] ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } int minimum_operation ( int b [ ] , int d , int spf [ ] ) { unordered_map < int , int > m ; int i = 0 ; int c = 0 ; for ( i = 0 ; i < d ; i ++ ) { int x = b [ i ] ; while ( x != 1 ) { x = x / spf [ x ] ; if ( m [ spf [ x ] ] == 0 ) { m [ spf [ x ] ] = 1 ; } } } m . erase ( 1 ) ; for ( auto x : m ) { int e = 0 , o = 0 ; int j = x . first ; for ( i = 0 ; i < d ; i ++ ) { if ( b [ i ] % j == 0 ) { int h = 0 ; int g = b [ i ] ; while ( g != 0 ) { if ( g % j != 0 ) { break ; } g = g / j ; h = h + 1 ; } if ( h % 2 == 0 ) { e = e + 1 ; } else { o = o + 1 ; } } else { e = e + 1 ; } } c = c + min ( o , e ) ; } return c ; } int main ( ) { int spf [ 1001 ] ; int b [ ] = { 1 , 4 , 6 } ; int d = sizeof ( b ) / sizeof ( b [ 0 ] ) ; spf_array ( spf ) ; cout << minimum_operation ( b , d , spf ) << endl ; }
Zuckerman Numbers | C ++ implementation to check if N is a Zuckerman number ; Function to get product of digits ; Function to check if N is an Zuckerman number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } bool isZuckerman ( int n ) { return n % getProduct ( n ) == 0 ; } int main ( ) { int n = 115 ; if ( isZuckerman ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Additive Prime Number | C ++ program for the above approach ; Check if N is prime or not ; Corner Cases ; This is checked to skip middle five numbers ; Function to get sum of digits ; Return the sum of digits ; Function to check whether the given number is Additive Prime number or not ; If number is not prime ; Check if sum of digits is prime or not ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> 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 ; } int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } bool isAdditivePrime ( int n ) { if ( ! isPrime ( n ) ) return false ; return isPrime ( getSum ( n ) ) ; } int main ( ) { int N = 23 ; if ( isAdditivePrime ( N ) ) cout << " Yes " ; else cout << " No " ; }
Straight | C ++ program for the above approach ; Function to check if N is a Straight Line number or not ; N must be > 99 ; Difference between consecutive digits must be same ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isStraighLineNum ( int N ) { if ( N <= 99 ) return false ; string str = to_string ( N ) ; int d = str [ 1 ] - str [ 0 ] ; for ( int i = 2 ; i < str . length ( ) ; i ++ ) if ( str [ i ] - str [ i - 1 ] != d ) return false ; return true ; } int main ( ) { int N = 135 ; if ( isStraighLineNum ( n ) ) cout << " Yes " ; else cout << " No " ; }
Second | 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 << pow ( 2 , n ) - 2 * n << endl ; } int main ( ) { int N = 4 ; findNthTerm ( N ) ; return 0 ; }
Alternating Numbers | C ++ program for the above approach ; Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isEvenOddForm ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 0 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 1 ) return false ; } return true ; } bool isOddEvenForm ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s [ i ] % 2 != 1 ) return false ; if ( i % 2 == 1 && s [ i ] % 2 != 0 ) return false ; } return true ; } bool isAlternating ( int n ) { string str = to_string ( n ) ; return ( isEvenOddForm ( str ) || isOddEvenForm ( str ) ) ; } int main ( ) { int N = 129 ; if ( isAlternating ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Ormiston prime Pairs | C ++ implementation to check Ormiston prime ; Function to check if the number is a prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagrams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Returns true if n1 and n2 are Ormiston 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 ; } const int TEN = 10 ; void updateFreq ( int n , int freq [ ] ) { while ( n ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } bool areAnagrams ( int a , int b ) { int freqA [ TEN ] = { 0 } ; int freqB [ TEN ] = { 0 } ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } bool OrmistonPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && areAnagrams ( n1 , n2 ) ) ; } int main ( ) { int n1 = 1913 , n2 = 1931 ; if ( OrmistonPrime ( n1 , n2 ) ) cout << " YES " << endl ; else cout << " NO " << endl ; return 0 ; }
Decakismyriagon Number | C ++ program for the above approach ; Function to find the N - th Decakismyriagon Number ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int DecakismyriagonNum ( int N ) { return ( 99998 * N * N - 99996 * N ) / 2 ; } int main ( ) { int N = 3 ; cout << DecakismyriagonNum ( N ) ; return 0 ; }
Untouchable Number | C ++ program for the above approach ; Function to calculate sum of all proper divisors of num ; Final result of summation of divisors ; Find all divisors of num ; If ' i ' is divisor of ' num ' ; If both divisors are same then add it only once else add both ; Add 1 to the result as 1 is also a divisor ; Function to check if N is a Untouchable Number ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int num ) { int result = 0 ; for ( int i = 2 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) result += i ; else result += ( i + num / i ) ; } } return ( result + 1 ) ; } bool isUntouchable ( int n ) { for ( int i = 1 ; i <= 2 * n ; i ++ ) { if ( divSum ( i ) == n ) return false ; } return true ; } int main ( ) { int N = 52 ; if ( isUntouchable ( n ) ) cout << " Yes " ; else cout << " No " ; }
Zygodrome Number | C ++ implementation to check if N is an zygodrome number ; Function to check if N is an zygodrome number ; convert N to string ; Adding a space at the beginning and end of the string ; Traverse the string ; If any character is not same as prev and next then return false ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool iszygodromeNum ( int N ) { string s = to_string ( N ) ; s = ' ▁ ' + s + ' ▁ ' ; for ( int i = 1 ; i < s . size ( ) - 1 ; i ++ ) { if ( s [ i ] != s [ i - 1 ] && s [ i ] != s [ i + 1 ] ) { return false ; } } return true ; } int main ( ) { int n = 1122 ; if ( iszygodromeNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Loeschian Number | C ++ program for the above approach ; Function to check if N is a Loeschian Number ; Iterate [ 0 , sqrt ( N ) ] for x ; Iterate [ 0 , sqrt ( N ) ] for y ; Check the given criteria ; If no such pair found then return false ; Driver Code ; Given Number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isLoeschian ( int n ) { for ( int x = 1 ; x <= sqrt ( n ) ; x ++ ) { for ( int y = 1 ; y <= sqrt ( n ) ; y ++ ) { if ( x * x + x * y + y * y == n ) return true ; } } return false ; } int main ( ) { int N = 19 ; if ( isLoeschian ( n ) ) cout << " Yes " ; else cout << " No " ; }
Admirable Numbers | C ++ implementation to check if N is an admirable number ; Function to calculate the sum of all divisors of a given number ; Sum of divisors ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add ; Add 1 and n to result as above loop considers proper divisors greater ; Function to check if there exists a proper divisor D ' ▁ of ▁ N ▁ such ▁ that ▁ sigma ( n ) -2D ' = 2 N ; Find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Check 1 since 1 is also a divisor ; Function to check if N is an admirable number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } bool check ( int num ) { int sigmaN = divSum ( num ) ; for ( int i = 2 ; i <= sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) { if ( sigmaN - 2 * i == 2 * num ) return true ; } else { if ( sigmaN - 2 * i == 2 * num ) return true ; if ( sigmaN - 2 * ( num / i ) == 2 * num ) return true ; } } } if ( sigmaN - 2 * 1 == 2 * num ) return true ; return false ; } bool isAdmirableNum ( int N ) { return check ( N ) ; } int main ( ) { int n = 12 ; if ( isAdmirableNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Program to print the series 2 , 15 , 41 , 80 , 132 , 197 … till N terms | C ++ implementation to print the given with the given Nth term ; Function to print the series ; Generate the ith term and ; Driver Code
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void printSeries ( int N ) { int ith_term = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { ith_term = ( 13 * i * ( i - 1 ) ) / 2 + 2 ; cout << ith_term << " , ▁ " ; } } int main ( ) { int N = 7 ; printSeries ( N ) ; return 0 ; }
Possible pairs forming a Pythagorean Triple with a given value | C ++ program to compute all the possible pairs that forms a pythagorean triple with a given value ; Function to generate all possible pairs ; Vector to store all the possible pairs ; Checking all the possible pair in the range of [ 1 , c ) ; If the pair satisfies the condition push it in the vector ; Driver Program ; If no valid pair exist ; Print all valid pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < pair < int , int > > Pairs ( int C ) { vector < pair < int , int > > ans ; for ( int i = 1 ; i < C ; i ++ ) { for ( int j = i + 1 ; j < C ; j ++ ) { if ( ( i * i ) + ( j * j ) == ( C * C ) ) { ans . push_back ( make_pair ( i , j ) ) ; } } } return ans ; } int main ( ) { int C = 13 ; vector < pair < int , int > > ans = Pairs ( C ) ; if ( ans . size ( ) == 0 ) { cout << " No ▁ valid ▁ pair ▁ exist " << endl ; return 0 ; } for ( auto i = ans . begin ( ) ; i != ans . end ( ) ; i ++ ) { cout << " ( " << i -> first << " , ▁ " << i -> second << " ) " << endl ; } return 0 ; }
How to calculate strike rate of a batsman | C ++ program to calculate strike rate of a batsman ; function to calculate strike rate of a batsman ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float strikerate ( int bowls , int runs ) { float z ; z = ( float ( runs ) / bowls ) * 100 ; return z ; } int main ( ) { int A , B ; A = 264 ; B = 173 ; cout << strikerate ( B , A ) << endl ; return 0 ; }
Check if a number exists with X divisors out of which Y are composite | C ++ program to check if a number exists having exactly X positive divisors out of which Y are composite divisors ; Count the number of times 2 divides N ; check for all possible numbers that can divide it ; if N at the end is a prime number . ; Function to check if any such number exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorize ( int N ) { int count = 0 ; int cnt = 0 ; while ( ( N % 2 ) == 0 ) { N = N / 2 ; count ++ ; } cnt = cnt + count ; for ( int i = 3 ; i <= sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } cnt = cnt + count ; } if ( N > 2 ) cnt = cnt + 1 ; return cnt ; } void ifNumberExists ( int X , int Y ) { int C , dsum ; C = X - Y - 1 ; dsum = factorize ( X ) ; if ( dsum >= C ) cout << " YES ▁ STRNEWLINE " ; else cout << " NO ▁ STRNEWLINE " ; } int main ( ) { int X , Y ; X = 6 ; Y = 4 ; ifNumberExists ( X , Y ) ; return 0 ; }
Nearest prime number in the array of every array element | C ++ program to find nearest prime number in the array for all array elements ; Create a boolean array and set all entries it as false . A value in prime [ i ] will be true if i is not a prime , else false ; Sieve of Eratosthenes function ; Update all multiples of i greater than or equal to the square of it numbers which are multiple of i and are less than i ^ 2 are already been marked . ; Function to find nearest prime number for all elements ; Compute and store all prime numbers up to maxm ; Store the indices of all primes ; If no primes are present in the array ; Store the current prime ; If the no further primes exist in the array ; For all indices less than that of the current prime ; If the current prime is nearer ; If the next prime is nearer ; Make the next prime as the current ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define max 10000000 NEW_LINE bool prime [ max ] = { false } ; void SieveOfEratosthenes ( int maxm ) { prime [ 0 ] = prime [ 1 ] = true ; for ( int i = 2 ; i * i <= maxm ; i ++ ) { if ( ! prime [ i ] ) { for ( int j = i * i ; j <= maxm ; j += i ) { prime [ j ] = true ; } } } } void print_nearest_prime ( int arr [ ] , int N ) { int maxm = * max_element ( arr , arr + N ) ; SieveOfEratosthenes ( maxm ) ; vector < int > primes ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! prime [ arr [ i ] ] ) primes . push_back ( i ) ; } if ( primes . size ( ) == 0 ) { for ( int i = 0 ; i < N ; i ++ ) { cout << -1 << " ▁ " ; } return ; } int curr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( curr == primes . size ( ) - 1 || i <= primes [ curr ] ) { cout << arr [ primes [ curr ] ] << " ▁ " ; continue ; } if ( abs ( primes [ curr ] - i ) < abs ( primes [ curr + 1 ] - i ) ) { cout << arr [ primes [ curr ] ] << " ▁ " ; } else { curr ++ ; cout << arr [ primes [ curr ] ] << " ▁ " ; } } } int main ( ) { int N = 6 ; int arr [ ] = { 8 , 7 , 12 , 15 , 3 , 11 } ; print_nearest_prime ( arr , N ) ; return 0 ; }
Find position of given term in a series formed with only digits 4 and 7 allowed | C ++ program for the above approach ; Function to find the position of the number N ; To store the position of N ; Iterate through all digit of N ; If current digit is 7 ; If current digit is 4 ; Print the final position ; Driver Code ; Given number of the series ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPosition ( int n ) { int i = 0 ; int pos = 0 ; while ( n > 0 ) { if ( n % 10 == 7 ) { pos = pos + pow ( 2 , i + 1 ) ; } else { pos = pos + pow ( 2 , i ) ; } i ++ ; n = n / 10 ; } cout << pos ; } int main ( ) { int N = 777 ; findPosition ( N ) ; return 0 ; }
Average of Cubes of first N natural numbers | C ++ program for the above approach ; Function to find average of cubes ; Storing sum of cubes of numbers in sum ; Calculate sum of cubes ; Return average ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findAverageOfCube ( int n ) { double sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += i * i * i ; } return sum / n ; } int main ( ) { int n = 3 ; cout << findAverageOfCube ( n ) ; }
Smallest and Largest N | C ++ program for the above approach ; Function to find n digit largest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit largest number ; Find the number of digits in number n ; Append 9 ; To make it largest n digit number starting and ending with n , we just need to append n at start and end ; Return the largest number ; Function to find n digit smallest number starting and ending with n ; Corner Case when n = 1 ; Result will store the n - 2 * length ( n ) digit smallest number ; Find the number of digits in number n ; To make it smallest n digit number starting and ending with n , we just need to append n at start and end ; Return the smallest number ; Driver code ; Given number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findNumberL ( int n ) { if ( n == 1 ) return "1" ; string result = " " ; int length = ( int ) floor ( log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '9' ; } result = to_string ( n ) + result + to_string ( n ) ; return result ; } string findNumberS ( int n ) { if ( n == 1 ) return "1" ; string result = " " ; int length = ( int ) floor ( log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '0' ; } result = to_string ( n ) + result + to_string ( n ) ; return result ; } int main ( ) { int N = 3 ; cout << " Smallest ▁ Number ▁ = ▁ " << findNumberS ( N ) << endl ; cout << " Largest ▁ Number ▁ = ▁ " << findNumberL ( N ) ; return 0 ; }
Maximum number of 0 s that can be flipped such that Array has no adjacent 1 s | C ++ program for the above approach ; Maximum number of 0 s that can be replaced by 1 ; Check for three consecutive 0 s ; Flip the bit ; Increase the count ; Driver 's Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int canReplace ( int array [ ] , int n ) { int i = 0 , count = 0 ; while ( i < n ) { if ( array [ i ] == 0 && ( i == 0 array [ i - 1 ] == 0 ) && ( i == n - 1 array [ i + 1 ] == 0 ) ) { array [ i ] = 1 ; count ++ ; } i ++ ; } return count ; } int main ( ) { int array [ 5 ] = { 1 , 0 , 0 , 0 , 1 } ; cout << canReplace ( array , 5 ) ; }
Sum of first N Star Numbers | C ++ program to find the sum of the first N star numbers ; Function to find the sum of the first N star number ; Variable to store the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_star_num ( int n ) { int summ = 2 * n * ( n + 1 ) * ( n - 1 ) + n ; return summ ; } int main ( ) { int n = 3 ; cout << sum_star_num ( n ) ; return 0 ; }
Count distinct median possible for an Array using given ranges of elements | C ++ implementation to Count the number of distinct medians of an array where each array elements are given by a range ; Function to Count the number of distinct medians of an array where each array elements are given by a range ; Loop to store the starting and end range in the array ; Condition to check if the length of the array is odd ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int long long int NEW_LINE void solve ( int n , const vector < pair < int , int > > & vec ) { vector < int > a , b ; for ( auto pr : vec ) { a . push_back ( pr . first ) ; b . push_back ( pr . second ) ; } sort ( a . begin ( ) , a . end ( ) ) ; sort ( b . begin ( ) , b . end ( ) ) ; int left , right , ans ; if ( ( n & 1 ) ) { left = a [ n / 2 ] ; right = b [ n / 2 ] ; ans = right - left + 1 ; } else { left = ( a [ n / 2 ] + a [ n / 2 - 1 ] ) ; right = ( b [ n / 2 ] + b [ n / 2 - 1 ] ) ; ans = right - left + 1 ; } cout << ans << endl ; } signed main ( ) { int N = 3 ; vector < pair < int , int > > vec = { { 100 , 100 } , { 10 , 10000 } , { 1 , 1000000000 } } ; solve ( N , vec ) ; return 0 ; }
Count of pairs from Array with sum equal to twice their bitwise AND | C ++ implementation to find the pairs with equal sum and twice the bitwise AND of the pairs ; Map to store the occurrence of elements of array ; Function to find the pairs with equal sum and twice the bitwise AND of the pairs ; Loop to find the frequency of elements of array ; Function to find the count such pairs in the array ; if an element occurs more than once then the answer will by incremented by nC2 times ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < int , int > mp ; int find_pairs ( int ar [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mp [ ar [ i ] ] ++ ; } for ( auto i : mp ) { int count = i . second ; if ( count > 1 ) { ans += ( ( count * ( count - 1 ) ) / 2 ) ; } } return ans ; } int main ( ) { int ar [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 5 , 7 , 8 } ; int arr_size = ( sizeof ( ar ) / sizeof ( ar [ 0 ] ) ) ; cout << find_pairs ( ar , arr_size ) ; return 0 ; }
360 | C ++ implementation for above approach ; Function to find the nth 360 - gon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gonNum360 ( int n ) { return ( 358 * n * n - 356 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << gonNum360 ( n ) ; return 0 ; }
120 | C ++ implementation for above approach ; Function to find the nth 120 - gon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gonNum120 ( int n ) { return ( 118 * n * n - 116 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << gonNum120 ( n ) ; return 0 ; }
Tetracontaoctagonal Number | C ++ implementation for above approach ; Function to find the nth Tetracontaoctagonal Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int TetracontaoctagonalNum ( int n ) { return ( 46 * n * n - 44 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << TetracontaoctagonalNum ( n ) ; return 0 ; }
257 | C ++ implementation for above approach ; Function to find the nth 257 - gon Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gonNum257 ( int n ) { return ( 255 * n * n - 253 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << gonNum257 ( n ) ; return 0 ; }
Tetracontadigonal Number | C ++ implementation for the above approach ; Function to find the nth Tetracontadigonal Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int TetracontadigonalNum ( int n ) { return ( 40 * n * n - 38 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << TetracontadigonalNum ( n ) ; return 0 ; }
Index of smallest triangular number with N digits | C ++ implementation of the above approach ; Function to return index of smallest triangular no n digits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findIndex ( int n ) { float x = sqrt ( 2 * pow ( 10 , ( n - 1 ) ) ) ; return round ( x ) ; } int main ( ) { int n = 3 ; cout << findIndex ( n ) ; return 0 ; }
Program to find the LCM of two prime numbers | C ++ Program to find LCM of two prime numbers ; Function to return the LCM of two prime numbers ; If the two numbers are equal then return any one of a and b ; Else return product of numbers ; Driver code ; Given two numbers ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLCMPrime ( int a , int b ) { if ( a == b ) { return a ; } return a * b ; } int main ( ) { int a = 3 , b = 5 ; cout << findLCMPrime ( a , b ) ; return 0 ; }
Smallest multiple of N with exactly N digits in its Binary number representation | C ++ program to find smallest multiple of n with exactly N digits in Binary number System . ; Function to find smallest multiple of n with exactly n digits in Binary number representation . ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void smallestNumber ( int N ) { cout << N * ceil ( pow ( 2 , ( N - 1 ) ) / N ) ; } int main ( ) { int N = 3 ; smallestNumber ( N ) ; return 0 ; }
Find the largest N digit multiple of N | C ++ program to find largest multiple of N containing N digits ; Function to find the largest N digit multiple of N ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void smallestNumber ( int N ) { cout << N * floor ( ( pow ( 10 , N ) - 1 ) / N ) ; } int main ( ) { int N = 2 ; smallestNumber ( N ) ; return 0 ; }
Icosikaiheptagonal Number | C ++ program to find N - th icosikaiheptagonal number ; Function to find the nth icosikaiheptagonal Number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int icosikaiheptagonalNum ( int n ) { return ( 25 * n * n - 23 * n ) / 2 ; } int main ( ) { int n = 3 ; cout << "3rd ▁ icosikaiheptagonal ▁ Number ▁ is ▁ " << icosikaiheptagonalNum ( n ) ; return 0 ; }
Program to check if N is a triacontagonal number | C ++ program to check whether a number is an triacontagonal number or not ; Function to check whether a number is an triacontagonal number or not ; Condition to check whether a number is an triacontagonal number or not ; Driver Code ; Given number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool istriacontagonal ( int N ) { float n = ( 26 + sqrt ( 224 * N + 676 ) ) / 56 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 30 ; if ( istriacontagonal ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Check whether one root of the Quadratic Equation is twice of other or not | C ++ program to check if one root of a Quadratic Equation is twice of other or not ; Function to find the required answer ; Driver code
#include <iostream> NEW_LINE using namespace std ; void checkSolution ( int a , int b , int c ) { if ( 2 * b * b == 9 * a * c ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int a = 1 , b = 3 , c = 2 ; checkSolution ( a , b , c ) ; return 0 ; }
Parity of the given mathematical expression using given N numbers | C ++ program to determine the parity of the given mathematical expression ; Iterating through the given integers ; If any odd number is present , then S is even parity ; Else , S is odd parity ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getParity ( int n , const vector < int > & A ) { for ( auto x : A ) { if ( x & 1 ) { cout << " Even " << endl ; return ; } } cout << " Odd " << endl ; } int main ( ) { int N = 3 ; vector < int > A = { 2 , 3 , 1 } ; getParity ( N , A ) ; return 0 ; }
Count of digits after concatenation of first N positive integers | C ++ program to find the number of digits after concatenating the first N positive integers ; Function to find the number of digits after concatenating the first N positive integers ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void numberOfDigits ( int N ) { int nod = floor ( log10 ( N ) + 1 ) ; int toDecrease = ( pow ( 10 , nod ) - 1 ) / 9 ; cout << ( N + 1 ) * nod - toDecrease << endl ; } int main ( ) { int N = 13 ; numberOfDigits ( N ) ; return 0 ; }
Length of maximum product subarray | C ++ program to find maximum length subarray having non zero product ; Function that returns the maximum length subarray having non zero product ; zeroindex list to store indexex of zero ; If zeroindex list is empty then Maxlength is as size of array ; If zeroindex list is not empty ; first zero is on index 2 that means two numbers positive , before index 2 so as their product is positive to ; Checking for other indexex ; If the difference is greater than maxlen then maxlen is updated ; To check the length of remaining array after last zeroindex ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Maxlength ( int arr [ ] , int N ) { vector < int > zeroindex ; int maxlen ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zeroindex . push_back ( i ) ; } if ( zeroindex . size ( ) == 0 ) { maxlen = N ; } else { maxlen = zeroindex [ 0 ] ; for ( int i = 0 ; i < zeroindex . size ( ) - 1 ; i ++ ) { if ( zeroindex [ i + 1 ] - zeroindex [ i ] - 1 > maxlen ) { maxlen = zeroindex [ i + 1 ] - zeroindex [ i ] - 1 ; } } if ( N - zeroindex [ zeroindex . size ( ) - 1 ] - 1 > maxlen ) { maxlen = N - zeroindex [ zeroindex . size ( ) - 1 ] - 1 ; } } cout << maxlen << endl ; } int main ( ) { int N = 9 ; int arr [ ] = { 7 , 1 , 0 , 1 , 2 , 0 , 9 , 2 , 1 } ; Maxlength ( arr , N ) ; }
Check if sum of exactly K elements of the Array can be odd or not | C ++ implementation of the above approach ; Function returns true if it is possible to have odd sum ; counting number of odd and even elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int arr [ ] , int N , int K ) { int oddCount = 0 , evenCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) evenCount ++ ; else oddCount ++ ; } if ( evenCount == N || ( oddCount == N && K % 2 == 0 ) || ( K == N && oddCount % 2 == 0 ) ) return false ; else return true ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 8 } ; int K = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPossible ( arr , N , K ) ) cout << " Possible " ; else cout << " Not ▁ Possible " ; return 0 ; }
Find the last two digits of Factorial of a given Number | C ++ implementation to find last two digits factorial of a given number ; Function to print the last two digits of N ! ; For N >= 10 , N ! % 100 will always be 0 ; Calculating N ! % 100 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void lastTwoDigits ( long long N ) { if ( N >= 10 ) { cout << "00" ; return ; } long long fac = 1 ; for ( int i = 1 ; i <= N ; i ++ ) fac = ( fac * i ) % 100 ; cout << fac ; } int main ( ) { int N = 7 ; lastTwoDigits ( N ) ; }
Product of N terms of a given Geometric series | C ++ program for the above approach ; Function to calculate product of geometric series ; Return the final product with the above formula ; Driver Code ; Given first term and common ratio ; Number of terms ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; float productOfGP ( float a , float r , int n ) { return pow ( a , n ) * pow ( r , n * ( n - 1 ) / 2 ) ; } int main ( ) { float a = 1 , r = 2 ; int N = 4 ; cout << productOfGP ( a , r , N ) ; }
Product of N terms of a given Geometric series | C ++ program for the above approach ; Function to calculate product of N terms of geometric series ; Find the product of first and the last term ; Return the sqrt of the above expression to find the product ; Driver Code ; Given first term and common ratio ; Number of terms ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; float productOfGP ( float a , float r , int n ) { int an = a * pow ( r , n - 1 ) ; return sqrt ( pow ( a * an , n ) ) ; } int main ( ) { float a = 1 , r = 2 ; int N = 4 ; cout << productOfGP ( a , r , N ) ; }
Find two numbers such that difference of their squares equal to N | C ++ Program to find two numbers with difference of their squares equal to N ; Function to check and print the required two positive integers ; Iterate till sqrt ( n ) to find factors of N ; Check if x is one of the factors of N ; Store the factor ; Compute the other factor ; Check if the two factors are of the same parity ; Compute a and b ; If no pair exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int n ) { for ( int x = 1 ; x <= sqrt ( n ) ; x ++ ) { if ( n % x == 0 ) { int small = x ; int big = n / x ; if ( small % 2 == big % 2 ) { int a = ( small + big ) / 2 ; int b = ( big - small ) / 2 ; cout << a << " ▁ " << b << endl ; return ; } } } cout << -1 << endl ; } int main ( ) { int n = 7 ; solve ( n ) ; return 0 ; }
Probability that an arbitrary positive divisor of 10 ^ X is an integral multiple of 10 ^ Y | C ++ program to find the probability of an arbitrary positive divisor of Xth power of 10 to be a multiple of Yth power of 10 ; Function to calculate and print the required probability ; Count of potential divisors of X - th power of 10 which are also multiples of Y - th power of 10 ; Count of divisors of X - th power of 10 ; Calculate GCD ; Print the reduced fraction probability ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int long long int NEW_LINE void prob ( int x , int y ) { int num = abs ( x - y + 1 ) * abs ( x - y + 1 ) ; int den = ( x + 1 ) * ( x + 1 ) ; int gcd = __gcd ( num , den ) ; cout << num / gcd << " / " << den / gcd << endl ; } signed main ( ) { int X = 2 , Y = 1 ; prob ( X , Y ) ; return 0 ; }
Program to check if N is a Chiliagon Number | C ++ for the above approach ; Function to check that if N is Chiliagon Number or not ; Condition to check if N is a Chiliagon Number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_Chiliagon ( int N ) { float n = ( 996 + sqrt ( 7984 * N + 992016 ) ) / 1996 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 1000 ; if ( is_Chiliagon ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Count of common subarrays in two different permutations of 1 to N | C ++ implementation of above approach ; Initialising Map for Index Mapping ; Mapping elements of A ; Modify elements of B according to Map ; Changing B [ i ] as the index of B [ i ] in A ; Count of common subarrays ; Traversing array B ; While consecutive elements are found , we increment K ; Add number of subarrays with length K to total count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int commonSubarrays ( int * A , int * B , int N ) { int Map [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) Map [ * ( A + i ) ] = i ; for ( int i = 0 ; i < N ; i ++ ) { * ( B + i ) = Map [ * ( B + i ) ] ; } int count = 0 ; int i = 0 , K ; while ( i < N ) { K = 1 ; i += 1 ; while ( i < N && B [ i ] == B [ i - 1 ] + 1 ) { i += 1 ; K += 1 ; } count = count + ( ( K ) * ( K + 1 ) ) / 2 ; } return count ; } int main ( ) { int N = 3 ; int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 2 , 3 , 1 } ; cout << ( commonSubarrays ( A , B , N ) ) << endl ; N = 5 ; int C [ ] = { 1 , 2 , 3 , 4 , 5 } ; int D [ ] = { 2 , 3 , 1 , 4 , 5 } ; cout << ( commonSubarrays ( C , D , N ) ) ; }
Represent N as sum of K even or K odd numbers with repetitions allowed | C ++ implementation to find an array of size K with all the even or odd elements in the array ; Function to find the array with all the even / odd elements ; First let 's check kth is odd or even ; if last element is also an odd number then we can choose odd elements for our answer ; Add 1 in the array ( k - 1 ) times ; Add last odd element ; If array of even elements would be the answer then k - 1 elements would be 2 ; if last element is also an even number then we can choose even elements for our answer ; Add 2 in the array ( k - 1 ) times ; Add last even element ; Printing the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getArrayOfSizeK ( int n , int k ) { vector < int > ans ; int odd = n - ( ( k - 1 ) * 1 ) ; if ( odd > 0 && odd % 2 != 0 ) { for ( int i = 0 ; i < k - 1 ; i ++ ) { ans . push_back ( 1 ) ; } ans . push_back ( odd ) ; } int even = n - ( ( k - 1 ) * 2 ) ; if ( even > 0 && even % 2 == 0 && ans . size ( ) == 0 ) { for ( int i = 0 ; i < k - 1 ; i ++ ) { ans . push_back ( 2 ) ; } ans . push_back ( even ) ; } if ( ans . size ( ) > 0 ) { for ( int i = 0 ; i < k ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } else { cout << " NO " << endl ; } } int main ( ) { int n = 10 , k = 3 ; getArrayOfSizeK ( n , k ) ; return 0 ; }
Check whether a given number N is a Nude Number or not | C ++ program to check if the number if Nude number or not ; Check if all digits of num divide num ; array to store all digits of the given number ; If any of the condition is true for any digit Then N is not a nude number ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool checkDivisbility ( int num ) { int digit ; int N = num ; while ( num != 0 ) { digit = num % 10 ; num = num / 10 ; if ( digit == 0 N % digit != 0 ) return false ; } return true ; } int main ( ) { int N = 128 ; bool result = checkDivisbility ( N ) ; if ( result ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Hensel 's Lemma | C ++ program to illustrate the Hensel 's Lemma ; Function to find the modular inverse of a modulo m ; Apply the Euclidean algorithm , to find the modular inverse ; Function to find the derivative of f ( x ) and f '(x) = 3 * (x ^ 2) ; Function to find the image of x in f ( x ) = x ^ 3 - k . ; Function to find the next power of the number ; Next power of prime for which solution is to be found ; Using Hensel 's recursion to find the solution (next_a) for next power of prime ; If next_a < 0 return equivalent positive remainder modulo p ; Return the next power of a ; Function to find the solution of the required exponent of prime ; The lemma does not work for derivative of f ( x ) at a1 ; Looping from 1 to power of prime whose solution is to be found ; Final answer after evaluating all the exponents up till the required exponent ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } int derivative ( int x ) { return 3 * x * x ; } int Image ( int x , int k ) { return x * x * x - k ; } int next_power ( int a_t , int t , int a1 , int prime , int k ) { int power_p = ( int ) pow ( prime , t + 1 ) ; int next_a = ( a_t - Image ( a_t , k ) * inv ( derivative ( a1 ) , prime ) ) % power_p ; if ( next_a < 0 ) return next_a += power_p ; return next_a ; } int powerOfPrime ( int prime , int power , int k , int a1 ) { if ( derivative ( a1 ) != 0 ) { int a_t = a1 ; for ( int p = 1 ; p < power ; p ++ ) { a_t = next_power ( a_t , p , a1 , prime , k ) ; } return a_t ; } return -1 ; } int main ( ) { int prime = 7 , a1 = 3 ; int power = 2 , k = 3 ; cout << powerOfPrime ( prime , power , k , a1 ) ; return 0 ; }
Count of numbers with all digits same in a given range | C ++ program to count the total numbers in the range L and R which have all the digit same ; Function that count the total numbersProgram between L and R which have all the digit same ; length of R ; tmp has all digits as 1 ; For each multiple of tmp in range 1 to 9 , check if it present in range [ L , R ] ; Increment the required count ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_same_digit ( int L , int R ) { int tmp = 0 , ans = 0 ; int n = log10 ( R ) + 1 ; for ( int i = 0 ; i < n ; i ++ ) { tmp = tmp * 10 + 1 ; for ( int j = 1 ; j <= 9 ; j ++ ) { if ( L <= ( tmp * j ) && ( tmp * j ) <= R ) { ans ++ ; } } } return ans ; } int main ( ) { int L = 12 , R = 68 ; cout << count_same_digit ( L , R ) << endl ; return 0 ; }
Minimum LCM of all pairs in a given array | C ++ program to find the pair having minimum LCM ; function that return pair having minimum LCM ; find max element in the array as the gcd of two elements from the array can 't greater than max element. ; created a 2D array to store minimum two multiple of any particular i . ; we already found two smallest multiple ; iterating over all gcd ; iterating over its multiple ; if we already found the two smallest multiple of i ; choosing smallest two multiple ; calculating lcm ; return final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLCM ( int arr [ ] , int n ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = max ( mx , arr [ i ] ) ; } vector < vector < int > > mul ( mx + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mul [ arr [ i ] ] . size ( ) > 1 ) { continue ; } mul [ arr [ i ] ] . push_back ( arr [ i ] ) ; } for ( int i = 1 ; i <= mx ; i ++ ) { for ( int j = i + i ; j <= mx ; j += i ) { if ( mul [ i ] . size ( ) > 1 ) { break ; } for ( int k : mul [ j ] ) { if ( mul [ i ] . size ( ) > 1 ) break ; mul [ i ] . push_back ( k ) ; } } } int ans = INT_MAX ; for ( int i = 1 ; i <= mx ; i ++ ) { if ( mul [ i ] . size ( ) <= 1 ) continue ; int a = mul [ i ] [ 0 ] , b = mul [ i ] [ 1 ] ; int lcm = ( a * b ) / i ; ans = min ( ans , lcm ) ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 , 6 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minLCM ( arr , n ) << endl ; return 0 ; }
Count of triplets of numbers 1 to N such that middle element is always largest | C ++ program to implement the above approach ; Function to find Number of triplets for given Number N such that middle element is always greater than left and right side element . ; check if arrangement is possible or Not ; else return total ways ; Driver code .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findArrangement ( int N ) { if ( N < 3 ) return 0 ; return ( ( N ) * ( N - 1 ) * ( N - 2 ) ) / 3 ; } int main ( ) { int N = 10 ; cout << findArrangement ( N ) ; return 0 ; }
Lexicographically smallest array formed by at most one swap for every pair of adjacent indices | C ++ implementation to find the lexicographically smallest array by at most single swap for every pair of adjacent indices ; Function to find the lexicographically smallest array ; maximum swaps possible ; hash to store swaps performed ; let current element be the minimum possible ; Find actual position of the minimum element ; Update minimum element and its position ; Perform swaps if possible ; Insert current swap in hash ; print the required array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSmallestArray ( int A [ ] , int n ) { int count = n - 1 ; map < pair < int , int > , int > mp ; for ( int i = 0 ; i < n && count > 0 ; ++ i ) { int mn = A [ i ] , pos = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( A [ j ] < mn ) { mn = A [ j ] ; pos = j ; } } while ( pos > i && count > 0 && ! mp [ { pos - 1 , pos } ] ) { mp [ { pos - 1 , pos } ] = 1 ; swap ( A [ pos ] , A [ pos - 1 ] ) ; -- pos ; -- count ; } } for ( int i = 0 ; i < n ; ++ i ) cout << A [ i ] << " ▁ " ; } int main ( ) { int A [ ] = { 2 , 1 , 4 , 3 , 6 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findSmallestArray ( A , n ) ; return 0 ; }
Sum of all perfect square divisors of numbers from 1 to N | C ++ Program to find the sum of all perfect square divisors of numbers from 1 to N ; Function for finding inverse of a number iteratively Here we will find the inverse of 6 , since it appears as denominator in the formula of sum of squares from 1 to N ; Store the value of the inverse of 6 once as we don 't need to call the function again and again ; Formula for finding the sum of first n squares ; No perfect square exists which is less than 4 ; Starting from 2 , present value of start is denoted here as curStart ; Finding end of the segment for which the contribution will be same ; Using the above mentioned formula to find ans % MOD ; Now for mthe next iteration start will become end + 1 ; Finally returning the answer ; Driver Code ; Here we are adding x because we have not counted 1 as perfect squares so if u want to add it you can just add that number to the ans
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MOD 1000000007 NEW_LINE #define int unsigned long long NEW_LINE int inv ( int a ) { int o = 1 ; for ( int p = MOD - 2 ; p > 0 ; p >>= 1 ) { if ( ( p & 1 ) == 1 ) o = ( o * a ) % MOD ; a = ( a * a ) % MOD ; } return o ; } int inv6 = inv ( 6 ) ; int sumOfSquares ( int n ) { n %= MOD ; return ( ( ( n * ( n + 1 ) ) % MOD * ( 2 * n + 1 ) ) % MOD * inv6 ) % MOD ; } int sums ( int n ) { if ( n < 4 ) return 0 ; int curStart = 2 , ans = 0 ; int sqrtN = sqrt ( n ) ; while ( curStart <= n / curStart ) { int V = n / ( curStart * curStart ) ; int end = sqrt ( n / V ) ; ans += ( n / ( curStart * curStart ) % MOD * ( sumOfSquares ( end ) + MOD - sumOfSquares ( curStart - 1 ) ) ) % MOD ; if ( ans >= MOD ) ans -= MOD ; curStart = end + 1 ; } return ans ; } int32_t main ( ) { int input [ ] = { 5 } ; for ( auto x : input ) { cout << " sum ▁ of ▁ all ▁ perfect " << " ▁ square ▁ divisors ▁ from " << " ▁ 1 ▁ to ▁ " << x << " ▁ is : ▁ " ; cout << x + sums ( x ) << endl ; } return 0 ; }
Check whether the binary equivalent of a number ends with "001" or not | C ++ implementation of the above approach ; Function returns true if s1 is suffix of s2 ; Function to check if binary equivalent of a number ends in "001" or not ; To store the binary number ; Count used to store exponent value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSuffix ( string s1 , string s2 ) { int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) return false ; return true ; } bool CheckBinaryEquivalent ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; int c = pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } string bin = to_string ( B_Number ) ; return isSuffix ( "001" , bin ) ; } int main ( ) { int N = 9 ; if ( CheckBinaryEquivalent ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check whether the binary equivalent of a number ends with "001" or not | C ++ implementation of the above approach ; Function to check if binary equivalent of a number ends in "001" or not ; To check if binary equivalent of a number ends in "001" or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool CheckBinaryEquivalent ( int N ) { return ( N - 1 ) % 8 == 0 ; } int main ( ) { int N = 9 ; if ( CheckBinaryEquivalent ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Panarithmic numbers within a given range | C ++ program to print Practical Numbers in given range ; function to compute divisors of a number ; vector to store divisors ; 1 will always be a divisor ; check if i is squareroot of A then only one time insert it in ans ; function to check that a number can be represented as sum of distinct divisor or not ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; return the possibility of given sum ; function to check a number is Practical or not ; vector to store divisors ; if all numbers can be represented as sum of unique divisors ; function to print Practical Numbers in a range ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > get_divisors ( int A ) { vector < int > ans ; ans . push_back ( 1 ) ; for ( int i = 2 ; i <= sqrt ( A ) ; i ++ ) { if ( A % i == 0 ) { ans . push_back ( i ) ; if ( ( i * i ) != A ) ans . push_back ( A / i ) ; } } return ans ; } bool Sum_Possible ( vector < int > set , int sum ) { int n = set . size ( ) ; bool subset [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set [ i - 1 ] ] ; } } return subset [ n ] [ sum ] ; } bool Is_Practical ( int A ) { vector < int > divisors ; divisors = get_divisors ( A ) ; for ( int i = 2 ; i < A ; i ++ ) { if ( Sum_Possible ( divisors , i ) == false ) return false ; } return true ; } void print_practica_No ( int A , int B ) { for ( int i = A ; i <= B ; i ++ ) { if ( Is_Practical ( i ) == true ) { cout << i << " ▁ " ; } } } int main ( ) { int A = 1 , B = 100 ; print_practica_No ( A , B ) ; return 0 ; }
Maximize the division result of Array using given operations | C ++ implementation to maximize the result of division of the given array elements ; Function to find the max result ; Sort the array in descending order ; loop to divide in this order arr [ 0 ] / ( arr [ 1 ] / arr [ 2 ] / ... . arr [ n - 2 ] / arr [ n - 1 ] ) ; return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float maxDivision ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; float mxdiv = arr [ 1 ] ; for ( int i = 2 ; i < n ; ++ i ) mxdiv = mxdiv / arr [ i ] ; return arr [ 0 ] / mxdiv ; } int main ( ) { int arr [ ] = { 100 , 1000 , 10 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxDivision ( arr , n ) ; return 0 ; }
Print all distinct Coprime sets possible from 1 to N | C ++ implementation to print all distinct co - prime sets possible for numbers from 1 to N ; Function to print all coprime sets ; Check if n is less than 4 then simply print all values till n ; For all the values of n > 3 ; Check if n is even then every set will contain 2 adjacent elements up - to n ; if n is odd then every set will contain 2 adjacent element except the last set which will have last three elements ; Last element for odd case ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void coPrimeSet ( int n ) { int firstadj , secadj ; if ( n < 4 ) { cout << " ( ▁ " ; for ( int i = 1 ; i <= n ; i ++ ) cout << i << " , ▁ " ; cout << " ) STRNEWLINE " ; } else { if ( n % 2 == 0 ) { for ( int i = 0 ; i < n / 2 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; cout << " ( " << firstadj << " , ▁ " << secadj << " ) STRNEWLINE " ; } } else { for ( int i = 0 ; i < n / 2 - 1 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; cout << " ( " << firstadj << " , ▁ " << secadj << " ) STRNEWLINE " ; } cout << " ( " << n - 2 << " , ▁ " << n - 1 << " , ▁ " << n << " ) STRNEWLINE " ; } } } int main ( ) { int n = 5 ; coPrimeSet ( n ) ; return 0 ; }
Find the sequence number of a triangular number | C ++ code to print sequence number of a triangular number ; If N is not tringular number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int N = 21 ; int A = sqrt ( 2 * N + 0.25 ) - 0.5 ; int B = A ; if ( B != A ) cout << " - 1" ; else cout << B ; }
Count subarrays with sum equal to its XOR value | C ++ program to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Function to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Maintain two pointers left and right ; Iterating through the array ; Calculate the window where the above condition is satisfied ; Count will be ( right - left ) ; Remove the previous element as it is already included ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll long long int NEW_LINE using namespace std ; ll operation ( int arr [ ] , int N ) { ll right = 0 , ans = 0 , num = 0 ; for ( ll left = 0 ; left < N ; left ++ ) { while ( right < N && num + arr [ right ] == ( num ^ arr [ right ] ) ) { num += arr [ right ] ; right ++ ; } ans += right - left ; if ( left == right ) right ++ ; else num -= arr [ left ] ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << operation ( arr , N ) ; }
Count of pairs in an Array whose sum is Prime | C ++ code to find number of pairs of elements in an array whose sum is prime ; Function for Sieve Of Eratosthenes ; Function to count total number of pairs of elements whose sum is prime ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool * sieveOfEratosthenes ( int N ) { bool * isPrime = new bool [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( isPrime [ i ] == true ) { int j = 2 ; while ( i * j <= N ) { isPrime [ i * j ] = false ; j ++ ; } } } return isPrime ; } int numPairsWithPrimeSum ( int * arr , int n ) { int N = 2 * 1000000 ; bool * isPrime = sieveOfEratosthenes ( N ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( isPrime [ sum ] ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << numPairsWithPrimeSum ( arr , n ) ; return 0 ; }
Count of numbers in Array ending with digits of number N | C ++ program to find the count of numbers in Array ending with digits of number N ; Array to keep the track of digits occurred Initially all are 0 ( false ) ; Function to initialize true if the digit is present ; Variable to store the last digit ; Loop to iterate through every digit of the number N ; Updating the array according to the presence of the digit in n at the array index ; Function to check if the numbers in the array end with the digits of the number N ; Variable to store the count ; Variable to store the last digit ; Checking the presence of the last digit in N ; Function to find the required count ; Driver code ; Preprocessing
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digit [ 10 ] = { 0 } ; void digitsPresent ( int n ) { int lastDigit ; while ( n != 0 ) { lastDigit = n % 10 ; digit [ lastDigit ] = true ; n /= 10 ; } } int checkLastDigit ( int num ) { int count = 0 ; int lastDigit ; lastDigit = num % 10 ; if ( digit [ lastDigit ] == true ) count ++ ; return count ; } void findCount ( int N , int K , int arr [ ] ) { int count = 0 ; for ( int i = 0 ; i < K ; i ++ ) { count = checkLastDigit ( arr [ i ] ) == 1 ? count + 1 : count ; } cout << count << endl ; } int main ( ) { int N = 1731 ; digitsPresent ( N ) ; int K = 5 ; int arr [ ] = { 57 , 6786 , 1111 , 3 , 9812 } ; findCount ( N , K , arr ) ; return 0 ; }
Check if count of even divisors of N is equal to count of odd divisors | C ++ program for the above approach ; Function to check if count of even and odd divisors are equal ; If ( n - 2 ) % 4 is an integer , then return true else return false ; Driver Code ; Given Number ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool divisorsSame ( int n ) { return ( n - 2 ) % 4 == 0 ; } int main ( ) { int N = 6 ; if ( divisorsSame ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit | C ++ program for the above approach . ; Function to find integer ; because 1 st integer is K itself ; Initialize min_d and max_d ; updating min_d and max_d ; break if min digit is 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find ( int K , int N ) { N -- ; while ( N -- ) { int curr_term = K ; int min_d = 9 ; int max_d = 0 ; while ( curr_term > 0 ) { int r = curr_term % 10 ; min_d = min ( min_d , r ) ; max_d = max ( max_d , r ) ; curr_term = curr_term / 10 ; } if ( min_d == 0 ) { break ; } K = K + min_d * max_d ; } return K ; } int main ( ) { int K = 487 ; int N = 2 ; cout << find ( K , N ) << endl ; return 0 ; }
Count of subarrays whose sum is a perfect square | C ++ code for the above approach . ; Function to find count of subarrays whose sum is a perfect square . ; to search for index with ( current prefix sum - j * j ) ; storing the prefix sum ; used to track the minimum value in prefixSum ; Calculating the prefixSum and tracking the prefixMin ; below statement is used if array contains negative numbers ; counts the no of subarrays with perfect square sum ; as 0 is a perfect square , so we initialize 0 th index - key with value 1 ; Here we count the perfect square subarray sum by searching if there is a prefix with sum = ( current prefixSum - ( sq * sq ) ) ; increasing our subarray count ; increasing the current prefixSum index value in map by 1 to count the other perfect squares while traversing further ; Driver code ; printing the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli long long int NEW_LINE lli countSubarrays ( int arr [ ] , int n ) { unordered_map < int , int > mp ; int prefixSum [ n ] ; int prefixMin = 0 ; prefixSum [ 0 ] = arr [ 0 ] ; prefixMin = min ( prefixMin , prefixSum [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; prefixMin = min ( prefixMin , prefixSum [ i ] ) ; } lli countSubs = 0 ; mp [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; prefixSum [ i ] - j * j >= prefixMin ; j ++ ) { if ( mp . find ( prefixSum [ i ] - j * j ) != mp . end ( ) ) countSubs += mp [ prefixSum [ i ] - j * j ] ; } mp [ prefixSum [ i ] ] ++ ; } return countSubs ; } int main ( ) { int arr [ ] = { 2 , 3 , -5 , 6 , -7 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; lli ans = countSubarrays ( arr , n ) ; cout << ans ; return 0 ; }
Find if two given Quadratic equations have common roots or not | C ++ Program to Find if two given Quadratic equations have common roots or not ; function to check if 2 quadratic equations have common roots or not . ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool checkSolution ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { return ( a1 / a2 ) == ( b1 / b2 ) && ( b1 / b2 ) == ( c1 / c2 ) ; } int main ( ) { float a1 = 1 , b1 = -5 , c1 = 6 ; float a2 = 2 , b2 = -10 , c2 = 12 ; if ( checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not | C ++ program to check if roots of a Quadratic Equation are numerically equal but opposite in sign or not ; Function to find the required answer ; Driver code
#include <iostream> NEW_LINE using namespace std ; void checkSolution ( int a , int b , int c ) { if ( b == 0 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int a = 2 , b = 0 , c = 2 ; checkSolution ( a , b , c ) ; return 0 ; }
Count of index pairs in array whose range product is a positive integer | C ++ Program to find the count of index pairs in the array positive range product ; Condition if number of negative elements is even then increase even_count ; Otherwise increase odd_count ; Condition if current element is negative ; Condition if number of negative elements is even then add even_count in answer ; Otherwise add odd_count in answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void positiveProduct ( int arr [ ] , int n ) { int even_count = 0 ; int odd_count = 0 ; int total_count = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( total_count % 2 == 0 ) even_count ++ ; else odd_count ++ ; if ( arr [ i ] < 0 ) total_count ++ ; if ( total_count % 2 == 0 ) ans += even_count ; else ans += odd_count ; } cout << ans << " STRNEWLINE " ; } int main ( ) { int A [ ] = { 5 , -3 , 3 , -1 , 1 } ; int size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; positiveProduct ( A , size ) ; return 0 ; }
Pair of integers having difference of their fifth power as X | C ++ implementation to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Function to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Loop to choose every possible pair with in the range ; Check if equation holds ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPair ( int x ) { int lim = 120 ; for ( int i = - lim ; i <= lim ; i ++ ) { for ( int j = - lim ; j <= lim ; j ++ ) { if ( pow ( i , 5 ) - pow ( j , 5 ) == x ) { cout << i << ' ▁ ' << j << endl ; return ; } } } cout << " - 1" ; } signed main ( ) { int X = 33 ; findPair ( X ) ; return 0 ; }
Sum of GCD of all numbers upto N with N itself | C ++ Program to find the Sum of GCD of all integers up to N with N itself ; Function to Find Sum of GCD of each numbers ; Consider all prime factors of no . and subtract their multiples from result ; Check if p is a prime factor ; If yes , then update no and result ; If no has a prime factor greater than sqrt ( n ) then at - most one such prime factor exists ; Return the result ; Finding GCD of pairs ; Calculate the divisors ; Return count of numbers from 1 to N with GCD d with N ; Check if d1 and d2 are equal then skip this ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int d , int n ) { int no = n / d ; int result = no ; for ( int p = 2 ; p * p <= no ; ++ p ) { if ( no % p == 0 ) { while ( no % p == 0 ) no /= p ; result -= result / p ; } } if ( no > 1 ) result -= result / no ; return result ; } int sumOfGCDofPairs ( int n ) { int res = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int d1 = i ; int d2 = n / i ; res += d1 * getCount ( d1 , n ) ; if ( d1 != d2 ) res += d2 * getCount ( d2 , n ) ; } } return res ; } int main ( ) { int n = 12 ; cout << sumOfGCDofPairs ( n ) ; return 0 ; }
Check whether Array represents a Fibonacci Series or not | C ++ program to check if the elements of a given array can form a Fibonacci Series ; Returns true if a permutation of arr [ 0. . n - 1 ] can form a Fibonacci Series ; Sort array ; After sorting , check if every element is equal to the sum of previous 2 elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkIsFibonacci ( int arr [ ] , int n ) { if ( n == 1 n == 2 ) return true ; sort ( arr , arr + n ) ; for ( int i = 2 ; i < n ; i ++ ) if ( ( arr [ i - 1 ] + arr [ i - 2 ] ) != arr [ i ] ) return false ; return true ; } int main ( ) { int arr [ ] = { 8 , 3 , 5 , 13 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkIsFibonacci ( arr , n ) ) cout << " Yes " << endl ; else cout << " No " ; return 0 ; }
Last digit in a power of 2 | C ++ program to find last digit in a power of 2. ; Corner case ; Find the shift in current cycle and return value accordingly ; return 6 ; When n % 4 == 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastDigit2PowerN ( int n ) { if ( n == 0 ) return 1 ; else if ( n % 4 == 1 ) return 2 ; else if ( n % 4 == 2 ) return 4 ; else if ( n % 4 == 3 ) return 8 ; else } int main ( ) { for ( int n = 0 ; n < 20 ; n ++ ) cout << lastDigit2PowerN ( n ) << " ▁ " ; return 0 ; }
Sum of all subsequences of length K | C ++ implementation to find sum of all subsequences of length K ; Function to find nCr ; Function that returns factorial of n ; Function for finding sum of all K length subsequences ; Calculate the sum of array ; Calculate nCk ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) ; int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int sumSubsequences ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int kLengthSubSequence ; kLengthSubSequence = nCr ( n , k ) ; int ans = sum * ( ( k * kLengthSubSequence ) / n ) ; return ans ; } int main ( ) { int arr [ ] = { 7 , 8 , 9 , 2 } ; int K = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumSubsequences ( arr , n , K ) ; return 0 ; }
Sum of first K numbers which are not divisible by N | C ++ Program to calculate the sum of first K numbers not divisible by N ; Function to find the sum ; Find the last multiple of N ; Find the K - th non - multiple of N ; Calculate the sum of all elements from 1 to val ; Calculate the sum of all multiples of N between 1 to val ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n , int k ) { int val = ( k / ( n - 1 ) ) * n ; int rem = k % ( n - 1 ) ; if ( rem == 0 ) { val = val - 1 ; } else { val = val + rem ; } int sum = ( val * ( val + 1 ) ) / 2 ; int x = k / ( n - 1 ) ; int sum_of_multiples = ( x * ( x + 1 ) * n ) / 2 ; sum -= sum_of_multiples ; return sum ; } int main ( ) { int n = 7 , k = 13 ; cout << findSum ( n , k ) << endl ; }
Count of the non | C ++ program to find count of non - prime divisors of given number ; Function to factors of the given number ; Loop to find the divisors of the number 2 ; Loop to find the divisors of the given number upto SQRT ( N ) ; Condition to check if the rest number is also a prime number ; Function to find the non - prime divisors of the given number ; Loop to count the number of the total divisors of given number ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > getFactorization ( int x ) { int count = 0 ; vector < int > v ; while ( x % 2 == 0 ) { count ++ ; x = x / 2 ; } if ( count != 0 ) v . push_back ( count ) ; for ( int i = 3 ; i <= sqrt ( x ) ; i += 2 ) { count = 0 ; while ( x % i == 0 ) { count ++ ; x /= i ; } if ( count != 0 ) v . push_back ( count ) ; } if ( x > 1 ) { v . push_back ( 1 ) ; } return v ; } int nonPrimeDivisors ( int N ) { vector < int > v = getFactorization ( N ) ; int ret = 1 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) ret = ret * ( v [ i ] + 1 ) ; ret = ret - v . size ( ) ; return ret ; } int main ( ) { int N = 8 ; cout << nonPrimeDivisors ( N ) << endl ; return 0 ; }
Check if a number is Full Fibonacci or not | C ++ program to check if a given number is a Full Fibonacci Number or not ; A utility function that returns true if x is perfect square ; Returns true if N is a Fibonacci Number and false otherwise ; N is Fibonacci if one of 5 * N ^ 2 + 4 or 5 * N ^ 2 - 4 or both is a perferct square ; Function to check digits ; Check if all digits are fibonacci or not ; Extract digit ; Check if the current digit is not fibonacci ; Function to check and return if N is a Full Fibonacci number or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int x ) { int s = sqrt ( x ) ; return ( s * s == x ) ; } bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } bool checkDigits ( int n ) { while ( n ) { int dig = n % 10 ; if ( dig == 4 && dig == 6 && dig == 7 && dig == 9 ) return false ; n /= 10 ; } return true ; } int isFullfibonacci ( int n ) { return ( checkDigits ( n ) && isFibonacci ( n ) ) ; } int main ( ) { int n = 13 ; if ( isFullfibonacci ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check whether a large number is divisible by 53 or not | C ++ program to check whether a number is divisible by 53 or not ; Function to check if the number is divisible by 53 or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisible ( string s ) { int flag = 0 ; while ( s . size ( ) > 4 ) { int l = s . size ( ) - 1 ; int x = ( s [ l ] - '0' ) * 37 ; reverse ( s . begin ( ) , s . end ( ) ) ; s . erase ( 0 , 1 ) ; int i = 0 , carry = 0 ; while ( x ) { int d = ( s [ i ] - '0' ) - ( x % 10 ) - carry ; if ( d < 0 ) { d += 10 ; carry = 1 ; } else carry = 0 ; s [ i ] = ( char ) ( d + '0' ) ; x /= 10 ; i ++ ; } while ( carry && i < l ) { int d = ( s [ i ] - '0' ) - carry ; if ( d < 0 ) { d += 10 ; carry = 1 ; } else carry = 0 ; s [ i ] = ( char ) ( d + '0' ) ; i ++ ; } reverse ( s . begin ( ) , s . end ( ) ) ; } int num = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { num = num * 10 + ( s [ i ] - '0' ) ; } if ( num % 53 == 0 ) return true ; else return false ; } int main ( ) { string N = "18432462191076" ; if ( isDivisible ( N ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }