text stringlengths 17 4.49k | code stringlengths 49 5.46k |
|---|---|
Maximum Prefix Sum possible by merging two given arrays | C ++ Program to implement the above approach ; Stores the maximum prefix sum of the array A [ ] ; Traverse the array A [ ] ; Stores the maximum prefix sum of the array B [ ] ; Traverse the array B [ ] ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPresum ( vector < int > a , vector < int > b ) { int X = max ( a [ 0 ] , 0 ) ; for ( int i = 1 ; i < a . size ( ) ; i ++ ) { a [ i ] += a [ i - 1 ] ; X = max ( X , a [ i ] ) ; } int Y = max ( b [ 0 ] , 0 ) ; for ( int i = 1 ; i < b . size ( ) ; i ++ ) { b [... |
Check if a number can be represented as sum of two positive perfect cubes | C ++ program for the above approach ; Function to check if N can be represented as sum of two perfect cubes or not ; if it is same return true ; ; if the curr smaller than n increment the lo ; if the curr is greater than curr decrement the hi ;... | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool sumOfTwoCubes ( int n ) { long long int lo = 1 , hi = ( long long int ) cbrt ( n ) ; while ( lo <= hi ) { long long int curr = ( lo * lo * lo + hi * hi * hi ) ; if ( curr == n ) return true ; if ( curr < n ) lo ++ ; else hi -- ; } return false ; } int main ( ... |
Generate an N | C ++ program for the above approach ; Function to generate all prime numbers upto 10 ^ 6 ; Initialize sieve [ ] as 1 ; Iterate over the range [ 2 , N ] ; If current element is non - prime ; Make all multiples of i as 0 ; Function to construct an array A [ ] satisfying the given conditions ; Stores the r... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int sieve [ 1000000 ] ; void sieveOfPrimes ( ) { memset ( sieve , 1 , sizeof ( sieve ) ) ; int N = 1000000 ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( sieve [ i ] == 0 ) continue ; for ( int j = i * i ; j <= N ; j += i ) sieve [ j ] = 0 ; } } void getArray ( in... |
Nth natural number after removing all numbers consisting of the digit 9 | C ++ implementation of above approach ; Function to find Nth number in base 9 ; Stores the Nth number ; Iterate while N is greater than 0 ; Update result ; Divide N by 9 ; Multiply p by 10 ; Return result ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; long long findNthNumber ( long long N ) { long long result = 0 ; long long p = 1 ; while ( N > 0 ) { result += ( p * ( N % 9 ) ) ; N = N / 9 ; p = p * 10 ; } return result ; } int main ( ) { int N = 9 ; cout << findNthNumber ( N ) ; return 0 ; } |
Check if an integer is rotation of another given integer | C ++ implementation of the approach ; Function to check if the integer A is a rotation of the integer B ; Stores the count of digits in A ; Stores the count of digits in B ; If dig1 not equal to dig2 ; Stores position of first digit ; Stores the first digit ; R... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( int A , int B ) { if ( A == B ) { return 1 ; } int dig1 = floor ( log10 ( A ) + 1 ) ; int dig2 = floor ( log10 ( B ) + 1 ) ; if ( dig1 != dig2 ) { return 0 ; } int temp = A ; while ( 1 ) { int power = pow ( 10 , dig1 - 1 ) ; int firstdigit = A / power ... |
Count of quadruples with product of a pair equal to the product of the remaining pair | C ++ program for the above approach ; Function to count the number of unique quadruples from an array that satisfies the given condition ; Hashmap to store the product of pairs ; Store the count of required quadruples ; Traverse the... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void sameProductQuadruples ( int nums [ ] , int N ) { unordered_map < int , int > umap ; int res = 0 ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = i + 1 ; j < N ; ++ j ) { int prod = nums [ i ] * nums [ j ] ; res += 8 * umap [ prod ] ; ++ umap [ prod ] ; } } ... |
Count ways to place M objects in distinct partitions of N boxes | C ++ implementation of the above Approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize Result ; Update x if x >= MOD to avoid multiplication overflow ; If y is odd , multiply x with result ; y = y / 2 ; Change x to x ^ 2 ; ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 1000000007 ; int power ( int x , unsigned int y , int p = MOD ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * 1LL * x ) % p ; y = y >> 1 ; x = ( x * 1LL * x ) % p ; } return res ; } void totalWays ( int N , int M ) { int ... |
Check if a graph constructed from an array based on given conditions consists of a cycle or not | C ++ program for the above approach ; Function to check if the graph constructed from given array contains a cycle or not ; Traverse the array ; If arr [ i ] is less than arr [ i - 1 ] and arr [ i ] ; Driver Code ; Given a... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void isCycleExists ( int arr [ ] , int N ) { bool valley = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { cout << " Yes " << endl ; return ; } } cout << " No " ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 4... |
Maximize first array element by performing given operations at most K times | C ++ program for the above approach ; Function to maximize the first array element ; Traverse the array ; Initialize cur_val to a [ i ] ; If all operations are not over yet ; If current value is greater than zero ; Incrementing first element ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int getMax ( int arr [ ] , int N , int K ) { for ( int i = 1 ; i < N ; i ++ ) { int cur_val = arr [ i ] ; while ( K >= i ) { if ( cur_val > 0 ) { arr [ 0 ] = arr [ 0 ] + 1 ; cur_val = cur_val - 1 ; K = K - i ; } else break ; } } cout << arr [ 0 ] ; } int main ( ) ... |
Count Non | C ++ program of the above approach ; Function to find the gcd of the two numbers ; Function to find distinct elements in the array by repeatidely inserting the absolute difference of all possible pairs ; Stores largest element of the array ; Traverse the array , arr [ ] ; Update max_value ; Stores GCD of ar... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int DistinctValues ( int arr [ ] , int N ) { int max_value = INT_MIN ; for ( int i = 0 ; i < N ; ++ i ) { max_value = max ( max_value , arr [ i ] ) ; } int GCDArr = arr [ 0 ] ; for (... |
Minimum row or column swaps required to make every pair of adjacent cell of a Binary Matrix distinct | C ++ program for the above approach ; Function to return number of moves to convert matrix into chessboard ; Size of the matrix ; Traverse the matrix ; Initialize rowSum to count 1 s in row ; Initialize colSum to coun... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int minSwaps ( vector < vector < int > > & b ) { int n = b . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( b [ 0 ] [ 0 ] ^ b [ 0 ] [ j ] ^ b [ i ] [ 0 ] ^ b [ i ] [ j ] ) return -1 ; } } int rowSum = 0 ; int colSum = 0 ; int... |
Minimum number of coins having value equal to powers of 2 required to obtain N | C ++ program for above approach ; Function to count of set bit in N ; Stores count of set bit in N ; Iterate over the range [ 0 , 31 ] ; If current bit is set ; Update result ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void count_setbit ( int N ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( 1 << i ) & N ) { result ++ ; } } cout << result << endl ; } int main ( ) { int N = 43 ; count_setbit ( N ) ; return 0 ; } |
Evaluate the expression ( N1 * ( N | C ++ program to implement the above approach ; Function to find the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; factorial [ i ] : Stores factorial of i ; Base Case for factorial ; Precompute the factorial ; dp [ N ] : Stores the value of the expre... | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define mod 1000000007 NEW_LINE int ValOfTheExpression ( int n ) { int factorial [ n ] = { 0 } ; factorial [ 0 ] = factorial [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { factorial [ i ] = ( ( factorial [ i - 1 ] % mod ) * ( i % mod ) ) % mod ; } int dp [ n ] =... |
Chocolate Distribution Problem | Set 2 | C ++ program for the above approach ; FUnction to print minimum number of candies required ; Distribute 1 chocolate to each ; Traverse from left to right ; Traverse from right to left ; Initialize sum ; Find total sum ; Return sum ; Driver Code ; Given array ; Size of the given ... | #include <iostream> NEW_LINE using namespace std ; void minChocolates ( int A [ ] , int N ) { int B [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = 1 ; } for ( int i = 1 ; i < N ; i ++ ) { if ( A [ i ] > A [ i - 1 ] ) B [ i ] = B [ i - 1 ] + 1 ; else B [ i ] = 1 ; } for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( A [... |
Construct longest possible sequence of unique elements with given LCM | C ++ program to implement the above approach ; Function to construct an array of unique elements whose LCM is N ; Stores array elements whose LCM is N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If N is divisible by i ; Insert i into newArr [ ] ;... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArrayWithGivenLCM ( int N ) { vector < int > newArr ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { newArr . push_back ( i ) ; if ( N / i != i ) { newArr . push_back ( N / i ) ; } } } sort ( newArr . begin ( ) , newArr . end ( ) ) ; for... |
Count numbers from given range having odd digits at odd places and even digits at even places | C ++ program to implement the above approach ; Function to calculate 5 ^ p ; Stores the result ; Multiply 5 p times ; Return the result ; Function to count all numbers upto N having odd digits at odd places and even digits a... | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll long long NEW_LINE ll getPower ( int p ) { ll res = 1 ; while ( p -- ) { res *= 5 ; } return res ; } ll countNumbersUtil ( ll N ) { ll count = 0 ; vector < int > digits ; while ( N ) { digits . push_back ( N % 10 ) ; N /= 10 ; } reverse ( digits . begi... |
Sum of first N natural numbers with alternate signs | C ++ program to implement the above approach ; Function to find the sum of first N natural numbers with alternate signs ; Stores sum of alternate sign of first N natural numbers ; If is an even number ; Update alternateSum ; If i is an odd number ; Update alternateS... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int alternatingSumOfFirst_N ( int N ) { int alternateSum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) { alternateSum += - i ; } else { alternateSum += i ; } } return alternateSum ; } int main ( ) { int N = 6 ; cout << alternatingSumOfFirst_N ( N ) ;... |
Sum of all numbers up to N that are co | C ++ program for the above approach ; Function to return gcd of a and b ; Base Case ; Recursive GCD ; Function to calculate the sum of all numbers till N that are coprime with N ; Stores the resultant sum ; Iterate over [ 1 , N ] ; If gcd is 1 ; Update sum ; Return the final sum... | #include <iostream> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int findSum ( unsigned int N ) { unsigned int sum = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { sum += i ; } } return sum ; } int main ( ) { int N = 5 ; cout << fin... |
Count all distinct pairs of repeating elements from the array for every array element | C ++ program for the above approach ; Function to print the required count of pairs excluding the current element ; Store the frequency ; Find all the count ; Delete the contribution of each element for equal pairs ; Print the answe... | #include <bits/stdc++.h> NEW_LINE #define int long long int NEW_LINE using namespace std ; void solve ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] ++ ; } int cnt = 0 ; for ( auto x : mp ) { cnt += ( ( x . second ) * ( x . second - 1 ) / 2 ) ; } int ans ... |
Mode in a stream of integers ( running integers ) | C ++ program to implement the above approach ; Function that prints the Mode values ; Map used to mp integers to its frequency ; To store the maximum frequency ; To store the element with the maximum frequency ; Loop used to read the elements one by one ; Updates the ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findMode ( int a [ ] , int n ) { map < int , int > mp ; int max = 0 ; int mode = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mp [ a [ i ] ] ++ ; if ( mp [ a [ i ] ] >= max ) { max = mp [ a [ i ] ] ; mode = a [ i ] ; } cout << mode << " β " ; } } int main ( ) { int... |
Count of distinct numbers formed by shuffling the digits of a large number N | C ++ program for the above approach ; Recursive function to return the value of ( x ^ n ) % m ; Base Case ; If N is even ; Else N is odd ; Function to find modular inverse of a number x under modulo m ; Using Fermat 's little theorem ; Funct... | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll long long int NEW_LINE ll modexp ( ll x , ll n , ll m ) { if ( n == 0 ) { return 1 ; } else if ( n % 2 == 0 ) { return modexp ( ( x * x ) % m , n / 2 , m ) ; } else { return ( x * modexp ( ( x * x ) % m , ( n - 1 ) / 2 , m ) % m ) ; } } ll modInverse (... |
Find prime factors of Array elements whose sum of exponents is divisible by K | C ++ program for the above approach ; To store the smallest prime factor till 10 ^ 5 ; Function to compute smallest prime factor array ; Initialize the spf array first element ; Marking smallest prime factor for every number to be itself ; ... | #include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; int spf [ 10001 ] ; 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 ; ... |
Generate first K multiples of N using Bitwise operators | C ++ program to implement the above approach ; Function to print the first K multiples of N ; Print the value of N * i ; Iterate each bit of N and add pow ( 2 , pos ) , where pos is the index of each set bit ; Check if current bit at pos j is fixed or not ; For ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void Kmultiples ( int n , int k ) { int a = n ; for ( int i = 1 ; i <= k ; i ++ ) { cout << n << " β * β " << i << " β = β " << a << endl ; int j = 0 ; while ( n >= ( 1 << j ) ) { a += n & ( 1 << j ) ; j ++ ; } } } int main ( ) { int N = 16 , K = 7 ; Kmultiples ( ... |
Least Square Regression Line | C ++ program to find the regression line ; Function to calculate b ; sum of array x ; sum of array y ; for sum of product of x and y ; sum of square of x ; Function to find the least regression line ; Finding b ; Calculating a ; Printing regression line ; Driver code ; Statistical data | #include <bits/stdc++.h> NEW_LINE using namespace std ; double calculateB ( int x [ ] , int y [ ] , int n ) { int sx = accumulate ( x , x + n , 0 ) ; int sy = accumulate ( y , y + n , 0 ) ; int sxsy = 0 ; int sx2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sxsy += x [ i ] * y [ i ] ; sx2 += x [ i ] * x [ i ] ; } double b ... |
Count of repeating digits in a given Number | C ++ program for the above approach ; Function that returns the count of repeating digits of the given number ; Initialize a variable to store count of Repeating digits ; Initialize cnt array to store digit count ; Iterate through the digits of N ; Retrieve the last digit o... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int countRepeatingDigits ( int N ) { int res = 0 ; int cnt [ 10 ] = { 0 } ; while ( N > 0 ) { int rem = N % 10 ; cnt [ rem ] ++ ; N = N / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( cnt [ i ] > 1 ) { res ++ ; } } return res ; } int main ( ) { int N = 12 ; cout... |
Find temperature of missing days using given sum and average | C ++ program for the above approach ; Function for finding the temperature ; Store Day1 - Day2 in diff ; Remaining from s will be Day1 ; Print Day1 and Day2 ; Driver Code ; Functions | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findTemperature ( int x , int y , int s ) { double Day1 , Day2 ; double diff = ( x - y ) * 6 ; Day2 = ( diff + s ) / 2 ; Day1 = s - Day2 ; cout << " Day1 β : β " << Day1 << endl ; cout << " Day2 β : β " << Day2 << endl ; } int main ( ) { int x = 5 , y = 10 , ... |
Find two numbers whose sum is N and does not contain any digit as K | C ++ program for the above approach ; Function to find two numbers whose sum is N and do not contain any digit as k ; Check every number i and ( n - i ) ; Check if i and n - i doesn 't contain k in them print i and n-i ; Check if flag is 0 then prin... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int freqCount ( string str , char k ) { int count = 0 ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { if ( str [ i ] == k ) count ++ ; } return count ; } void findAandB ( int n , int k ) { int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( freqCount ( to_str... |
Find the value of P and modular inverse of Q modulo 998244353 | C ++ implementation to find the value of P . Q - 1 mod 998244353 ; Function to find the value of P * Q ^ - 1 mod 998244353 ; Loop to find the value until the expo is not zero ; Multiply p with q if expo is odd ; Reduce the value of expo by 2 ; Driver code ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; long long calculate ( long long p , long long q ) { long long mod = 998244353 , expo ; expo = mod - 2 ; while ( expo ) { if ( expo & 1 ) { p = ( p * q ) % mod ; } q = ( q * q ) % mod ; expo >>= 1 ; } return p ; } int main ( ) { int p = 1 , q = 4 ; cout << calculat... |
Find two numbers with given sum and maximum possible LCM | C ++ program of the above approach ; Function that print two numbers with the sum X and maximum possible LCM ; variables to store the result ; If X is odd ; If X is even ; If floor ( X / 2 ) is even ; If floor ( X / 2 ) is odd ; Print the result ; Driver Code ;... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void maxLCMWithGivenSum ( int X ) { int A , B ; if ( X & 1 ) { A = X / 2 ; B = X / 2 + 1 ; } else { if ( ( X / 2 ) % 2 == 0 ) { A = X / 2 - 1 ; B = X / 2 + 1 ; } else { A = X / 2 - 2 ; B = X / 2 + 2 ; } } cout << A << " β " << B << endl ; } int main ( ) { int X = ... |
Length of longest subarray whose sum is not divisible by integer K | C ++ Program to find the length of the longest subarray whose sum is not divisible by integer K ; Function to find the longest subarray with sum is not divisible by k ; left is the index of the leftmost element that is not divisible by k ; right is th... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxSubarrayLength ( int arr [ ] , int n , int k ) { int left = -1 ; int right ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] % k ) != 0 ) { if ( left == -1 ) { left = i ; } right = i ; } sum += arr [ i ] ; } if ( ( sum % k ) != 0 ) { retur... |
Minimum steps to convert X to Y by repeated division and multiplication | C ++ implementation to find minimum steps to convert X to Y by repeated division and multiplication ; Check if X is greater than Y then swap the elements ; Check if X equals Y ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int X , int Y ) { if ( X > Y ) { int temp = X ; X = Y ; Y = temp ; } if ( X == Y ) cout << 0 << endl ; else if ( Y % X == 0 ) cout << 1 << endl ; else cout << 2 << endl ; } int main ( ) { int X = 8 , Y = 13 ; solve ( X , Y ) ; return 0 ; } |
Count quadruplets ( A , B , C , D ) till N such that sum of square of A and B is equal to that of C and D | C ++ program for the above approach ; Function to count the quadruples ; Counter variable ; Map to store the sum of pair ( a ^ 2 + b ^ 2 ) ; Iterate till N ; Calculate a ^ 2 + b ^ 2 ; Increment the value in map ;... | #include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int ll ; ll countQuadraples ( ll N ) { ll cnt = 0 ; map < ll , ll > m ; for ( ll a = 1 ; a <= N ; a ++ ) { for ( ll b = 1 ; b <= N ; b ++ ) { ll x = a * a + b * b ; m [ x ] += 1 ; } } for ( ll c = 1 ; c <= N ; c ++ ) { for ( ll d = 1 ; d <= N ; d... |
Count of distinct index pair ( i , j ) such that element sum of First Array is greater | C ++ program for the above problem ; function to find the number of pairs satisfying the given cond . ; variables used for traversal ; count variable to store the count of possible pairs ; Nested loop to find out the possible pairs... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int count_pairs ( int a [ ] , int b [ ] , int N ) { int i , j ; int count = 0 ; for ( i = 0 ; i < ( N - 1 ) ; i ++ ) { for ( j = ( i + 1 ) ; j < N ; j ++ ) { if ( ( a [ i ] + a [ j ] ) > ( b [ i ] + b [ j ] ) ) { count ++ ; } } } return count ; } int main ( ) { in... |
Count of distinct index pair ( i , j ) such that element sum of First Array is greater | C ++ program of the above approach ; Function to find the number of pairs . ; Array c [ ] where c [ i ] = a [ i ] - b [ i ] ; Sort the array c ; Initialise answer as 0 ; Iterate from index 0 to n - 1 ; If c [ i ] <= 0 then in the s... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfPairs ( int * a , int * b , int n ) { int c [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = a [ i ] - b [ i ] ; } sort ( c , c + n ) ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( c [ i ] <= 0 ) continue ; int pos = lower_bound ( c , c... |
Find K for every Array element such that at least K prefixes are Γ’ β°Β₯ K | C ++ program for the above approach ; Function to find the K - value for every index in the array ; Multiset to store the array in the form of red - black tree ; Iterating over the array ; Inserting the current value in the multiset ; Condition t... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int print_h_index ( int arr [ ] , int N ) { multiset < int > ms ; for ( int i = 0 ; i < N ; i ++ ) { ms . insert ( arr [ i ] ) ; if ( * ms . begin ( ) < ms . size ( ) ) { ms . erase ( ms . begin ( ) ) ; } cout << ms . size ( ) << " β " ; } } int main ( ) { int arr... |
Prefix Product Array | C ++ Program to generate Prefix Product Array ; Function to generate prefix product array ; Update the array with the product of prefixes ; Print the array ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int prefixProduct ( int a [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i ] * a [ i - 1 ] ; } for ( int j = 0 ; j < n ; j ++ ) { cout << a [ j ] << " , β " ; } return 0 ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 5 , 10 } ; int N = sizeof ( a... |
Count of ways to distribute N items among 3 people with one person receiving maximum | C ++ program to find the number of ways to distribute N item among three people such that one person always gets the maximum value ; Function to find the number of ways to distribute N items among 3 people ; No distribution possible ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( int N ) { if ( N < 4 ) return 0 ; int ans = ( ( N - 1 ) * ( N - 2 ) ) / 2 ; int s = 0 ; for ( int i = 2 ; i <= N - 3 ; i ++ ) { for ( int j = 1 ; j < i ; j ++ ) { if ( N == 2 * i + j ) s ++ ; } } if ( N % 3 == 0 ) s = 3 * s + 1 ; else s = 3 * s ; r... |
Magnanimous Numbers | C ++ implementation to check if a number is Magnanimous ; Function to check if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the number is Magnanimous or not ; converting the number to string ; finding length of string ; nu... | #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 ; } bool isMagnanimous ( int... |
Honaker Prime Number | C ++ program for the above approach ; Function to precompute the position of every prime number using Sieve ; 0 and 1 are not prime numbers ; Variable to store the position ; Incrementing the position for every prime number ; Function to get sum of digits ; Function to check whether the given num... | #include <bits/stdc++.h> NEW_LINE #define limit 10000000 NEW_LINE using namespace std ; int position [ limit + 1 ] ; void sieve ( ) { position [ 0 ] = -1 , position [ 1 ] = -1 ; int pos = 0 ; for ( int i = 2 ; i <= limit ; i ++ ) { if ( position [ i ] == 0 ) { position [ i ] = ++ pos ; for ( int j = i * 2 ; j <= limit... |
Check if Matrix sum is prime or not | C ++ implementation to check if the sum of matrix is prime or not ; Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for to find the sum of the given matrix ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 4 , M = 5 ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } int takeSum ( int a [ N ] [ M ] ) { int s = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j... |
Sum of sum | C ++ program to implement the above approach ; Function to find the sum ; Calculate sum - series for every natural number and add them ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; static long sumOfSumSeries ( int N ) { long sum = 0L ; for ( int i = 1 ; i <= N ; i ++ ) { sum = sum + ( i * ( i + 1 ) ) / 2 ; } return sum ; } int main ( ) { int N = 5 ; cout << sumOfSumSeries ( N ) ; } |
Sum of sum | C ++ program to implement the above approach ; Function to find the sum ; Driver code | #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; long sumOfSumSeries ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } int main ( ) { int N = 5 ; cout << sumOfSumSeries ( N ) ; return 0 ; } |
Tetradic Primes | C ++ implementation to print all Tetradic primes smaller than or equal to N . ; Function to check if the number N having all digits lies in the set ( 0 , 1 , 8 ) ; Function to check if the number N is palindrome ; Function to check if a number N is Tetradic ; Function to generate all primes and checki... | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isContaindigit ( int n ) { while ( n > 0 ) { if ( ! ( n % 10 == 0 n % 10 == 1 n % 10 == 8 ) ) return false ; n = n / 10 ; } return true ; } bool ispalindrome ( int n ) { string temp = to_string ( n ) ; int l = temp . length ( ) ; for ( int i = 0 ; i < l / 2 ;... |
Astonishing Numbers | C ++ implementation for the above approach ; Function to concatenate two integers into one ; Convert both the integers to string ; Concatenate both strings ; Convert the concatenated string to integer ; return the formed integer ; Function to check if N is a Astonishing number ; Loop to find sum o... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int concat ( int a , int b ) { string s1 = to_string ( a ) ; string s2 = to_string ( b ) ; string s = s1 + s2 ; int c = stoi ( s ) ; return c ; } bool isAstonishing ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum... |
Digitally balanced numbers | C ++ implementation to check if a number is a digitally balanced number ; Function to check if the digits in the number is the same number of digits ; Loop to iterate over the digits of the number N ; Loop to iterate over the map ; Driver Code ; function to check | #include <bits/stdc++.h> NEW_LINE using namespace std ; int checkSame ( int n , int b ) { map < int , int > m ; while ( n != 0 ) { int r = n % b ; n = n / b ; m [ r ] ++ ; } int last = -1 ; for ( auto i = m . begin ( ) ; i != m . end ( ) ; i ++ ) { if ( last != -1 && i -> second != last ) { return false ; } else { last... |
Sum of series formed by difference between product and sum of N natural numbers | C ++ program to implement the above approach ; Function to calculate the sum upto Nth term ; Stores the sum of the series ; Stores the product of natural numbers upto the current term ; Stores the sum of natural numbers upto the upto curr... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int seriesSum ( int n ) { int sum = 0 ; int currProd = 1 ; int currSum = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { currProd *= i ; currSum += i ; sum += currProd - currSum ; } return sum ; } int main ( ) { int N = 5 ; cout << seriesSum ( N ) << " β " ; } |
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 in the array arr [ ] ; Iterate over the array ; Check if the element is itself or not ; Check for divisibility ; Return th... | #include <bits/stdc++.h> NEW_LINE #define ll long long int NEW_LINE using namespace std ; int count ( int a [ ] , int n ) { int countElements = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool flag = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; if ( a [ i ] % a [ j ] == 0 ) { flag = false ; break ; } ... |
Smallest N digit number divisible by N | C ++ program for the above approach ; Function to find the smallest N - digit number divisible by N ; Return the smallest N - digit number calculated using above formula ; Driver Code ; Given N ; Function Call | #include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int smallestNumber ( int N ) { return N * ceil ( pow ( 10 , ( N - 1 ) ) / N ) ; } int main ( ) { int N = 2 ; cout << smallestNumber ( N ) ; return 0 ; } |
Count pairs in an array containing at least one even value | C ++ implementation to count pairs in an array such that each pair contains at least one even element ; Function to count the pairs in the array such as there is at least one even element in each pair ; Generate all possible pairs and increment then count if ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int CountPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] % 2 == 0 arr [ j ] % 2 == 0 ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 8 , 2 , 3 , 1 , 4 , 2 } ;... |
Count pairs in an array containing at least one even value | C ++ implementation to Count pairs in an array such that each pair contains at least one even element ; Function to count the pairs in the array such as there is at least one even element in each pair ; Store count of even and odd elements ; Check element is ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int CountPairs ( int arr [ ] , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return ( even * ( even - 1 ) ) / 2 + ( even * odd ) ; } int main ( ) { int arr [ ] = { 8 , 2 , 3 , 1 , 4 , 2 } ... |
Giuga Numbers | C ++ program for the above approach ; Function to check if n is a composite number ; Corner cases ; This is checked to skip middle 5 numbers ; Function to check if N is a Giuga Number ; N should be composite to be a Giuga Number ; Print the number of 2 s that divide n ; N must be odd at this point . So ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } bool isGiugaNum ( in... |
Droll Numbers | C ++ program for the above approach ; Function to check droll numbers ; To store sum of even prime factors ; To store sum of odd prime factors ; Add the number of 2 s that divide n in sum_even ; N must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and... | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDroll ( int n ) { if ( n == 1 ) return false ; int sum_even = 0 ; int sum_odd = 0 ; while ( n % 2 == 0 ) { sum_even += 2 ; n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { sum_odd += i ; n = n / i ; } } if ( n > 2 ) sum... |
Count all pairs of divisors of a number N whose sum is coprime with N | C ++ program to count all pairs of divisors such that their sum is coprime with N ; Function to calculate GCD ; Function to count all valid pairs ; Initialize count ; Check if sum of pair and n are coprime ; Return the result ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return ( gcd ( b , a % b ) ) ; } int CountPairs ( int n ) { int cnt = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int div1 = i ; int div2 = n / i ; int sum = div1 + div2 ; if ( gcd ( sum , n ... |
Check if A can be converted to B by reducing with a Prime number | C ++ implementation to find if it is possible to make a equal to b ; Function to find if it is possible to make A equal to B ; Driver Code ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int A , int B ) { return ( A - B > 1 ) ; } int main ( ) { int A = 10 , B = 4 ; if ( isPossible ( A , B ) ) cout << " Yes " ; else cout << " No " ; return 0 ; } |
Maximize sum of minimum difference of divisors of nodes in N | C ++ program to maximize the sum of minimum difference of divisors of nodes in an n - ary tree ; Array to store the result at each node ; Function to get minimum difference between the divisors of a number ; Iterate from square root of N to N ; return absol... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int sub [ 100005 ] ; int minDivisorDifference ( int n ) { int num1 ; int num2 ; for ( int i = sqrt ( n ) ; i <= n ; i ++ ) { if ( n % i == 0 ) { num1 = i ; num2 = n / i ; break ; } } return abs ( num1 - num2 ) ; } int dfs ( vector < int > g [ ] , int u , int par )... |
Program to check if N is a Centered Cubic Number | C ++ program to check if N is a centered cubic number ; Function to check if the number N is a centered cubic number ; Iterating from 1 ; Infinite loop ; Finding ith_term ; Checking if the number N is a Centered cube number ; If ith_term > N then N is not a Centered cu... | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCenteredcube ( int N ) { int i = 1 ; while ( true ) { int ith_term = ( 2 * i + 1 ) * ( i * i + i + 1 ) ; if ( ith_term == N ) { return true ; } if ( ith_term > N ) { return false ; } i ++ ; } } int main ( ) { int N = 9 ; if ( isCenteredcube ( N ) ) { cout <... |
Product of N terms of a given Geometric series | C ++ program for the above approach ; Function to calculate product of geometric series ; Initialise final product with 1 ; Multiply product with each term stored in a ; Return the final product ; Driver Code ; Given first term and common ratio ; Number of terms ; Functi... | #include <bits/stdc++.h> NEW_LINE using namespace std ; float productOfGP ( float a , float r , int n ) { float product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product = product * a ; a = a * r ; } return product ; } int main ( ) { float a = 1 , r = 2 ; int N = 4 ; cout << productOfGP ( a , r , N ) ; } |
Sum of given N fractions in reduced form | C ++ program for the above approach ; Function to find GCD of a & b using Euclid Lemma ; Base Case ; Function to find the LCM of all elements in arr [ ] ; Initialize result ; Iterate arr [ ] to find LCM ; Return the final LCM ; Function to find the sum of N fraction in reduced... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } int findlcm ( int arr [ ] , int n ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } ... |
Minimum LCM of all pairs in a given array | C ++ program to find minimum possible lcm from any pair ; function to compute GCD of two numbers ; function that return minimum possible lcm from any pair ; fix the ith element and iterate over all the array to find minimum LCM ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int minLCM ( int arr [ ] , int n ) { int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int g = gcd ( arr [ i ] , arr [ j ] ) ; int lcm = a... |
Find two numbers whose difference of fourth power is equal to N | C ++ implementation to find the values of x and y for the given equation with integer N ; Function which find required x & y ; Upper limit of x & y , if such x & y exists ; num1 stores x ^ 4 ; num2 stores y ^ 4 ; If condition is satisfied the print and r... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int n ) { int upper_limit = ceil ( pow ( n , 1.0 / 4 ) ) ; for ( int x = 0 ; x <= upper_limit ; x ++ ) { for ( int y = 0 ; y <= upper_limit ; y ++ ) { int num1 = x * x * x * x ; int num2 = y * y * y * y ; if ( num1 - num2 == n ) { cout << " x β = β " ... |
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 ; To store the count of even factors and odd factors ; Loop till [ 1 , sqrt ( N ) ] ; If divisors are equal add only one ; Check for even divisor ;... | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool divisorsSame ( int n ) { int even_div = 0 , odd_div = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } } else { if ( i % 2 == 0 ) { even_div ++ ; } else { odd_di... |
Check if N is a Balanced Prime number or not | C ++ program to check if a given number is Balanced prime ; Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a Balanced prime ; If n is not a pr... | #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 ; } bool isBalancedPrime ( i... |
Count of nodes having odd divisors in the given subtree for Q queries | C ++ implementation to count the number of nodes having odd number of divisors for each query ; Adjacency list for tree . ; Array for values and answer at ith node . ; Function to check whether N has odd divisors or not ; DFS function to pre - comp... | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define N 100001 NEW_LINE vector < int > adj [ N ] ; int a [ N ] , ans [ N ] ; bool hasOddNumberOfDivisors ( int n ) { if ( ( double ) sqrt ( n ) == ( int ) sqrt ( n ) ) return true ; return false ; } int dfs ( int node , int parent ) { int count = 0 ; for ( auto... |
Minimum Cost to make all array elements equal using given operations | C ++ implementation to find the minimum cost to make all array elements equal ; Function that returns the cost of making all elements equal to current element ; Compute the lower bound of current element ; Calculate the requirement of add operation ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int costCalculation ( int current , int arr [ ] , int n , int pref [ ] , int a , int r , int minimum ) { int index = lower_bound ( arr , arr + n , current ) - arr ; int left = index * current - pref [ index ] ; int right = pref [ n ] - pref [ index ] - ( n - index... |
Count of integers up to N which represent a Binary number | C ++ Program to count the number of integers upto N which are of the form of binary representations ; Function to return the count ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set ans... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int countBinaries ( int N ) { int ctr = 1 ; int ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += pow ( 2 , ctr - 1 ) ; } else if ( N % 10 > 1 ) { ans = pow ( 2 , ctr ) - 1 ; } ctr ++ ; N /= 10 ; } return ans ; } int main ( ) { int N = 20 ; cout << countBina... |
Count of integers up to N which represent a Binary number | C ++ Program to count the number of integers upto N which are of the form of binary representations ; Function to return the count ; PreCompute and store the powers of 2 ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; I... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int countBinaries ( int N ) { vector < int > powersOfTwo ( 11 ) ; powersOfTwo [ 0 ] = 1 ; for ( int i = 1 ; i < 11 ; i ++ ) { powersOfTwo [ i ] = powersOfTwo [ i - 1 ] * 2 ; } int ctr = 1 ; int ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += powersOfTwo [ ... |
Find the sum of the first Nth Centered Hexadecagonal Number | C ++ program to find the sum of the first N centred hexadecagonal numbers ; Centered_Hexadecagonal number function ; Formula to calculate nth Centered_Hexadecagonal number & return it into main function . ; Function to find the sum of the first N centered he... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Centered_Hexadecagonal_num ( int n ) { return ( 8 * n * n - 8 * n + 1 ) ; } int sum_Centered_Hexadecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Hexadecagonal_num ( i ) ; } return summ ; } int main ( ) { int n =... |
Find the sum of the first N Centered heptagonal number | C ++ program to find the sum of the first N centered heptagonal numbers ; Function to find the N - th centered heptagonal number ; Formula to calculate nth centered heptagonal number ; Function to find the sum of the first N centered heptagonal numbers ; Variable... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int center_heptagonal_num ( int n ) { return ( 7 * n * n - 7 * n + 2 ) / 2 ; } int sum_center_heptagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += center_heptagonal_num ( i ) ; } return summ ; } int main ( ) { int n = 5 ; cout <... |
Find the sum of the first N Centered Dodecagonal Number | C ++ program to find the sum of the first N Centred Dodecagonal number ; Function to find the N - th Centered Dodecagonal number ; Formula to calculate nth Centered_Dodecagonal number ; Function to find the sum of the first N Centered_Dodecagonal number ; Variab... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Centered_Dodecagonal_num ( int n ) { return 6 * n * ( n - 1 ) + 1 ; } int sum_Centered_Dodecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Dodecagonal_num ( i ) ; } return summ ; } int main ( ) { int n = 5 ; cout ... |
Find the sum of the first N Centered Octagonal Number | C ++ program to find the sum of the first N centered octagonal number ; Function to find the N - th centered octagonal number ; Formula to calculate nth centered octagonal number ; Function to find the sum of the first N centered octagonal numbers ; Variable to st... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int center_Octagonal_num ( int n ) { return ( 4 * n * n - 4 * n + 1 ) ; } int sum_center_Octagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += center_Octagonal_num ( i ) ; } return summ ; } int main ( ) { int n = 5 ; cout << ( sum... |
Find the sum of the first N Centered Decagonal Numbers | C ++ program to find the sum of the first N centred decagonal number ; Function to find the N - th centred decagonal number ; Formula to calculate nth centered_decagonal number & return it into main function . ; Function to find the sum of the first N centered de... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Centered_decagonal_num ( int n ) { return ( 5 * n * n - 5 * n + 1 ) ; } int sum_Centered_decagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_decagonal_num ( i ) ; } return summ ; } int main ( ) { int n = 5 ; cout <<... |
Find the sum of the first N Centered Octadecagonal Numbers | C ++ program to find the sum of the first N centered octadecagonal numbers ; Function to find the N - th centered octadecagonal number ; Formula to calculate nth centered octadecagonal number ; Function to find the sum of the first N centered octadecagonal nu... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int center_octadecagon_num ( int n ) { return ( 9 * n * n - 9 * n + 1 ) ; } int sum_center_octadecagon_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += center_octadecagon_num ( i ) ; } return summ ; } int main ( ) { int n = 3 ; cout <<... |
Find the sum of the first Nth Centered Pentadecagonal Number | C ++ program to find the sum of the first N centered pentadecagonal number ; Function to find the centered pentadecagonal number ; Formula to calculate N - th centered pentadecagonal number ; Function to find the sum of the first N centered pentadecagonal n... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Centered_Pentadecagonal_num ( int n ) { return ( 15 * n * n - 15 * n + 2 ) / 2 ; } int sum_Centered_Pentadecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Pentadecagonal_num ( i ) ; } return summ ; } int main ( ) ... |
Program to check if N is a Octagonal Number | C ++ program for the above approach ; Function to check if N is a Octagonal Number ; Condition to check if the number is a octagonal number ; Driver Code ; Given Number ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isoctagonal ( int N ) { float n = ( 2 + sqrt ( 12 * N + 4 ) ) / 6 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 8 ; if ( isoctagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; } |
Program to check if N is a Pentadecagonal Number | C ++ program for the above approach ; Function to check if N is a Pentadecagon number ; Condition to check if the number is a Pentadecagon number ; Driver Code ; Given Number ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPentadecagon ( int N ) { float n = ( 11 + sqrt ( 104 * N + 121 ) ) / 26 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 15 ; if ( isPentadecagon ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; } |
Program to check if N is a Tetradecagonal Number | C ++ program for the above approach ; Function to check if N is a Tetradecagonal Number ; Condition to check if the number is a tetradecagonal number ; Driver Code ; Given Number ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool istetradecagonal ( int N ) { float n = ( 10 + sqrt ( 96 * N + 100 ) ) / 24 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 11 ; if ( istetradecagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; } |
Find the sum of the first Nth Icosagonal Numbers | C ++ program to find the sum of the first N icosagonal number ; Function to calculate the N - th icosagonal number ; Formula to calculate nth icosagonal number & return it ; Function to find the sum of the first N icosagonal numbers ; Variable to store the sum ; Loop t... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Icosagonal_num ( int n ) { return ( 18 * n * n - 16 * n ) / 2 ; } int sum_Icosagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { summ += Icosagonal_num ( i ) ; } return summ ; } int main ( ) { int n = 5 ; cout << sum_Icosagonal_num ( n )... |
Find the sum of the first N Centered Pentagonal Number | C ++ program to find the sum of the first N centered pentagonal numbers ; Function to find the Centered_Pentagonal number ; Formula to calculate nth Centered_Pentagonal number & return it into main function . ; Function to find the sum of the first N Centered_Pen... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Centered_Pentagonal_num ( int n ) { return ( 5 * n * n - 5 * n + 2 ) / 2 ; } int sum_Centered_Pentagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Pentagonal_num ( i ) ; } return summ ; } int main ( ) { int n = 5 ; ... |
Find the sum of the first Nth Centered Tridecagonal Numbers | C ++ program to find the sum of the first Nth centered tridecagonal number ; Function to calculate the N - th centered tridecagonal number ; Formula to calculate Nth centered tridecagonal number & return it ; Function to find the sum of the first N centered ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int Centered_tridecagonal_num ( int n ) { return ( 13 * n * ( n - 1 ) + 2 ) / 2 ; } int sum_Centered_tridecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { summ += Centered_tridecagonal_num ( i ) ; } return summ ; } int main ( ) { int n = ... |
Program to check if N is a Concentric Hexagonal Number | C ++ program to check if N is a Concentric Hexagonal Number ; Function to check if the number is a Concentric hexagonal number ; Condition to check if the number is a Concentric hexagonal number ; Driver Code ; Function call | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isConcentrichexagonal ( int N ) { float n = sqrt ( ( 2 * N ) / 3 ) ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 6 ; if ( isConcentrichexagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; } |
Count Sexy Prime Pairs in the given array | C ++ program to count Sexy Prime pairs in array ; To store check the prime number ; A utility function that find the Prime Numbers till N ; Resize the Prime Number ; Loop till sqrt ( N ) to find prime numbers and make their multiple false in the bool array Prime ; Function th... | #include <bits/stdc++.h> NEW_LINE using namespace std ; vector < bool > Prime ; void computePrime ( int N ) { Prime . resize ( N + 1 , true ) ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( Prime [ i ] ) { for ( int j = i * i ; j < N ; j += i ) { Prime [ j ] = false ; } } } } int coun... |
Count of ways to write N as a sum of three numbers | C ++ program to count the total number of ways to write N as a sum of three numbers ; Function to find the number of ways ; Check if number is less than 2 ; Calculate the sum ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void countWays ( int n ) { if ( n <= 2 ) cout << " - 1" ; else { int ans = ( n - 1 ) * ( n - 2 ) / 2 ; cout << ans ; } } int main ( ) { int N = 5 ; countWays ( N ) ; return 0 ; } |
Logarithm tricks for Competitive Programming | C ++ implementation to check that a integer is a power of Two ; Function to check if the number is a power of two ; Driver Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int n ) { return ( ceil ( log2 ( n ) ) == floor ( log2 ( n ) ) ) ; } int main ( ) { int N = 8 ; if ( isPowerOfTwo ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } } |
Count of pairs having bit size at most X and Bitwise OR equal to X | C ++ implementation to Count number of possible pairs of ( a , b ) such that their Bitwise OR gives the value X ; Function to count the pairs ; Initializing answer with 1 ; Iterating through bits of x ; check if bit is 1 ; multiplying ans by 3 if bit ... | #include <iostream> NEW_LINE using namespace std ; int count_pairs ( int x ) { int ans = 1 ; while ( x > 0 ) { if ( x % 2 == 1 ) ans = ans * 3 ; x = x / 2 ; } return ans ; } int main ( ) { int X = 6 ; cout << count_pairs ( X ) << endl ; return 0 ; } |
Find the Kth number which is not divisible by N | C ++ implementation for above approach ; Function to find the Kth not divisible by N ; Lowest possible value ; Highest possible value ; To store the Kth non divisible number of N ; Using binary search ; Calculating mid value ; Sol would have the value by subtracting all... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void kthNonDivisible ( int N , int K ) { int L = 1 ; int H = INT_MAX ; int ans = 0 ; while ( L <= H ) { int mid = ( L + H ) / 2 ; int sol = mid - mid / N ; if ( sol > K ) { H = mid - 1 ; } else if ( sol < K ) { L = mid + 1 ; } else { ans = mid ; H = mid - 1 ; } } ... |
Print any pair of integers with sum of GCD and LCM equals to N | C ++ implementation to Print any pair of integers whose summation of GCD and LCM is equal to integer N ; Function to print the required pair ; print the pair ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void printPair ( int n ) { cout << 1 << " β " << n - 1 ; } int main ( ) { int n = 14 ; printPair ( n ) ; return 0 ; } |
Find the length of largest subarray in which all elements are Autobiographical Numbers | C ++ program to find the length of the largest subarray whose every element is an Autobiographical Number ; function to check number is autobiographical ; Convert integer to string ; Iterate for every digit to check for their total... | #include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAutoBiographyNum ( int number ) { int count = 0 , position , size , digit ; string NUM ; NUM = to_string ( number ) ; size = NUM . length ( ) ; for ( int i = 0 ; i < size ; i ++ ) { position = NUM [ i ] - '0' ; count = 0 ; for ( int j = 0 ; j < size ; j ++ ... |
Euler 's Factorization method | C ++ program to implement Eulers Factorization algorithm ; Function to return N as the sum of two squares in two possible ways ; Iterate a loop from 1 to sqrt ( n ) ; If i * i is square check if there exists another integer such that h is a perfect square and i * i + h = n ; If h is perf... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOfSquares ( int n , vector < pair < int , int > > & vp ) { for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { int h = n - i * i , h1 = sqrt ( h ) ; if ( h1 * h1 == h ) { int a = max ( h1 , i ) , b = min ( h1 , i ) ; if ( vp . size ( ) == 1 && a != vp [ 0 ] . fir... |
Print the nodes of the Binary Tree whose height is a Prime number | C ++ implementation of nodes at prime height in the given tree ; To store Prime Numbers ; To store height of each node ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to perform dfs ; Store the... | #include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX 100000 NEW_LINE vector < int > graph [ MAX + 1 ] ; vector < bool > Prime ( MAX + 1 , true ) ; int height [ MAX + 1 ] ; void SieveOfEratosthenes ( ) { int i , j ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( i = 2 ; i * i <= MAX ; i ++ ) { if ( Prime [ i ... |
Find Prime Adam integers in the given range [ L , R ] | C ++ program to find all prime adam numbers in the given range ; Reversing a number by taking remainder at a time ; Function to check if a number is a prime or not ; Iterating till the number ; Checking for factors ; Returning 1 if the there are no factors of the ... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int reverse ( int a ) { int rev = 0 ; while ( a != 0 ) { int r = a % 10 ; rev = rev * 10 + r ; a = a / 10 ; } return ( rev ) ; } int prime ( int a ) { int k = 0 ; for ( int i = 2 ; i < a ; i ++ ) { if ( a % i == 0 ) { k = 1 ; break ; } } if ( k == 1 ) { return ( 0... |
Determine whether the given integer N is a Peculiar Number or not | C ++ implementation to check if the number is peculiar ; Function to find sum of digits of a number ; Function to check if the number is peculiar ; Store a duplicate of n ; Driver code | #include <bits/stdc++.h> NEW_LINE using namespace std ; int sumDig ( int n ) { int s = 0 ; while ( n != 0 ) { s = s + ( n % 10 ) ; n = n / 10 ; } return s ; } bool Pec ( int n ) { int dup = n ; int dig = sumDig ( n ) ; if ( dig * 3 == dup ) return true ; else return false ; } int main ( ) { int n = 36 ; if ( Pec ( n ) ... |
Find N numbers such that a number and its reverse are divisible by sum of its digits | C ++ program to print the first N numbers such that every number and the reverse of the number is divisible by its sum of digits ; Function to calculate the sum of digits ; Loop to iterate through every digit of the number ; Returnin... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int digit_sum ( int n ) { int sum = 0 , m ; while ( n > 0 ) { m = n % 10 ; sum = sum + m ; n = n / 10 ; } return ( sum ) ; } int reverse ( int n ) { int r = 0 ; while ( n != 0 ) { r = r * 10 ; r = r + n % 10 ; n = n / 10 ; } return ( r ) ; } void operation ( int n... |
Split N natural numbers into two sets having GCD of their sums greater than 1 | C ++ program to split N natural numbers into two sets having GCD of their sums greater than 1 ; Function to create and print the two sets ; No such split possible for N <= 2 ; Print the first set consisting of even elements ; Print the seco... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void createSets ( int N ) { if ( N <= 2 ) { cout << " - 1" << endl ; return ; } for ( int i = 2 ; i <= N ; i += 2 ) cout << i << " β " ; cout << " STRNEWLINE " ; for ( int i = 1 ; i <= N ; i += 2 ) { cout << i << " β " ; } } int main ( ) { int N = 6 ; createSets (... |
Count the nodes in the given tree whose weight is a powerful number | C ++ implementation to Count the nodes in the given tree whose weight is a powerful number ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return false ; Check if n is not... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = 0 ; vector < int > graph [ 100 ] ; vector < int > weight ( 100 ) ; bool isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= sqrt ( ... |
Number of ways to color boundary of each block of M * N table | C ++ program to count the number of ways to color boundary of each block of M * N table . ; Function to compute all way to fill the boundary of all sides of the unit square ; Count possible ways to fill all upper and left side of the rectangle M * N ; Coun... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int CountWays ( int N , int M ) { int count = 1 ; count = pow ( 3 , M + N ) ; count *= pow ( 2 , M * N ) ; return count ; } int main ( ) { int N = 3 ; int M = 2 ; cout << CountWays ( N , M ) ; return 0 ; } |
Nth positive number whose absolute difference of adjacent digits is at most 1 | C ++ Program to find Nth number with absolute difference between all adjacent digits at most 1. ; Return Nth number with absolute difference between all adjacent digits at most 1. ; To store all such numbers ; Enqueue all integers from 1 to... | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findNthNumber ( int N ) { long long arr [ N + 1 ] ; queue < long long > q ; for ( int i = 1 ; i <= 9 ; i ++ ) q . push ( i ) ; for ( int i = 1 ; i <= N ; i ++ ) { arr [ i ] = q . front ( ) ; q . pop ( ) ; if ( arr [ i ] % 10 != 0 ) q . push ( arr [ i ] * 10 +... |
Unique element in an array where all elements occur K times except one | Set 2 | C ++ program for the above approach ; Function that find the unique element in the array arr [ ] ; Store all unique element in set ; Sum of all element of the array ; Sum of element in the set ; Print the unique element using formula ; Dri... | #include <bits/stdc++.h> NEW_LINE using namespace std ; int findUniqueElements ( int arr [ ] , int N , int K ) { unordered_set < int > s ( arr , arr + N ) ; int arr_sum = accumulate ( arr , arr + N , 0 ) ; int set_sum = accumulate ( s . begin ( ) , s . end ( ) , 0 ) ; cout << ( K * set_sum - arr_sum ) / ( K - 1 ) ; } i... |
Form the Cubic equation from the given roots | C ++ program for the approach ; Function to find the cubic equation whose roots are a , b and c ; Find the value of coefficient ; Print the equation as per the above coefficients ; Driver Code ; Function Call | #include <bits/stdc++.h> NEW_LINE using namespace std ; void findEquation ( int a , int b , int c ) { int X = ( a + b + c ) ; int Y = ( a * b ) + ( b * c ) + ( c * a ) ; int Z = a * b * c ; cout << " x ^ 3 β - β " << X << " x ^ 2 β + β " << Y << " x β - β " << Z << " β = β 0" ; } int main ( ) { int a = 5 , b = 2 , c = ... |
Gill 's 4th Order Method to solve Differential Equations | C ++ program to implement Gill 's method ; A sample differential equation " dy / dx β = β ( x β - β y ) /2" ; Finds value of y for a given x using step size h and initial value y0 at x0 ; Count number of iterations using step size or height h ; Value of K_i ; I... | #include <bits/stdc++.h> NEW_LINE using namespace std ; float dydx ( float x , float y ) { return ( x - y ) / 2 ; } float Gill ( float x0 , float y0 , float x , float h ) { int n = ( int ) ( ( x - x0 ) / h ) ; float k1 , k2 , k3 , k4 ; float y = y0 ; for ( int i = 1 ; i <= n ; i ++ ) { k1 = h * dydx ( x0 , y ) ; k2 = h... |
Program to print numbers from N to 1 in reverse order | C ++ program to print all numbers between 1 to N in reverse order ; Recursive function to print from N to 1 ; Driven Code | #include <bits/stdc++.h> NEW_LINE using namespace std ; void PrintReverseOrder ( int N ) { for ( int i = N ; i > 0 ; i -- ) cout << i << " β " ; } int main ( ) { int N = 5 ; PrintReverseOrder ( N ) ; return 0 ; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.