text
stringlengths
17
4.49k
code
stringlengths
49
5.46k
Check whether two numbers are in silver ratio | C ++ implementation to check whether two numbers are in silver ratio with each other ; Function to check that two numbers are in silver ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in silver ratio ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checksilverRatio ( float a , float b ) { if ( a < b ) swap ( a , b ) ; float ratio1 = ( ( a / b ) * 1000.0 ) / 1000.0 ; float ratio2 = ( int ) ( ( ( 2 * a + b ) / a ) * 1000 ) ; ratio2 = ratio2 / 1000 ; if ( ratio1 == ratio2 && ( int ) ( ratio1 - 2.414 ) == 0 ) { cout << " Yes STRNEWLINE " ; return true ; } else { cout << " No STRNEWLINE " ; return false ; } } int main ( ) { float a = 2.414 ; float b = 1 ; checksilverRatio ( a , b ) ; }
Nth term where K + 1 th term is product of Kth term with difference of max and min digit of Kth term | C ++ implementation to find the value of the given function for the value ; Function to find minimum digit in the decimal representation of N ; Loop to find the minimum digit in the number ; Function to find maximum digit in the decimal representation of N ; Loop to find the maximum digit in the number ; Function to find the value of the given function ; Loop to compute the values of the given number ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MIN ( int n ) { int ans = 11 ; while ( n ) { ans = min ( ans , n % 10 ) ; n /= 10 ; } return ans ; } int MAX ( int n ) { int ans = -1 ; while ( n ) { ans = max ( ans , n % 10 ) ; n /= 10 ; } return ans ; } void Find_value ( int n , int k ) { k -- ; int x = 0 ; int y = 0 ; while ( k -- ) { x = MIN ( n ) ; y = MAX ( n ) ; if ( y - x == 0 ) break ; n *= ( y - x ) ; } cout << n ; } int main ( ) { int N = 487 , D = 5 ; Find_value ( N , D ) ; return 0 ; }
Program to check if N is a Decagonal Number | C ++ program for the above approach ; Function to check if N is a Decagonal Number ; Condition to check if the number is a decagonal number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isdecagonal ( int N ) { float n = ( 3 + sqrt ( 16 * N + 9 ) ) / 8 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 10 ; if ( isdecagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Octadecagon number | C ++ program for the above approach ; Function to check if N is a Octadecagon Number ; Condition to check if the number is a Octadecagon number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isOctadecagon ( int N ) { float n = ( 14 + sqrt ( 128 * N + 196 ) ) / 32 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 18 ; if ( isOctadecagon ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Count of subsequences which consists exactly K prime numbers | C ++ implementation to find the count of subsequences which consist exactly K primes ; Returns factorial of n ; Function to return total number of combinations ; Function check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for finding number of subsequences which consists exactly K primes ; if number of primes are less thn k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } int countSubsequences ( int arr [ ] , int n , int k ) { int countPrime = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) countPrime ++ ; } if ( countPrime < k ) return 0 ; return nCr ( countPrime , k ) * pow ( 2 , ( n - countPrime ) ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int K = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubsequences ( arr , n , K ) ; return 0 ; }
Length of the longest alternating even odd subarray | C ++ program to find the Length of the longest alternating even odd subarray ; Function to find the longest subarray ; Length of longest alternating subarray ; Iterate in the array ; increment count if consecutive elements has an odd sum ; Store maximum count in longest ; Reinitialize cnt as 1 consecutive elements does not have an odd sum ; Length of ' longest ' can never be 1 since even odd has to occur in pair or more so return 0 if longest = 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestEvenOddSubarray ( int a [ ] , int n ) { int longest = 1 ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] + a [ i + 1 ] ) % 2 == 1 ) { cnt ++ ; } else { longest = max ( longest , cnt ) ; cnt = 1 ; } } if ( longest == 1 ) return 0 ; return max ( cnt , longest ) ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 , 7 , 8 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << longestEvenOddSubarray ( a , n ) ; return 0 ; }
Check whether given number N is a Moran Number or not | C ++ implementation to check if the number is Moran number ; Function to calculate digit sum ; Function to check if number is prime ; Function to check if number is moran number ; Calculate digit sum ; Check if n is completely divisible by digit sum ; Calculate the quotient ; Check if the number is prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digSum ( int a ) { int sum = 0 ; while ( a ) { sum += a % 10 ; a = a / 10 ; } return sum ; } bool isPrime ( int r ) { bool s = true ; for ( int i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } void moranNo ( int n ) { int dup = n ; int sum = digSum ( dup ) ; if ( n % sum == 0 ) { int c = n / sum ; if ( isPrime ( c ) ) { cout << " Yes " ; return ; } } cout << " No " << endl ; } int main ( ) { int n = 21 ; moranNo ( n ) ; return 0 ; }
Program to check if N is a Hendecagonal Number | C ++ program for the above approach ; Function to check if N is a Hendecagonal Number ; Condition to check if the number is a hendecagonal number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ishendecagonal ( int N ) { float n = ( 7 + sqrt ( 72 * N + 49 ) ) / 18 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 11 ; if ( ishendecagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Hexadecagonal Number | C ++ program for the above approach ; Function to check if N is a hexadecagonal number ; Condition to check if the number is a hexadecagonal number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ishexadecagonal ( int N ) { float n = ( 12 + sqrt ( 112 * N + 144 ) ) / 28 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 16 ; if ( ishexadecagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Nonagonal Number | C ++ program for the above approach ; Function to check if N is a is a Nonagonal Number ; Condition to check if the number is a nonagonal number ; Driver Code ; Given Number ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isnonagonal ( int N ) { float n = ( 5 + sqrt ( 56 * N + 25 ) ) / 14 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 9 ; if ( isnonagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Count of decreasing pairs formed from numbers 1 to N | C ++ program to count decreasing pairs formed from numbers 1 to N ; Function to count the possible number of pairs ; if the number is even then the answer in ( N / 2 ) - 1 ; if the number is odd then the answer in N / 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divParts ( int N ) { if ( N % 2 == 0 ) cout << ( N / 2 ) - 1 << endl ; else cout << N / 2 << endl ; } int main ( ) { int N = 8 ; divParts ( N ) ; return 0 ; }
Count the minimum steps to reach 0 from the given integer N | C ++ program to Count the minimum steps to reach 0 from the given integer N ; Function returns min step to reach 0 from N ; Direct possible reduction of value N ; Remaining steps needs to be reduced by 1 ; Summation of both the values ; Return the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinSteps ( int n , int jump ) { int quotient = n / jump ; int remainder = n % jump ; int steps = quotient + remainder ; return steps ; } int main ( ) { int N = 6 , K = 3 ; cout << getMinSteps ( N , K ) ; return 0 ; }
Construct an Array of size N whose sum of cube of all elements is a perfect square | C ++ implementation of the above approach ; Function to construct an array of size N ; Prints the first N natural numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArray ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { cout << i << " ▁ " ; } } int main ( ) { int N = 5 ; constructArray ( N ) ; return 0 ; }
Program to check if N is a Centered Decagonal Number | C ++ program for the above approach ; Function to check if number N is a Centered decagonal number ; Condition to check if N is Centered Decagonal Number ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCentereddecagonal ( int N ) { float n = ( 5 + sqrt ( 20 * N + 5 ) ) / 10 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int N = 11 ; if ( isCentereddecagonal ( N ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
C / C ++ program to add N distances given in inch | C ++ program for the above approach ; Struct defined for the inch - feet system ; Variable to store the inch - feet ; Function to find the sum of all N set of Inch Feet distances ; Variable to store sum ; Traverse the InchFeet array ; Find the total sum of feet and inch ; If inch sum is greater than 11 ; Find integral part of inch_sum ; Delete the integral part x ; Add x % 12 to inch_sum ; Add x / 12 to feet_sum ; Print the corresponding sum of feet_sum and inch_sum ; Driver Code ; Given a set of inch - feet ; Function Call
#include " iostream " NEW_LINE using namespace std ; struct InchFeet { int feet ; float inch ; } ; void findSum ( InchFeet arr [ ] , int N ) { int feet_sum = 0 ; float inch_sum = 0.0 ; int x ; for ( int i = 0 ; i < N ; i ++ ) { feet_sum += arr [ i ] . feet ; inch_sum += arr [ i ] . inch ; } if ( inch_sum >= 12 ) { int x = ( int ) inch_sum ; inch_sum -= x ; inch_sum += x % 12 ; feet_sum += x / 12 ; } cout << " Feet ▁ Sum : ▁ " << feet_sum << ' ' << " Inch ▁ Sum : ▁ " << inch_sum << endl ; } int main ( ) { InchFeet arr [ ] = { { 10 , 3.7 } , { 10 , 5.5 } , { 6 , 8.0 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findSum ( arr , N ) ; return 0 ; }
Count Sexy Prime Pairs in the given array | C ++ program to count Sexy Prime pairs in array ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are SPP ( Sexy Prime Pair ) or not ; Function to find SPP ( Sexy Prime Pair ) pairs from the given array ; Iterate through all pairs ; Increment count if SPP ( Sexy Prime Pair ) pair ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) pair
#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 += 6 ) { if ( n % i == 0 || n % ( i + 6 ) == 0 ) { return false ; } } return true ; } bool SexyPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && abs ( n1 - n2 ) == 6 ) ; } int countSexyPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( SexyPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 6 , 7 , 5 , 11 , 13 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSexyPairs ( arr , n ) ; return 0 ; }
Find the K | C ++ program to Find the kth Permutation Sequence of first n natural numbers ; Function to find the index of number at first position of kth sequence of set of size n ; n_actual_fact = n ! ; First position of the kth sequence will be occupied by the number present at index = k / ( n - 1 ) ! ; Function to find the kth permutation of n numbers ; Store final answer ; Insert all natural number upto n in set ; Mark the first position ; subtract 1 to get 0 based indexing ; itr now points to the number at index in set s ; remove current number from the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findFirstNumIndex ( int & k , int n ) { if ( n == 1 ) return 0 ; n -- ; int first_num_index ; int n_partial_fact = n ; while ( k >= n_partial_fact && n > 1 ) { n_partial_fact = n_partial_fact * ( n - 1 ) ; n -- ; } first_num_index = k / n_partial_fact ; k = k % n_partial_fact ; return first_num_index ; } string findKthPermutation ( int n , int k ) { string ans = " " ; set < int > s ; for ( int i = 1 ; i <= n ; i ++ ) s . insert ( i ) ; set < int > :: iterator itr ; itr = s . begin ( ) ; k = k - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int index = findFirstNumIndex ( k , n - i ) ; advance ( itr , index ) ; ans += ( to_string ( * itr ) ) ; s . erase ( itr ) ; itr = s . begin ( ) ; } return ans ; } int main ( ) { int n = 3 , k = 4 ; string kth_perm_seq = findKthPermutation ( n , k ) ; cout << kth_perm_seq << endl ; return 0 ; }
Find the Largest N digit perfect square number in Base B | C ++ implementation to find Largest N digit perfect square number in Base B ; Function to find the largest N digit number ; Largest n - digit perfect square ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nDigitPerfectSquares ( int n , int b ) { int largest = pow ( ceil ( sqrt ( pow ( b , n ) ) ) - 1 , 2 ) ; cout << largest ; } int main ( ) { int N = 1 , B = 8 ; nDigitPerfectSquares ( N , B ) ; return 0 ; }
Find Cube root of a number using Log function | C ++ program to Find Cube root of a number using Logarithm ; Function to find the cube root ; calculate the cube root ; Return the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE double cubeRoot ( double n ) { double ans = pow ( 3 , ( 1.0 / 3 ) * ( log ( n ) / log ( 3 ) ) ) ; return ans ; } int main ( ) { double N = 8 ; printf ( " % .2lf ▁ " , cubeRoot ( N ) ) ; return 0 ; }
Find the maximum possible value for the given periodic function | C ++ Program to find the maximum possible value for the given function ; Function to return the maximum value of f ( x ) ; Driver code
#include <iostream> NEW_LINE using namespace std ; int floorMax ( int A , int B , int N ) { int x = min ( B - 1 , N ) ; return ( A * x ) / B ; } int main ( ) { int A = 11 , B = 10 , N = 9 ; cout << floorMax ( A , B , N ) ; return 0 ; }
Minimum moves taken to move coin of each cell to any one cell of Matrix | C ++ program to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; Function to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; Initializing count to 0 ; Number of layers that are around the centre element ; Iterating over ranger of layers ; Increase the value of count by 8 * k * k ; Driver code
#include <iostream> NEW_LINE using namespace std ; int calculateMoves ( int n ) { int count = 0 ; int layers = n / 2 ; for ( int k = 1 ; k < layers + 1 ; k ++ ) { count += 8 * k * k ; } return count ; } int main ( ) { int N = 5 ; cout << calculateMoves ( N ) ; }
Check if N can be represented as sum of squares of two consecutive integers | C ++ implementation to check that a number is sum of squares of 2 consecutive numbers or not ; Function to check that the a number is sum of squares of 2 consecutive numbers or not ; Condition to check if the a number is sum of squares of 2 consecutive numbers or not ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSumSquare ( int N ) { float n = ( 2 + sqrt ( 8 * N - 4 ) ) / 2 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 13 ; if ( isSumSquare ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Centered heptagonal number | C ++ implementation to check that a number is a Centered heptagonal number or not ; Function to check that the number is a Centered heptagonal number ; Condition to check if the number is a Centered heptagonal number ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCenteredheptagonal ( int N ) { float n = ( 7 + sqrt ( 56 * N - 7 ) ) / 14 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int n = 8 ; if ( isCenteredheptagonal ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Centered nonadecagonal number | C ++ implementation to check that a number is a Centered nonadecagonal number or not ; Function to check that the number is a Centered nonadecagonal number ; Condition to check if the number is a Centered nonadecagonal number ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCenterednonadecagonal ( int N ) { float n = ( 19 + sqrt ( 152 * N + 209 ) ) / 38 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int n = 20 ; if ( isCenterednonadecagonal ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Program to check if N is a Centered Octadecagonal number | C ++ implementation to check that a number is a Centered Octadecagonal number or not ; Function to check that the number is a Centered Octadecagonal number ; Implement the formula generated ; Condition to check if the number is a Centered Octadecagonal number ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCenteredOctadecagonal ( int N ) { float n = ( 9 + sqrt ( 36 * N + 45 ) ) / 18 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int n = 19 ; if ( isCenteredOctadecagonal ( n ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Logarithm tricks for Competitive Programming | C ++ implementation to find Kth root of the number ; Function to find the Kth root of the number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double kthRoot ( double n , int k ) { return pow ( k , ( 1.0 / k ) * ( log ( n ) / log ( k ) ) ) ; } int main ( ) { double N = 8.0 ; int K = 3 ; cout << kthRoot ( N , K ) ; return 0 ; }
Logarithm tricks for Competitive Programming | C ++ implementation to check if the number is power of K ; Function to check if the number is power of K ; logarithm function to calculate value ; compare to the result1 or result2 both are equal ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int N , int K ) { int res1 = log ( N ) / log ( K ) ; double res2 = log ( N ) / log ( K ) ; return ( res1 == res2 ) ; } int main ( ) { int N = 8 ; int K = 2 ; if ( isPower ( N , K ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Count of subarrays which start and end with the same element | C ++ program to Count total sub - array which start and end with same element ; function to find total sub - array which start and end with same element ; initialize result with 0 ; array to count frequency of 1 to N ; update frequency of A [ i ] ; update result with sub - array contributed by number i ; print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void cntArray ( int A [ ] , int N ) { int result = 0 ; int frequency [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { frequency [ A [ i ] ] ++ ; } for ( int i = 1 ; i <= N ; i ++ ) { int frequency_of_i = frequency [ i ] ; result += + ( ( frequency_of_i ) * ( frequency_of_i + 1 ) ) / 2 ; } cout << result << endl ; } int main ( ) { int A [ ] = { 1 , 5 , 6 , 1 , 9 , 5 , 8 , 10 , 8 , 9 } ; int N = sizeof ( A ) / sizeof ( int ) ; cntArray ( A , N ) ; return 0 ; }
Length of array pair formed where one contains all distinct elements and other all same elements | C ++ implementation to Divide the array into two arrays having same size , one with distinct elements and other with same elements ; Function to find the max size possible ; Counting the maximum frequency ; Counting total distinct elements ; Find max of both the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSize ( int a [ ] , int n ) { vector < int > frq ( n + 1 ) ; for ( int i = 0 ; i < n ; ++ i ) frq [ a [ i ] ] ++ ; int maxfrq = * max_element ( frq . begin ( ) , frq . end ( ) ) ; int dist = n + 1 - count ( frq . begin ( ) , frq . end ( ) , 0 ) ; int ans1 = min ( maxfrq - 1 , dist ) ; int ans2 = min ( maxfrq , dist - 1 ) ; int ans = max ( ans1 , ans2 ) ; return ans ; } int main ( ) { int arr [ ] = { 4 , 2 , 4 , 1 , 4 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxSize ( arr , n ) ; return 0 ; }
Longest subarray with all elements same | C ++ program to find largest subarray with all equal elements . ; Function to find largest sub array with all equal elements . ; Traverse the array ; If element is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subarray ( int arr [ ] , int n ) { int ans = 1 , temp = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { ++ temp ; } else { ans = max ( ans , temp ) ; temp = 1 ; } } ans = max ( ans , temp ) ; return ans ; } int main ( ) { int arr [ ] = { 2 , 2 , 1 , 1 , 2 , 2 , 2 , 3 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << subarray ( arr , n ) ; return 0 ; }
Min and max length subarray having adjacent element difference atmost K | C ++ program to find the minimum and maximum length subarray such that difference between adjacent elements is atmost K ; Function to find the maximum and minimum length subarray ; Initialise minimum and maximum size of subarray in worst case ; Left will scan the size of possible subarray in left of selected element ; Right will scan the size of possible subarray in right of selected element ; Temp will store size of group associateed with every element ; Loop to find size of subarray for every element of array ; Left will move in left direction and compare difference between itself and element left to it ; right will move in right direction and compare difference between itself and element right to it ; if subarray of much lesser or much greater is found than yet known then update ; Print minimum and maximum possible size of subarray ; Driver Code ; function call to find maximum and minimum possible sub array
#include <iostream> NEW_LINE using namespace std ; void findMaxMinSubArray ( int arr [ ] , int K , int n ) { int min = n ; int max = 0 ; int left ; int right ; int tmp ; for ( int i = 0 ; i < n ; i ++ ) { tmp = 1 ; left = i ; while ( left - 1 >= 0 && abs ( arr [ left ] - arr [ left - 1 ] ) <= K ) { left -- ; tmp ++ ; } right = i ; while ( right + 1 <= n - 1 && abs ( arr [ right ] - arr [ right + 1 ] ) <= K ) { right ++ ; tmp ++ ; } if ( min > tmp ) min = tmp ; if ( max < tmp ) max = tmp ; } cout << min << " , ▁ " << max << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 6 , 7 } ; int K = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMaxMinSubArray ( arr , K , n ) ; return 0 ; }
Count of elements which is the sum of a subarray of the given Array | C ++ implementation to count the elements such that their exist a subarray whose sum is equal to this element ; Function to count the elements such that their exist a subarray whose sum is equal to this element ; Loop to count the frequency ; Loop to iterate over every possible subarray of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countElement ( int arr [ ] , int n ) { map < int , int > freq ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { int tmpsum = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { tmpsum += arr [ j ] ; if ( freq . find ( tmpsum ) != freq . end ( ) ) { ans += freq [ tmpsum ] ; } } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countElement ( arr , n ) << endl ; return 0 ; }
Nth root of a number using log | C ++ implementation to find the Kth root of a number using log ; Function to find the Kth root of the number using log function ; Driver Code
#include <bits/stdc++.h> NEW_LINE double kthRoot ( double n , int k ) { return pow ( k , ( 1.0 / k ) * ( log ( n ) / log ( k ) ) ) ; } int main ( void ) { double n = 81 ; int k = 4 ; printf ( " % lf ▁ " , kthRoot ( n , k ) ) ; return 0 ; }
Make A , B and C equal by adding total value N to them | C ++ program to distribute integer N among A , B , C such that they become equal ; Find maximum among the three elements ; Summation of three elements ; Check if difference is divisible by 3 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void distributeN ( int A , int B , int C , int n ) { int max1 = max ( A , B ) ; int max2 = max ( B , C ) ; int maximum = max ( max1 , max2 ) ; int sum = A + B + C ; int p = ( 3 * maximum ) - sum ; int diff = n - p ; if ( diff < 0 diff % 3 ) cout << " No " ; else cout << " Yes " ; } int main ( ) { int A = 10 , B = 20 ; int C = 15 , n = 14 ; distributeN ( A , B , C , n ) ; return 0 ; }
Represent N as sum of K odd numbers with repetitions allowed | C ++ implementation to represent N as sum of K even numbers ; Function to print the representation ; N must be greater than equal to 2 * K and must be odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOddNumbers ( int N , int K ) { int check = N - ( K - 1 ) ; if ( check > 0 && check % 2 == 1 ) { for ( int i = 0 ; i < K - 1 ; i ++ ) { cout << "1 ▁ " ; } cout << check ; } else cout << " - 1" ; } int main ( ) { int N = 5 ; int K = 3 ; sumOddNumbers ( N , K ) ; return 0 ; }
Number of continuous reductions of A from B or B from A to make them ( 1 , 1 ) | C ++ implementation to find the minimum number of operations required to reach ( 1 , 1 ) ; Function to find the minimum number of steps required ; Condition to check if it is not possible to reach ; Condition to check if the pair is reached to 1 , 1 ; Condition to change the A as the maximum element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSteps ( int a , int b , int c ) { if ( a < 1 b < 1 ) return -1 ; if ( a == 1 && b == 1 ) return c ; if ( a < b ) { a = a + b ; b = a - b ; a = a - b ; } return minimumSteps ( a - b , b , c + 1 ) ; } int main ( ) { int a = 75 ; int b = 17 ; cout << minimumSteps ( a , b , 0 ) << endl ; }
Longest Mountain Subarray | C ++ code for Longest Mountain Subarray ; Function to find the longest mountain subarray ; If the size of array is less than 3 , the array won 't show mountain like behaviour ; When a new mountain sub - array is found , there is a need to set the variables k , j to - 1 in order to help calculate the length of new mountain sub - array ; j marks the starting index of a new mountain sub - array . So set the value of j to current index i . ; Checks if next element is less than current element ; Checks if starting element exists or not , if the starting element of the mountain sub - array exists then the index of ending element is stored in k ; This condition checks if both starting index and ending index exists or not , if yes , the length is calculated . ; d holds the length of the longest mountain sub - array . If the current length is greater than the calculated length , then value of d is updated . ; ignore if there is no increase or decrease in the value of the next element ; Checks and calculates the length if last element of the array is the last element of a mountain sub - array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LongestMountain ( vector < int > & a ) { int i = 0 , j = -1 , k = -1 , p = 0 , d = 0 , n = 0 ; if ( a . size ( ) < 3 ) { return 0 ; } for ( i = 0 ; i < a . size ( ) - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] ) { if ( k != -1 ) { k = -1 ; j = -1 ; } if ( j == -1 ) { j = i ; } } else { if ( a [ i + 1 ] < a [ i ] ) { if ( j != -1 ) { k = i + 1 ; } if ( k != -1 && j != -1 ) { if ( d < k - j + 1 ) { d = k - j + 1 ; } } } else { k = -1 ; j = -1 ; } } } if ( k != -1 && j != -1 ) { if ( d < k - j + 1 ) { d = k - j + 1 ; } } return d ; } int main ( ) { vector < int > d = { 1 , 3 , 1 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 7 , 6 , 5 } ; cout << LongestMountain ( d ) << endl ; return 0 ; }
Check whether an Array can be made 0 by splitting and merging repeatedly | C ++ program for the above approach ; Function that finds if it is possible to make the array contain only 1 element i . e . 0 ; Check if element is odd ; According to the logic in above approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string solve ( vector < int > & A ) { int i , ctr = 0 ; for ( i = 0 ; i < A . size ( ) ; i ++ ) { if ( A [ i ] % 2 ) { ctr ++ ; } } if ( ctr % 2 ) { return " No " ; } else { return " Yes " ; } } int main ( ) { vector < int > arr = { 9 , 17 } ; cout << solve ( arr ) << endl ; return 0 ; }
Count of perfect squares of given length | C ++ Program to count perfect squares of given length ; Function to check if a number is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to return the count of n digit perfect squares ; Initialize result ; Traverse through all numbers of n digits ; Check if current number ' i ' is perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ; } int countSquares ( int n ) { int cnt = 0 ; for ( int i = pow ( 10 , ( n - 1 ) ) ; i < pow ( 10 , n ) ; i ++ ) { if ( i != 0 && isPerfectSquare ( i ) ) cnt ++ ; } return cnt ; } int main ( ) { int n = 3 ; cout << countSquares ( n ) ; return 0 ; }
Count of perfect squares of given length | C ++ Program to count perfect squares of given length ; Function to return the count of n digit perfect squares ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSquares ( int n ) { int r = ceil ( sqrt ( pow ( 10 , n ) ) ) ; int l = ceil ( sqrt ( pow ( 10 , n - 1 ) ) ) ; return r - l ; } int main ( ) { int n = 3 ; cout << countSquares ( n ) ; return 0 ; }
Sum of alternating sign cubes of first N Natural numbers | C ++ implementation to compute the sum of cubes with alternating sign ; Function to compute sum of the cubes with alternating sign ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int summation ( int N ) { int co = ( N + 1 ) / 2 ; int ce = ( N ) / 2 ; int se = 2 * ( ( ce * ( ce + 1 ) ) * ( ce * ( ce + 1 ) ) ) ; int so = ( co * co ) * ( 2 * ( ( co * co ) ) - 1 ) ; return so - se ; } int main ( ) { int n = 3 ; cout << summation ( n ) ; return 0 ; }
Program to check if N is a Star Number | C ++ implementation to check that a number is a star number or not ; Function to check that the number is a star number ; Condition to check if the number is a star number ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isStar ( int N ) { float n = ( 6 + sqrt ( 24 * N + 12 ) ) / 6 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 13 ; if ( isStar ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Smallest number to make Array sum at most K by dividing each element | C ++ program to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Function to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Binary search between 1 and 10 ^ 9 ; Calculating the new sum after dividing every element by mid ; If after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; Else , move mid + 1 to high ; Returning the minimum number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDivisor ( int arr [ ] , int n , int limit ) { int low = 0 , high = 1e9 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ceil ( ( double ) arr [ i ] / ( double ) mid ) ; } if ( sum <= limit ) high = mid ; else low = mid + 1 ; } return low ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 6 ; cout << findMinDivisor ( arr , N , K ) ; }
Count of triplets ( a , b , c ) in the Array such that a divides b and b divides c | C ++ program to find count of triplets ( a , b , c ) in the Array such that a divides b and b divides c ; Function to count triplets ; Iterate for middle element ; Iterate left array for a [ i ] ; Iterate right array for a [ k ] ; return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int arr [ ] , int n ) { int count = 0 ; for ( int j = 1 ; j < n - 1 ; j ++ ) { int p = 0 , q = 0 ; for ( int i = 0 ; i < j ; i ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) p ++ ; } for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ k ] % arr [ j ] == 0 ) q ++ ; } count += p * q ; } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getCount ( arr , N ) << endl ; return 0 ; }
Find pair with maximum ratio in an Array | C ++ implementation to find the maximum pair in the array ; Function to find the maximum pair possible for the array ; Loop to iterate over every possible pair in the array ; Check pair ( x , y ) as well as ( y , x ) for maximum value ; Update the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float computeMaxValue ( float arr [ ] , int n ) { float ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { float val = max ( arr [ i ] / arr [ j ] , arr [ j ] / arr [ i ] ) ; ans = max ( ans , val ) ; } } return ans ; } int main ( ) { float arr [ ] = { 15 , 10 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << computeMaxValue ( arr , n ) ; return 0 ; }
Find the Kth number which is not divisible by N | C ++ implementation to find the K 'th non-divisible number of N ; Function to find the Kth not divisible by N ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kthNonDivisible ( int N , int K ) { return K + floor ( ( K - 1 ) / ( N - 1 ) ) ; } int main ( ) { int N = 3 ; int K = 6 ; cout << kthNonDivisible ( N , K ) ; return 0 ; }
Find the maximum sum pair in an Array with even parity | C ++ implementation to find a pair with even parity and maximum sum ; Function that returns true if count of set bits in given number is even ; Parity will store the count of set bits ; Function to print the elements of the array ; Function to remove all the even parity elements from the given array ; Traverse the array ; If the current element has even parity ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sz = 1e3 ; bool isEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x & 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } void printArray ( int arr [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { cout << arr [ i ] << ' ▁ ' ; } } void findPairEvenParity ( int arr [ ] , int len ) { int firstMaximum = INT_MIN ; int secondMaximum = INT_MIN ; for ( int i = 0 ; i < len ; i ++ ) { if ( isEvenParity ( arr [ i ] ) ) { if ( arr [ i ] >= firstMaximum ) { secondMaximum = firstMaximum ; firstMaximum = arr [ i ] ; } else if ( arr [ i ] >= secondMaximum ) { secondMaximum = arr [ i ] ; } } } cout << firstMaximum << " ▁ " << secondMaximum ; } int main ( ) { int arr [ ] = { 18 , 15 , 8 , 9 , 14 } ; int len = sizeof ( arr ) / sizeof ( int ) ; findPairEvenParity ( arr , len ) ; return 0 ; }
Program to check if N is a Hexagonal Number or not | C ++ Program to check if N is a Hexagonal Number ; Function to check if number is hexagonal ; Calculate the value for n ; Check if n - floor ( n ) is equal to 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isHexagonal ( int N ) { float val = 8 * N + 1 ; float x = 1 + sqrt ( val ) ; float n = ( x ) / 4 ; if ( ( n - ( int ) n ) == 0 ) return true ; else return false ; } int main ( ) { int N = 14 ; if ( isHexagonal ( N ) == true ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Program to convert Number in characters | C ++ program to convert number in characters ; To calculate the reverse of the number ; The remainder will give the last digit of the number ; Extract the first digit of the reversed number ; Match it with switch case ; Divide the number by 10 to get the next number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void NumbertoCharacter ( int n ) { int rev = 0 , r = 0 ; while ( n > 0 ) { r = n % 10 ; rev = rev * 10 + r ; n = n / 10 ; } while ( rev > 0 ) { r = rev % 10 ; switch ( r ) { case 1 : cout << " one ▁ " ; break ; case 2 : cout << " two ▁ " ; break ; case 3 : cout << " three ▁ " ; break ; case 4 : cout << " four ▁ " ; break ; case 5 : cout << " five ▁ " ; break ; case 6 : cout << " six ▁ " ; break ; case 7 : cout << " seven ▁ " ; break ; case 8 : cout << " eight ▁ " ; break ; case 9 : cout << " nine ▁ " ; break ; case 0 : cout << " zero ▁ " ; break ; default : cout << " UnValid ▁ " ; break ; } rev = rev / 10 ; } } #include <iostream> NEW_LINE int main ( ) { int n = 12345 ; NumbertoCharacter ( n ) ; return 0 ; }
Count all subarrays whose sum can be split as difference of squares of two Integers | C ++ program to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; Function to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; Loop to iterate over all the possible subsequences of the array ; Finding the sum of all the possible subsequences ; Condition to check whether the number can be split as difference of squares ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Solve ( int arr [ ] , int n ) { int temp = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { temp = 0 ; for ( int j = i ; j < n ; j ++ ) { temp += arr [ j ] ; if ( ( temp + 2 ) % 4 != 0 ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << Solve ( arr , N ) ; return 0 ; }
Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M | C ++ program to find the product of all the combinations of M elements from an array whose index in the sorted order divides M completely ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; If y is odd , multiply x with result ; y must be even now ; Iterative Function to calculate ( nCr ) % p and save in f [ n ] [ r ] C ( n , r ) % p = [ C ( n - 1 , r - 1 ) % p + C ( n - 1 , r ) % p ] % p and C ( n , 0 ) = C ( n , n ) = 1 ; If j > i then C ( i , j ) = 0 ; If i is equal to j then C ( i , j ) = 1 ; C ( i , j ) = ( C ( i - 1 , j ) + C ( i - 1 , j - 1 ) ) % p ; Initialize the answer ; For every element arr [ i ] , x is count of occurrence of arr [ i ] in different set such that index of arr [ i ] in those sets divides m completely . ; Finding the count of arr [ i ] by placing it at the index which divides m completely ; Using fermat 's little theorem ; Multiplying with the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int lli ; const int m = 4 ; long long int power ( lli x , lli y , lli p ) { lli res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } void nCr ( lli n , lli p , lli f [ ] [ m + 1 ] ) { for ( lli i = 0 ; i <= n ; i ++ ) { for ( lli j = 0 ; j <= m ; j ++ ) { if ( j > i ) f [ i ] [ j ] = 0 ; else if ( j == 0 j == i ) f [ i ] [ j ] = 1 ; else f [ i ] [ j ] = ( f [ i - 1 ] [ j ] + f [ i - 1 ] [ j - 1 ] ) % p ; } } } void operations ( lli arr [ ] , lli n , lli f [ ] [ m + 1 ] ) { lli p = 1000000007 ; nCr ( n , p - 1 , f ) ; sort ( arr , arr + n ) ; lli ans = 1 ; for ( lli i = 0 ; i < n ; i ++ ) { long long int x = 0 ; for ( lli j = 1 ; j <= m ; j ++ ) { if ( m % j == 0 ) x = ( x + ( f [ n - i - 1 ] [ m - j ] * f [ i ] [ j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ; } ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) ; } cout << ans << endl ; } int main ( ) { lli arr [ ] = { 4 , 5 , 7 , 9 , 3 } ; lli n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; lli f [ n + 1 ] [ m + 1 ] ; operations ( arr , n , f ) ; }
Check whether a number can be represented by the product of two squares | C ++ implementation to Check whether a number can be represented by the product of two squares ; Function to check if there exist two numbers product of whose squares is n . ; check whether the product of the square of both numbers is equal to N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prodSquare ( int n ) { for ( long i = 2 ; i * i <= n ; i ++ ) for ( long j = 2 ; j <= n ; j ++ ) if ( i * i * j * j == n ) return true ; return false ; } int main ( ) { int n = 25 ; if ( prodSquare ( n ) ) cout << " Yes " ; else cout << " No " ; }
Check whether a number can be represented by the product of two squares | C ++ implementation to Check whether a number can be represented by the product of two squares ; Function to check if there exist two numbers product of whose squares is n ; Initialize map ; Store square value in hashmap ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prodSquare ( int n ) { unordered_map < float , float > s ; for ( int i = 2 ; i * i <= n ; ++ i ) { s [ i * i ] = 1 ; if ( s . find ( n / ( i * i ) ) != s . end ( ) ) return true ; } return false ; } int main ( ) { int n = 25 ; if ( prodSquare ( n ) ) cout << " Yes " ; else cout << " No " ; }
Print N distinct numbers following the given operations | C ++ implementation to Print N distinct numbers ; Function to print the required array ; Check if number is a multiple of 4 ; Printing Left Half of the array ; Printing Right Half of the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool printArr ( int n ) { if ( n % 4 == 0 ) { for ( int i = 1 ; i <= n / 2 ; i ++ ) cout << i * 2 << ' ▁ ' ; for ( int i = 1 ; i < n / 2 ; i ++ ) cout << i * 2 - 1 << ' ▁ ' ; cout << n + n / 2 - 1 << ' ' ; } else cout << " - 1" ; } int main ( ) { int n = 22 ; printArr ( n ) ; return 0 ; }
Sum of product of all subsets formed by only divisors of N | C ++ program to find the sum of product of all the subsets formed by only divisors of N ; Function to find the sum of product of all the subsets formed by only divisors of N ; Vector to store all the divisors of n ; Loop to find out the divisors of N ; Both ' i ' and ' n / i ' are the divisors of n ; Check if ' i ' and ' n / i ' are equal or not ; Calculating the answer ; Excluding the value of the empty set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GetSum ( int n ) { vector < int > divisors ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { divisors . push_back ( i ) ; if ( i != n / i ) { divisors . push_back ( n / i ) ; } } } int ans = 1 ; for ( auto i : divisors ) { ans *= ( i + 1 ) ; } ans = ans - 1 ; return ans ; } int main ( ) { int N = 4 ; cout << GetSum ( N ) << endl ; }
Length of longest Powerful number subsequence in an Array | C ++ program to find the length of Longest Powerful Subsequence in an Array ; 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 a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to find the longest subsequence which contain all powerful numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } int longestPowerfulSubsequence ( int arr [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) answer ++ ; } return answer ; } int main ( ) { int arr [ ] = { 6 , 4 , 10 , 13 , 9 , 25 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestPowerfulSubsequence ( arr , n ) << endl ; return 0 ; }
Maximum OR value of a pair in an Array without using OR operator | C ++ implementation of the approach ; Function to return the maximum bitwise OR for any pair of the given array without using bitwise OR operation ; find maximum element in the array ; finding complement will set all unset bits in a number ; iterate through all other array elements to find maximum AND value ; c will give the maximum value that could be added to max_value to produce maximum OR value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOR ( int arr [ ] , int n ) { int max_value = * max_element ( arr , arr + n ) ; int number_of_bits = floor ( log2 ( max_value ) ) + 1 ; int complement = ( ( 1 << number_of_bits ) - 1 ) ^ max_value ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != max_value ) { c = max ( c , ( complement & arr [ i ] ) ) ; } } return ( max_value + c ) ; } int main ( ) { int arr [ ] = { 3 , 6 , 8 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxOR ( arr , n ) ; return 0 ; }
Find the XOR of the elements in the given range [ L , R ] with the value K for a given set of queries | C ++ implementation to perform the XOR range updates on an array ; Function to perform the update operation on the given array ; Converting the indices to 0 indexing . ; Saving the XOR of K from the starting index in the range [ L , R ] . ; Saving the XOR of K at the ending index in the given [ L , R ] . ; Function to display the resulting array ; Finding the resultant value in the result array ; Combining the effects of the updates with the original array without changing the initial array . ; Driver code ; Query 1 ; Query 2 ; Query 3
#include <bits/stdc++.h> NEW_LINE using namespace std ; void update ( int res [ ] , int L , int R , int K ) { L -= 1 ; R -= 1 ; res [ L ] ^= K ; res [ R + 1 ] ^= K ; } void display ( int arr [ ] , int res [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { res [ i ] = res [ i ] ^ res [ i - 1 ] ; } for ( int i = 0 ; i < n ; i ++ ) { cout << ( arr [ i ] ^ res [ i ] ) << " ▁ " ; } cout << endl ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 8 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int res [ N ] ; memset ( res , 0 , sizeof ( res ) ) ; int L = 1 , R = 3 , K = 2 ; update ( res , L , R , K ) ; L = 2 ; R = 4 ; K = 3 ; update ( res , L , R , K ) ; display ( arr , res , N ) ; return 0 ; }
Shortest subarray to be removed to make all Array elements unique | C ++ program to make array elements pairwise distinct by removing at most one subarray of minimum length ; Function to check if elements of Prefix and suffix of each sub array of size K are pairwise distinct or not ; Hash map to store frequencies of elements of prefix and suffix ; Variable to store number of occurrences of an element other than one ; Adding frequency of elements of suffix to hash for subarray starting from first index There is no prefix for this sub array ; Counting extra elements in current Hash map ; If there are no extra elements return true ; Check for remaining sub arrays ; First element of suffix is now part of subarray which is being removed so , check for extra elements ; Decrement frequency of first element of the suffix ; Increment frequency of last element of the prefix ; Check for extra elements ; If there are no extra elements return true ; Function for calculating minimum length of the subarray , which on removing make all elements pairwise distinct ; Possible range of length of subarray ; Binary search to find minimum ans ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int n , int k ) { map < int , int > m ; int extra = 0 ; for ( int i = k ; i < n ; i ++ ) m [ a [ i ] ] ++ ; for ( auto x : m ) extra += x . second - 1 ; if ( extra == 0 ) return true ; for ( int i = 1 ; i + k - 1 < n ; i ++ ) { if ( m [ a [ i + k - 1 ] ] > 1 ) extra -- ; m [ a [ i + k - 1 ] ] -- ; m [ a [ i - 1 ] ] ++ ; if ( m [ a [ i - 1 ] ] > 1 ) extra ++ ; if ( extra == 0 ) return true ; } return false ; } int minlength ( int a [ ] , int n ) { int lo = 0 , hi = n + 1 ; int ans = 0 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( check ( a , n , mid ) ) { ans = mid ; hi = mid ; } else lo = mid + 1 ; } return ans ; } int main ( ) { int a [ 5 ] = { 1 , 2 , 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( int ) ; cout << minlength ( a , n ) ; }
Find N distinct integers with zero sum | C ++ implementation to Print N distinct numbers such that their sum is 0 ; Function to print distinct n numbers such that their sum is 0 ; Print 2 symmetric numbers ; print a extra 0 if N is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int N ) { for ( int i = 1 ; i <= N / 2 ; i ++ ) { cout << i << " , ▁ " << - i << " , ▁ " ; } if ( N % 2 == 1 ) cout << 0 ; } int main ( ) { int N = 10 ; findNumbers ( N ) ; }
Program to calculate Electricity Bill | C ++ implementation to calculate the electricity bill ; Function to calculate the electricity bill ; Condition to find the charges bar in which the units consumed is fall ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateBill ( int units ) { if ( units <= 100 ) { return units * 10 ; } else if ( units <= 200 ) { return ( 100 * 10 ) + ( units - 100 ) * 15 ; } else if ( units <= 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( units - 200 ) * 20 ; } else if ( units > 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( 100 * 20 ) + ( units - 300 ) * 25 ; } return 0 ; } int main ( ) { int units = 250 ; cout << calculateBill ( units ) ; }
Find all divisors of first N natural numbers | C ++ implementation to find all the divisors of the first N natural numbers ; Function to find the factors of the numbers from 1 to N ; Loop to find the factors of the first N natural numbers of the integer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void factors ( int n ) { int i , j ; cout << "1 ▁ - - > 1 STRNEWLINE " ; for ( i = 2 ; i <= n ; i ++ ) { cout << i << " ▁ - - > " ; for ( j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { cout << j << " , ▁ " ; if ( i / j != j ) cout << i / j << " , ▁ " ; } } cout << " STRNEWLINE " ; } } int main ( ) { int n = 5 ; factors ( n ) ; }
Find all divisors of first N natural numbers | C ++ implementation to find the factors of first N natural numbers ; Initialize global divisor vector array of sequence container ; Calculate all divisors of number ; Function to find the factors of first n natural numbers ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1e5 ; vector < int > divisor [ MAX + 1 ] ; void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . push_back ( i ) ; } } void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { cout << i << " - - > " ; for ( auto & divi : divisor [ i ] ) { cout << divi << " , ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int n = 5 ; sieve ( ) ; findNFactors ( n ) ; }
Maximum number of prime factors a number can have with exactly x factors | C ++ implementation to find the maximum count of the prime factors by the count of factors of number ; Function to count number of prime factors of x ; Count variable is incremented for every prime factor of x ; Loop to count the number of the prime factors of the given number ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int countPrimeFactors ( int n ) { if ( n == 1 ) return 0 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { cnt ++ ; n = n / i ; } } if ( n > 2 ) cnt ++ ; return cnt ; } int main ( ) { int x = 8 ; int prime_factor_cnt = countPrimeFactors ( x ) ; cout << prime_factor_cnt << endl ; return 0 ; }
Count of N digit palindromic numbers divisible by 9 | C ++ implementation to count the number of N digit palindromic numbers divisible by 9 ; Function to find the count of N digits palindromic numbers which are divisible by 9 ; if N is odd ; if N is even ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPalindromic ( int n ) { int count ; if ( n % 2 == 1 ) { count = pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } int main ( ) { int n = 3 ; cout << countPalindromic ( n ) ; return 0 ; }
Count of sub | C ++ program to find the count of sub - arrays with odd product ; Function that returns the count of sub - arrays with odd product ; Initialize the count variable ; Initialize variable to store the last index with even number ; Initialize variable to store count of continuous odd numbers ; Loop through the array ; Check if the number is even or not ; Calculate count of continuous odd numbers ; Increase the count of sub - arrays with odd product ; Store the index of last even number ; N considered as index of even number ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubArrayWithOddProduct ( int * A , int N ) { int count = 0 ; int last = -1 ; int K = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { K = ( i - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; last = i ; } } K = ( N - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; return count ; } int main ( ) { int arr [ ] = { 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubArrayWithOddProduct ( arr , n ) ; return 0 ; }
Calculate the CGPA and CGPA % of marks obtained by a Student in N subjects | C ++ program to calculate the CGPA and CGPA percentage of a student ; Variable to store the grades in every subject ; Variables to store CGPA and the sum of all the grades ; Computing the grades ; Computing the sum of grades ; Computing the CGPA ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double CgpaCalc ( double marks [ ] , int n ) { double grade [ n ] ; double cgpa , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { grade [ i ] = ( marks [ i ] / 10 ) ; } for ( int i = 0 ; i < n ; i ++ ) { sum += grade [ i ] ; } cgpa = sum / n ; return cgpa ; } int main ( ) { int n = 5 ; double marks [ ] = { 90 , 80 , 70 , 80 , 90 } ; double cgpa = CgpaCalc ( marks , n ) ; cout << " CGPA ▁ = ▁ " ; printf ( " % .1f STRNEWLINE " , cgpa ) ; cout << " CGPA ▁ Percentage ▁ = ▁ " ; printf ( " % .2f " , cgpa * 9.5 ) ; }
Find the subarray of size K with minimum XOR | C ++ implementation to find the subarray with minimum XOR ; Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Driver Code ; int k = 3 ; Subarray size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinXORSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } cout << min_xor << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int n = sizeof arr / sizeof arr [ 0 ] ; findMinXORSubarray ( arr , n , k ) ; return 0 ; }
Greatest number that can be formed from a pair in a given Array | C ++ implementation to find the greatest number from the given pairs of the array ; Function to find the greatest number formed from the pairs ; first append Y at the end of X ; then append X at the end of Y ; Now see which of the two formed numbers is greater than other ; Function to find pairs from array ; Iterate through all pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getNumber ( int a , int b ) { string X = to_string ( a ) ; string Y = to_string ( b ) ; string XY = X + Y ; string YX = Y + X ; return XY > YX ? XY : YX ; } void printMaxPair ( int arr [ ] , int n ) { int largest = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int number = stoi ( getNumber ( arr [ i ] , arr [ j ] ) ) ; largest = max ( largest , number ) ; } cout << largest ; } int main ( ) { int a [ ] = { 23 , 14 , 16 , 25 , 3 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printMaxPair ( a , n ) ; return 0 ; }
Check whether two numbers are in golden ratio | C ++ implementation to check whether two numbers are in golden ratio with each other ; Function to check that two numbers are in golden ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in golden ratio ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkGoldenRatio ( float a , float b ) { if ( a <= b ) { float temp = a ; a = b ; b = temp ; } std :: stringstream ratio1 ; ratio1 << std :: fixed << std :: setprecision ( 3 ) << ( a / b ) ; std :: stringstream ratio2 ; ratio2 << std :: fixed << std :: setprecision ( 3 ) << ( a + b ) / a ; if ( ( ratio1 . str ( ) == ratio2 . str ( ) ) && ratio1 . str ( ) == "1.618" ) { cout << " Yes " << endl ; return true ; } else { cout << " No " << endl ; return false ; } } int main ( ) { float a = 0.618 ; float b = 1 ; checkGoldenRatio ( a , b ) ; return 0 ; }
Find the minimum number to be added to N to make it a power of K | C ++ program to find the minimum number to be added to N to make it a power of K ; Function to return the minimum number to be added to N to make it a power of K . ; Computing the difference between then next greater power of K and N ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll long long int NEW_LINE using namespace std ; int minNum ( int n , int k ) { int x = ( int ) ( log ( n ) / log ( k ) ) + 1 ; int mn = pow ( k , x ) - n ; return mn ; } int main ( ) { int n = 20 , k = 5 ; cout << minNum ( n , k ) ; return 0 ; }
Previous perfect square and cube number smaller than number N | C ++ implementation to find the previous perfect square and cube smaller than the given number ; Function to find the previous perfect square of the number N ; If N is already a perfect square decrease prevN by 1. ; Function to find the previous perfect cube ; If N is already a perfect cube decrease prevN by 1. ; Driver Code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int previousPerfectSquare ( int N ) { int prevN = floor ( sqrt ( N ) ) ; if ( prevN * prevN == N ) prevN -= 1 ; return prevN * prevN ; } int previousPerfectCube ( int N ) { int prevN = floor ( cbrt ( N ) ) ; if ( prevN * prevN * prevN == N ) prevN -= 1 ; return prevN * prevN * prevN ; } int main ( ) { int n = 30 ; cout << previousPerfectSquare ( n ) << " STRNEWLINE " ; cout << previousPerfectCube ( n ) << " STRNEWLINE " ; return 0 ; }
Find the count of even odd pairs in a given Array | C ++ program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; variable to store count of such pairs ; Iterate through all pairs ; Increment count if condition is satisfied ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] % 2 == 0 ) && ( arr [ j ] % 2 == 1 ) ) { res ++ ; } return res ; } int main ( ) { int a [ ] = { 5 , 4 , 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findCount ( a , n ) ; return 0 ; }
Find the count of even odd pairs in a given Array | C ++ program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; check if number is even or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( int arr [ ] , int n ) { int count = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count ++ ; else ans = ans + count ; } return ans ; } int main ( ) { int a [ ] = { 5 , 4 , 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findCount ( a , n ) ; return 0 ; }
Product of all non repeating Subarrays of an Array | C ++ program to find the product of all non - repeating Subarrays of an Array ; Function to find the product of all non - repeating Subarrays of an Array ; Finding the occurrence of every element ; Iterating through the array and finding the product ; We are taking the power of each element in array with the occurrence and then taking product of those . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long product ( int arr [ ] , int n ) { double occurrence = pow ( 2 , n - 1 ) ; double product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product *= pow ( arr [ i ] , occurrence ) ; } return ( long ) product ; } int main ( ) { int arr [ ] = { 10 , 3 , 7 } ; int len = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << product ( arr , len ) ; return 0 ; }
Sum of all N | C ++ program for the above approach ; Function to find a ^ b efficiently ; Base Case ; To store the value of a ^ b ; Multiply temp by a until b is not 0 ; Return the final ans a ^ b ; Function to find sum of all N - digit palindromic number divisible by 9 ; Base Case ; If N is even , decrease ways by 1 ; Find the total number of ways ; Iterate over [ 1 , N ] and find the sum at each index ; Print the final Sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int MOD = 1000000007 ; long long int power ( long long int a , long long int b ) { if ( b == 0 ) return 1 ; long long int temp = power ( a , b / 2 ) ; temp = ( temp * temp ) % MOD ; if ( b % 2 != 0 ) { temp = ( temp * a ) % MOD ; } return temp ; } void palindromicSum ( int N ) { long long int sum = 0 , res , ways ; if ( N == 1 ) { cout << "9" << endl ; return ; } if ( N == 2 ) { cout << "99" << endl ; return ; } ways = N / 2 ; if ( N % 2 == 0 ) ways -- ; res = power ( 9 , ways - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum = sum * 10 + 45 * res ; sum %= MOD ; } cout << sum << endl ; } int main ( ) { int N = 3 ; palindromicSum ( N ) ; return 0 ; }
Maximize array sum by concatenating corresponding elements of given two arrays | C ++ implementation to find the maximum array sum by concatenating corresponding elements of given two arrays ; Function to join the two numbers ; Loop to reverse the digits of the one number ; Loop to join two numbers ; Function to find the maximum array sum ; Loop to iterate over the two elements of the array ; Find the array sum ; Return the array sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int joinNumbers ( int numA , int numB ) { int revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = numB / 10 ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = revB / 10 ; } return numA ; } int findMaxSum ( int A [ ] , int B [ ] , int n ) { int maxArr [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { int X = joinNumbers ( A [ i ] , B [ i ] ) ; int Y = joinNumbers ( B [ i ] , A [ i ] ) ; int mx = max ( X , Y ) ; maxArr [ i ] = mx ; } int maxAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } int main ( ) { int N = 5 ; int A [ 5 ] = { 11 , 23 , 38 , 43 , 59 } ; int B [ 5 ] = { 36 , 24 , 17 , 40 , 56 } ; cout << findMaxSum ( A , B , 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 ; For n <= 2 such sets can never be formed ; Check if N is even or odd and store the element which divides the sum of N natural numbers accordingly ; First set ; Print elements of second set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll long long NEW_LINE void createSets ( ll n ) { if ( n <= 2 ) { cout << " - 1" ; return ; } else { ll x = ( n % 2 == 0 ) ? ( n / 2 ) : ( ( n + 1 ) / 2 ) ; cout << x << endl ; for ( ll i = 1 ; i <= n ; i ++ ) { if ( i == x ) continue ; cout << i << " ▁ " ; } } return ; } int main ( ) { ll N = 7 ; createSets ( N ) ; return 0 ; }
Count the factors of K present in the given Array | C ++ implementation to find the count of factors of K present in array ; Function to find the count of factors of K present in array ; Loop to consider every element of array ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; int calcCount ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 6 ; cout << calcCount ( arr , n , k ) ; return 0 ; }
Multiply N complex numbers given as strings | C ++ Program to multiply N complex Numbers ; Function which returns the string in digit format ; a : real b : imaginary ; sa : sign of a sb : sign of b ; Extract the real number ; Extract the imaginary part ; if size == 1 means we reached at result ; Extract the first two elements ; Remove them ; Calculate and store the real part ; Calculate and store the imaginary part ; Append the real part ; Append the imaginary part ; Insert into vector ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll long long NEW_LINE vector < long long int > findnum ( string s1 ) { vector < long long int > v ; int a = 0 , b = 0 ; int sa = 0 , sb = 0 , i = 0 ; if ( s1 [ 0 ] == ' - ' ) { sa = 1 ; i = 1 ; } while ( isdigit ( s1 [ i ] ) ) { a = a * 10 + ( int ( s1 [ i ] ) - 48 ) ; i ++ ; } if ( s1 [ i ] == ' + ' ) { sb = 0 ; i += 1 ; } if ( s1 [ i ] == ' - ' ) { sb = 1 ; i += 1 ; } while ( i < s1 . length ( ) && isdigit ( s1 [ i ] ) ) { b = b * 10 + ( int ( s1 [ i ] ) - 48 ) ; i ++ ; } if ( sa ) a *= -1 ; if ( sb ) b *= -1 ; v . push_back ( a ) ; v . push_back ( b ) ; return v ; } string complexNumberMultiply ( vector < string > v ) { while ( v . size ( ) != 1 ) { vector < ll > v1 = findnum ( v [ 0 ] ) ; vector < ll > v2 = findnum ( v [ 1 ] ) ; v . erase ( v . begin ( ) ) ; v . erase ( v . begin ( ) ) ; ll r = ( v1 [ 0 ] * v2 [ 0 ] - v1 [ 1 ] * v2 [ 1 ] ) ; ll img = v1 [ 0 ] * v2 [ 1 ] + v1 [ 1 ] * v2 [ 0 ] ; string res = " " ; res += to_string ( r ) ; res += ' + ' ; res += to_string ( img ) + ' i ' ; v . insert ( v . begin ( ) , res ) ; } return v [ 0 ] ; } int main ( ) { int n = 3 ; vector < string > v = { "3 + 1i " , "2 + 1i " , " - 5 + - 7i " } ; cout << complexNumberMultiply ( v ) << " STRNEWLINE " ; return 0 ; }
Length of largest subarray whose all elements are Perfect Number | C ++ program to find the length of the largest sub - array of an array every element of whose is a perfect number ; Function that returns true if n is perfect ; Variable to store sum of divisors ; Find all divisors and add them ; Check if sum of divisors is equal to n , then n is a perfect number ; Function to return the length of the largest sub - array of an array every element of whose is a perfect number ; Check if arr [ i ] is a perfect number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfect ( long long int n ) { long long int sum = 1 ; for ( long long int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } int contiguousPerfectNumber ( int arr [ ] , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPerfect ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = max ( max_length , current_length ) ; } return max_length ; } int main ( ) { int arr [ ] = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << contiguousPerfectNumber ( arr , n ) ; return 0 ; }
Length of largest subarray whose all elements Powerful number | C ++ program to find the length of the largest sub - array of an array every element of whose is a powerful number ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; If n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to return the length of the largest sub - array of an array every element of whose is a powerful number ; If arr [ i ] is a Powerful number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } int contiguousPowerfulNumber ( int arr [ ] , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = max ( max_length , current_length ) ; } return max_length ; } int main ( ) { int arr [ ] = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << contiguousPowerfulNumber ( arr , n ) ; return 0 ; }
Maximize sum of K corner elements in Array | C ++ program to maximize the sum of K elements in the array by taking only corner elements ; Function to return maximum sum ; Base case ; Pick the start index ; Pick the end index ; Recursive function call ; Return the final answer ; Function to find the maximized sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int arr [ ] , int K , int start , int end , int max_sum ) { if ( K == 0 ) return max_sum ; int max_sum_start = max_sum + arr [ start ] ; int max_sum_end = max_sum + arr [ end ] ; int ans = max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) ; return ans ; } void maximizeSum ( int arr [ ] , int K , int n ) { int max_sum = 0 ; int start = 0 ; int end = n - 1 ; cout << maxSum ( arr , K , start , end , max_sum ) ; } int main ( ) { int arr [ ] = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximizeSum ( arr , K , n ) ; return 0 ; }
Maximize sum of K corner elements in Array | C ++ program to maximize the sum of K elements in the array by taking only corner elements ; Function to return maximum sum ; Initialize variables ; Iterate over first K elements of array and update the value for curr_points ; Update value for max_points ; j points to the end of the array ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxPointCount ( int arr [ ] , int K , int size ) { int curr_points = 0 ; int max_points = 0 ; for ( int i = 0 ; i < K ; i ++ ) curr_points += arr [ i ] ; max_points = curr_points ; int j = size - 1 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { curr_points = curr_points + arr [ j ] - arr [ i ] ; max_points = max ( curr_points , max_points ) ; j -- ; } return max_points ; } int main ( ) { int arr [ ] = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxPointCount ( arr , K , n ) ; return 0 ; }
Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9 | C ++ implementation to find the sum of all the N digit palindromic numbers divisible by 9 ; Function for finding count of N digits palindrome which are divisible by 9 ; if N is odd ; if N is even ; Function for finding sum of N digits palindrome which are divisible by 9 ; count the possible number of palindrome ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPalindrome ( int n ) { int count ; if ( n % 2 == 1 ) { count = pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } int sumPalindrome ( int n ) { int count = countPalindrome ( n ) ; int res = 0 ; if ( n == 1 ) return 9 ; if ( n == 2 ) return 99 ; for ( int i = 0 ; i < n ; i ++ ) { res = res * 10 + count * 5 ; } return res ; } int main ( ) { int n = 3 ; cout << sumPalindrome ( n ) ; return 0 ; }
Find the total count of numbers up to N digits in a given base B | C ++ implementation to find the count of natural numbers upto N digits ; Function to return the count of natural numbers upto N digits ; Loop to iterate from 1 to N and calculating number of natural numbers for every ' i ' th digit . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count ( int N , int B ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( B - 1 ) * pow ( B , i - 1 ) ; } return sum ; } int main ( ) { int N = 2 , B = 10 ; cout << count ( N , B ) ; return 0 ; }
Find smallest perfect square number A such that N + A is also a perfect square number | C ++ code to find out the smallest perfect square X which when added to N yields another perfect square number . ; X is the smallest perfect square number ; Loop from 1 to square root of N ; Condition to check whether i is factor of N or not ; Condition to check whether factors satisfies the equation or not ; Stores minimum value ; Return if X * X if X is not equal to 1e9 else return - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long SmallestPerfectSquare ( long N ) { long X = ( long ) 1e9 ; long ans ; for ( int i = 1 ; i < sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { long a = i ; long b = N / i ; if ( ( b - a != 0 ) && ( ( b - a ) % 2 == 0 ) ) { X = min ( X , ( b - a ) / 2 ) ; } } } if ( X != 1e9 ) ans = X * X ; else ans = -1 ; return ans ; } int main ( ) { long N = 3 ; cout << SmallestPerfectSquare ( N ) ; return 0 ; }
Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | C ++ implementation to find the Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road ; Function to find the minimum cost ; Sort the given array ; Initialise a new array of double size ; Fill the array elements ; Find the minimum path cost ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( int arr [ ] , int n , int circumference ) { sort ( arr , arr + n ) ; int arr2 [ 2 * n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } int res = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) res = min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } int main ( ) { int arr [ ] = { 19 , 3 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int circumference = 20 ; cout << minCost ( arr , n , circumference ) ; return 0 ; }
Unique element in an array where all elements occur K times except one | Set 2 | CPP program for the above approach ; Function to find single occurrence element ; By shifting 1 to left ith time and taking and with 1 will give us that ith bit of a [ j ] is 1 or 0 ; Taking modulo of p with k ; Generate result ; Loop for negative numbers ; Check if the calculated value res is present in array , then mark c = 1 and if c = 1 return res else res must be - ve ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findunique ( vector < int > & a , int k ) { int res = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int p = 0 ; for ( int j = 0 ; j < a . size ( ) ; j ++ ) { p += ( abs ( a [ j ] ) & ( 1 << i ) ) != 0 ? 1 : 0 ; } p %= k ; res += pow ( 2 , i ) * p ; } int c = 0 ; for ( auto x : a ) if ( x == res ) { c = 1 ; break ; } return c == 1 ? res : - res ; } int main ( ) { vector < int > a = { 12 , 12 , 2 , 2 , 3 } ; int k = 2 ; cout << findunique ( a , k ) << " STRNEWLINE " ; }
Find minimum GCD of all pairs in an array | C ++ program to find the minimum GCD of any pair in the array ; Function returns the Minimum GCD of any pair ; Finding GCD of all the elements in the array . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumGCD ( int arr [ ] , int n ) { int g = 0 ; for ( int i = 0 ; i < n ; i ++ ) { g = __gcd ( g , arr [ i ] ) ; } return g ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 8 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MinimumGCD ( arr , N ) << endl ; }
Minimum steps to reach the Nth stair in jumps of perfect power of 2 | C ++ program for the above approach ; Function to count the number of jumps required to reach Nth stairs . ; Till N becomes 0 ; Removes the set bits from the right to left ; Driver Code ; Number of stairs ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int stepRequired ( int N ) { int cnt = 0 ; while ( N ) { N = N & ( N - 1 ) ; cnt ++ ; } return cnt ; } int main ( ) { int N = 23 ; cout << stepRequired ( N ) ; return 0 ; }
Count of total subarrays whose sum is a Fibonacci Numbers | C ++ program for the above approach ; Function to check whether a number is perfect square or not ; Function to check whether a number is fibonacci number or not ; If 5 * n * n + 4 or 5 * n * n - 5 is a perfect square , then the number is Fibonacci ; Function to count the subarray with sum fibonacci number ; Traverse the array arr [ ] to find the sum of each subarray ; To store the sum ; Check whether sum of subarray between [ i , j ] is fibonacci or not ; Driver Code ; Function Call
#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 ) ; } void fibonacciSubarrays ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int sum = 0 ; for ( int j = i ; j < n ; ++ j ) { sum += arr [ j ] ; if ( isFibonacci ( sum ) ) { ++ count ; } } } cout << count ; } int main ( ) { int arr [ ] = { 6 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; fibonacciSubarrays ( arr , n ) ; return 0 ; }
Count the number of ways to fill K boxes with N distinct items | C ++ program to calculate the above formula ; To store the factorials of all numbers ; Function to calculate factorial of all numbers ; Calculate x to the power y in O ( log n ) time ; Function to find inverse mod of a number x ; Calculate ( n C r ) ; Loop to compute the formula evaluated ; Add even power terms ; Subtract odd power terms ; Choose the k boxes which were used ; Driver code
#include <bits/stdc++.h> NEW_LINE #define mod 1000000007 NEW_LINE #define int long long NEW_LINE using namespace std ; int factorial [ 100005 ] ; void StoreFactorials ( int n ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { factorial [ i ] = ( i * factorial [ i - 1 ] ) % mod ; } } int Power ( int x , int y ) { int ans = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) { ans = ( ans * x ) % mod ; } x = ( x * x ) % mod ; y /= 2 ; } return ans ; } int invmod ( int x ) { return Power ( x , mod - 2 ) ; } int nCr ( int n , int r ) { return ( factorial [ n ] * invmod ( ( factorial [ r ] * factorial [ n - r ] ) % mod ) ) % mod ; } int CountWays ( int n , int k ) { StoreFactorials ( n ) ; int ans = 0 ; for ( int i = k ; i >= 0 ; i -- ) { if ( i % 2 == k % 2 ) { ans = ( ans + ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ; } else { ans = ( ans + mod - ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ; } } ans = ( ans * nCr ( n , k ) ) % mod ; return ans ; } signed main ( ) { int N = 5 ; int K = 5 ; cout << CountWays ( N , K ) << " STRNEWLINE " ; return 0 ; }
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 ; if N is less than 1 then return void function ; recursive call of the function ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void PrintReverseOrder ( int N ) { if ( N <= 0 ) { return ; } else { cout << N << " ▁ " ; PrintReverseOrder ( N - 1 ) ; } } int main ( ) { int N = 5 ; PrintReverseOrder ( N ) ; return 0 ; }
Find the XOR of first half and second half elements of an array | C ++ program to find the xor of the first half elements and second half elements of an array ; Function to find the xor of the first half elements and second half elements of an array ; xor of elements in FirstHalfXOR ; xor of elements in SecondHalfXOR ; Driver Code ; Function call
#include <iostream> NEW_LINE using namespace std ; void XOROfElements ( int arr [ ] , int n ) { int FirstHalfXOR = 0 ; int SecondHalfXOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) FirstHalfXOR ^= arr [ i ] ; else SecondHalfXOR ^= arr [ i ] ; } cout << FirstHalfXOR << " , " << SecondHalfXOR << endl ; } int main ( ) { int arr [ ] = { 20 , 30 , 50 , 10 , 55 , 15 , 42 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; XOROfElements ( arr , N ) ; return 0 ; }
Minimum volume of cone that can be circumscribed about a sphere of radius R | C ++ program to find the minimum volume of the cone that can be circumscribed about a sphere of radius R ; Function to find the volume of the cone ; r = radius of cone h = height of cone Volume of cone = ( 1 / 3 ) * ( 3.14 ) * ( r * r ) * ( h ) we get radius of cone from the derivation is root ( 2 ) times multiple of R we get height of cone from the derivation is 4 times multiple of R ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float Volume_of_cone ( float R ) { float V = ( 1 / 3.0 ) * ( 3.14 ) * ( 2 * ( R * R ) ) * ( 4 * R ) ; return V ; } int main ( ) { float R = 10.0 ; cout << Volume_of_cone ( R ) ; }
Program to find Surface Area and Volume of Octagonal Prism | C ++ program to find the Surface area and volume of octagonal prism ; Function to find the Volume of octagonal prism ; Formula to calculate volume = ( area * h ) ; Display volume ; Function to find the surface area of octagonal prism ; Formula to calculate Surface area ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void find_volume ( float area , float h ) { float Volume = ( area * h ) ; cout << " Volume : ▁ " << Volume << endl ; } void find_Surface_area ( float area , float a , float h ) { float Surface_area = ( 2 * area ) + ( 8 * a * h ) ; cout << " Surface ▁ area : ▁ " << Surface_area << endl ; } int main ( ) { float h = 1 ; float a = 6 ; float d = 2 ; float area = 2 * a * d ; find_Surface_area ( area , a , h ) ; find_volume ( area , h ) ; return 0 ; }
Count of pairs upto N such whose LCM is not equal to their product for Q queries | C ++ program to find the count of pairs from 1 to N such that their LCM is not equal to their product ; To store Euler 's Totient Function ; To store prefix sum table ; Compute Totients of all numbers smaller than or equal to N ; Make phi [ 1 ] = 0 since 1 cannot form any pair ; Initialise all remaining phi [ ] with i ; Compute remaining phi ; If phi [ p ] is not computed already , then number p is prime ; phi of prime number is p - 1 ; Update phi of all multiples of p ; Add the contribution of p to its multiple i by multiplying it with ( 1 - 1 / p ) ; Function to store prefix sum table ; Prefix Sum of all Euler 's Totient Values ; Total number of pairs that can be formed ; Driver Code ; Function call to compute all phi ; Function call to store all prefix sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N 100005 NEW_LINE int phi [ N ] ; int pref [ N ] ; void precompute ( ) { phi [ 1 ] = 0 ; for ( int i = 2 ; i < N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } void prefix ( ) { for ( int i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + phi [ i ] ; } void find_pairs ( int n ) { int total = ( n * ( n - 1 ) ) / 2 ; int ans = total - pref [ n ] ; cout << " Number ▁ of ▁ pairs ▁ from ▁ 1 ▁ to ▁ " << n << " ▁ are ▁ " << ans << endl ; } int main ( ) { precompute ( ) ; prefix ( ) ; int q [ ] = { 5 , 7 } ; int n = sizeof ( q ) / sizeof ( q [ 0 ] ) ; for ( int i = 0 ; i < n ; i ++ ) { find_pairs ( q [ i ] ) ; } return 0 ; }
Program to balance the given Chemical Equation | C ++ program to balance the given Chemical Equation ; Function to calculate GCD ; Function to calculate b1 , b2 and b3 ; Variable declaration ; temp variable to store gcd ; Computing GCD ; 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 ) ; } void balance ( int x , int y , int p , int q ) { int b1 , b2 , b3 ; if ( p % x == 0 && q % y == 0 ) { b1 = p / x ; b2 = q / y ; b3 = 1 ; } else { p = p * y ; q = q * x ; b3 = x * y ; int temp = gcd ( p , gcd ( q , b3 ) ) ; b1 = p / temp ; b2 = q / temp ; b3 = b3 / temp ; } cout << b1 << " ▁ " << b2 << " ▁ " << b3 << endl ; } int main ( ) { int x = 2 , y = 3 , p = 4 , q = 5 ; balance ( x , y , p , q ) ; }
Find the Maximum Alternate Subsequence Sum from a given array | C ++ program to find the maximum alternating subsequence sum for a given array ; Function to find maximum alternating subsequence sum ; Initialize sum to 0 ; Calculate the sum ; Return the final result ; Driver Code ; Array initialization ; Length of array
#include <iostream> NEW_LINE using namespace std ; int maxAlternatingSum ( int arr [ ] , int n ) { int max_sum = 0 ; int i = 0 ; while ( i < n ) { int current_max = arr [ i ] ; int k = i ; while ( k < n && ( ( arr [ i ] > 0 && arr [ k ] > 0 ) || ( arr [ i ] < 0 && arr [ k ] < 0 ) ) ) { current_max = max ( current_max , arr [ k ] ) ; k += 1 ; } max_sum += current_max ; i = k ; } return max_sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , -1 , -2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxAlternatingSum ( arr , n ) ; return 0 ; }