codeparrot/xlcost-text-to-code huggingface dataset:
Count inversions in an array | Set 3 ( Using BIT ) | C ++ program to count inversions using Binary Indexed Tree ; Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] . ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Converts an array to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a copy of arrp [ ] in temp and sort the temp array in increasing order ; Traverse all array elements ; lower_bound ( ) Returns pointer to the first element greater than or equal to arr [ i ] ; Returns inversion count arr [ 0. . n - 1 ] ; Convert arr [ ] to an array with values from 1 to n and relative order of smaller and greater elements remains same . For example , { 7 , - 90 , 100 , 1 } is converted to { 3 , 1 , 4 , 2 } ; Create a BIT with size equal to maxElement + 1 ( Extra one is used so that elements can be directly be used as index ) ; Traverse all elements from right . ; Get count of elements smaller than arr [ i ] ; Add current element to BIT ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getSum ( int BITree [ ] , int index ) { while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } void updateBIT ( int BITree [ ] , int n , int index , int val ) { while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } void convert ( int arr [ ] , int n ) { int temp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = arr [ i ] ; sort ( temp , temp + n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = lower_bound ( temp , temp + n , arr [ i ] ) - temp + 1 ; } } int getInvCount ( int arr [ ] , int n ) { convert ( arr , n ) ; int BIT [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BIT [ i ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { invcount += getSum ( BIT , arr [ i ] - 1 ) ; updateBIT ( BIT , n , arr [ i ] , 1 ) ; } return invcount ; } int main ( ) { int arr [ ] = { 8 , 4 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << " Number ▁ of ▁ inversions ▁ are ▁ : ▁ " << getInvCount ( arr , n ) ; return 0 ; }
Compute n ! under modulo p | Simple method to compute n ! % p ; Returns value of n ! % p ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int modFact ( int n , int p ) { if ( n >= p ) return 0 ; int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = ( result * i ) % p ; return result ; } int main ( ) { int n = 25 , p = 29 ; cout << modFact ( n , p ) ; return 0 ; }
Chinese Remainder Theorem | Set 2 ( Inverse Modulo based Implementation ) | A C ++ program to demonstrate working of Chinise remainder Theorem ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm . Refer below post for details : https : www . geeksforgeeks . org / multiplicative - inverse - under - modulo - m / ; Apply extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as euclid 's algo ; Make x1 positive ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd for every pair is 1 ) ; Compute product of all numbers ; Initialize result ; Apply above formula ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m , a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } int findMinX ( int num [ ] , int rem [ ] , int k ) { int prod = 1 ; for ( int i = 0 ; i < k ; i ++ ) prod *= num [ i ] ; int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int pp = prod / num [ i ] ; result += rem [ i ] * inv ( pp , num [ i ] ) * pp ; } return result % prod ; } int main ( void ) { int num [ ] = { 3 , 4 , 5 } ; int rem [ ] = { 2 , 3 , 1 } ; int k = sizeof ( num ) / sizeof ( num [ 0 ] ) ; cout << " x ▁ is ▁ " << findMinX ( num , rem , k ) ; return 0 ; }
Chinese Remainder Theorem | Set 1 ( Introduction ) | A C ++ program to demonstrate working of Chinise remainder Theorem ; k is size of num [ ] and rem [ ] . Returns the smallest number x such that : x % num [ 0 ] = rem [ 0 ] , x % num [ 1 ] = rem [ 1 ] , ... ... ... ... ... ... x % num [ k - 2 ] = rem [ k - 1 ] Assumption : Numbers in num [ ] are pairwise coprime ( gcd for every pair is 1 ) ; As per the Chinese remainder theorem , this loop will always break . ; Check if remainder of x % num [ j ] is rem [ j ] or not ( for all j from 0 to k - 1 ) ; If all remainders matched , we found x ; Else try next number ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinX ( int num [ ] , int rem [ ] , int k ) { while ( true ) { int j ; for ( j = 0 ; j < k ; j ++ ) if ( x % num [ j ] != rem [ j ] ) break ; if ( j == k ) return x ; x ++ ; } return x ; } int main ( void ) { int num [ ] = { 3 , 4 , 5 } ; int rem [ ] = { 2 , 3 , 1 } ; int k = sizeof ( num ) / sizeof ( num [ 0 ] ) ; cout << " x ▁ is ▁ " << findMinX ( num , rem , k ) ; return 0 ; }
Compute nCr % p | Set 2 ( Lucas Theorem ) | A Lucas Theorem based solution to compute nCr % p ; Returns nCr % p . In this Lucas Theorem based program , this function is only called for n < p and r < p . ; The array C is going to store last row of pascal triangle at the end . And last entry of last row is nCr ; One by constructs remaining rows of Pascal Triangle from top to bottom ; Fill entries of current row using previous row values ; nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; ; Lucas Theorem based function that returns nCr % p This function works like decimal to binary conversion recursive function . First we compute last digits of n and r in base p , then recur for remaining digits ; Base case ; Compute last digits of n and r in base p ; Compute result for last digits computed above , and for remaining digits . Multiply the two results and compute the result of multiplication in modulo p . return ( nCrModpLucas ( n / p , r / p , p ) * Last digits of n and r nCrModpDP ( ni , ri , p ) ) % p ; Remaining digits ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nCrModpDP ( int n , int r , int p ) { int C [ r + 1 ] ; memset ( C , 0 , sizeof ( C ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; } int nCrModpLucas ( int n , int r , int p ) { if ( r == 0 ) return 1 ; int ni = n % p , ri = r % p ; } int main ( ) { int n = 1000 , r = 900 , p = 13 ; cout << " Value ▁ of ▁ nCr ▁ % ▁ p ▁ is ▁ " << nCrModpLucas ( n , r , p ) ; return 0 ; }
Fibonacci Coding | C ++ program for Fibonacci Encoding of a positive integer n ; To limit on the largest Fibonacci number to be used ; Array to store fibonacci numbers . fib [ i ] is going to store ( i + 2 ) 'th Fibonacci number ; Stores values in fib and returns index of the largest fibonacci number smaller than n . ; Fib [ 0 ] stores 2 nd Fibonacci No . ; Fib [ 1 ] stores 3 rd Fibonacci No . ; Keep Generating remaining numbers while previously generated number is smaller ; Return index of the largest fibonacci number smaller than or equal to n . Note that the above loop stopped when fib [ i - 1 ] became larger . ; Returns pointer to the char string which corresponds to code for n ; allocate memory for codeword ; index of the largest Fibonacci f <= n ; Mark usage of Fibonacci f ( 1 bit ) ; Subtract f from n ; Move to Fibonacci just smaller than f ; Mark all Fibonacci > n as not used ( 0 bit ) , progress backwards ; additional '1' bit ; return pointer to codeword ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  30 NEW_LINE int fib [ N ] ; int largestFiboLessOrEqual ( int n ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; int i ; for ( i = 2 ; fib [ i - 1 ] <= n ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; return ( i - 2 ) ; } char * fibonacciEncoding ( int n ) { int index = largestFiboLessOrEqual ( n ) ; char * codeword = ( char * ) malloc ( sizeof ( char ) * ( index + 3 ) ) ; int i = index ; while ( n ) { codeword [ i ] = '1' ; n = n - fib [ i ] ; i = i - 1 ; while ( i >= 0 && fib [ i ] > n ) { codeword [ i ] = '0' ; i = i - 1 ; } } codeword [ index + 1 ] = '1' ; codeword [ index + 2 ] = ' \0' ; return codeword ; } int main ( ) { int n = 143 ; cout << " Fibonacci ▁ code ▁ word ▁ for ▁ " << n << " ▁ is ▁ " << fibonacciEncoding ( n ) ; return 0 ; }
Print all Good numbers in given range | C ++ program to print good numbers in a given range [ L , R ] ; To check whether n is a good number and doesn ' t ▁ contain ▁ digit ▁ ' d '. ; Get last digit and initialize sum from right side ; If last digit is d , return ; Traverse remaining digits ; Current digit ; If digit is d or digit is less than or equal to sum of digits on right side ; Update sum and n ; Print Good numbers in range [ L , R ] ; Traverse all numbers in given range ; If current numbers is good , print it . ; Driver program ; Print good numbers in [ L , R ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValid ( int n , int d ) { int digit = n % 10 ; int sum = digit ; if ( digit == d ) return false ; n /= 10 ; while ( n ) { digit = n % 10 ; if ( digit == d digit <= sum ) return false ; else { sum += digit ; n /= 10 ; } } return 1 ; } void printGoodNumbers ( int L , int R , int d ) { for ( int i = L ; i <= R ; i ++ ) { if ( isValid ( i , d ) ) cout << i << " ▁ " ; } } int main ( ) { int L = 410 , R = 520 , d = 3 ; printGoodNumbers ( L , R , d ) ; return 0 ; }
Count number of squares in a rectangle | C ++ program to count squares in a rectangle of size m x n ; Returns count of all squares in a rectangle of size m x n ; If n is smaller , swap m and n ; Now n is greater dimension , apply formula ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int countSquares ( int m , int n ) { if ( n < m ) { int temp = m ; m = n ; n = temp ; } return n * ( n + 1 ) * ( 3 * m - n + 1 ) / 6 ; } int main ( ) { int m = 4 , n = 3 ; cout << " Count ▁ of ▁ squares ▁ is ▁ " << countSquares ( m , n ) ; }
Zeckendorf 's Theorem (Non | C ++ program for Zeckendorf 's theorem. It finds representation of n as sum of non-neighbouring Fibonacci Numbers. ; Returns the greatest Fibonacci Number smaller than or equal to n . ; Corner cases ; Find the greatest Fibonacci Number smaller than n . ; Prints Fibonacci Representation of n using greedy algorithm ; Find the greates Fibonacci Number smaller than or equal to n ; Print the found fibonacci number ; Reduce n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nearestSmallerEqFib ( int n ) { if ( n == 0 n == 1 ) return n ; int f1 = 0 , f2 = 1 , f3 = 1 ; while ( f3 <= n ) { f1 = f2 ; f2 = f3 ; f3 = f1 + f2 ; } return f2 ; } void printFibRepresntation ( int n ) { while ( n > 0 ) { int f = nearestSmallerEqFib ( n ) ; cout << f << " ▁ " ; n = n - f ; } } int main ( ) { int n = 30 ; cout << " Non - neighbouring ▁ Fibonacci ▁ Representation ▁ of ▁ " << n << " ▁ is ▁ STRNEWLINE " ; printFibRepresntation ( n ) ; return 0 ; }
Count number of ways to divide a number in 4 parts | A Dynamic Programming based solution to count number of ways to represent n as sum of four numbers ; " parts " is number of parts left , n is the value left " nextPart " is starting point from where we start trying for next part . ; Base cases ; If this subproblem is already solved ; Count number of ways for remaining number n - i remaining parts " parts - 1" , and for all part varying from ' nextPart ' to ' n ' ; Store computed answer in table and return result ; This function mainly initializes dp table and calls countWaysUtil ( ) ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 5001 ] [ 5001 ] [ 5 ] ; int countWaysUtil ( int n , int parts , int nextPart ) { if ( parts == 0 && n == 0 ) return 1 ; if ( n <= 0 parts <= 0 ) return 0 ; if ( dp [ n ] [ nextPart ] [ parts ] != -1 ) return dp [ n ] [ nextPart ] [ parts ] ; for ( int i = nextPart ; i <= n ; i ++ ) ans += countWaysUtil ( n - i , parts - 1 , i ) ; return ( dp [ n ] [ nextPart ] [ parts ] = ans ) ; } int countWays ( int n ) { memset ( dp , -1 , sizeof ( dp ) ) ; return countWaysUtil ( n , 4 , 1 ) ; } int main ( ) { int n = 8 ; cout << countWays ( n ) << endl ; return 0 ; }
Segmented Sieve | C ++ program to print print all primes smaller than n using segmented sieve ; This functions finds all primes smaller than ' limit ' using simple sieve of eratosthenes . It also stores found primes in vector prime [ ] ; Create a boolean array " mark [ 0 . . n - 1 ] " and initialize all entries of it as true . A value in mark [ p ] will finally be false if ' p ' is Not a prime , else true . ; If p is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers and store them in prime ; Prints all prime numbers smaller than ' n ' ; Compute all primes smaller than or equal to square root of n using simple sieve ; Divide the range [ 0. . n - 1 ] in different segments We have chosen segment size as sqrt ( n ) . ; While all segments of range [ 0. . n - 1 ] are not processed , process one segment at a time ; To mark primes in current range . A value in mark [ i ] will finally be false if ' i - low ' is Not a prime , else true . ; Use the found primes by simpleSieve ( ) to find primes in current range ; Find the minimum number in [ low . . high ] that is a multiple of prime [ i ] ( divisible by prime [ i ] ) For example , if low is 31 and prime [ i ] is 3 , we start with 33. ; Mark multiples of prime [ i ] in [ low . . high ] : We are marking j - low for j , i . e . each number in range [ low , high ] is mapped to [ 0 , high - low ] so if range is [ 50 , 100 ] marking 50 corresponds to marking 0 , marking 51 corresponds to 1 and so on . In this way we need to allocate space only for range ; Numbers which are not marked as false are prime ; Update low and high for next segment ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void simpleSieve ( int limit , vector < int > & prime ) { vector < bool > mark ( limit + 1 , true ) ; for ( int p = 2 ; p * p < limit ; p ++ ) { if ( mark [ p ] == true ) { for ( int i = p * p ; i < limit ; i += p ) mark [ i ] = false ; } } for ( int p = 2 ; p < limit ; p ++ ) { if ( mark [ p ] == true ) { prime . push_back ( p ) ; cout << p << " ▁ " ; } } } void segmentedSieve ( int n ) { int limit = floor ( sqrt ( n ) ) + 1 ; vector < int > prime ; prime . reserve ( limit ) ; simpleSieve ( limit , prime ) ; int low = limit ; int high = 2 * limit ; while ( low < n ) { if ( high >= n ) high = n ; bool mark [ limit + 1 ] ; memset ( mark , true , sizeof ( mark ) ) ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) { int loLim = floor ( low / prime [ i ] ) * prime [ i ] ; if ( loLim < low ) loLim += prime [ i ] ; for ( int j = loLim ; j < high ; j += prime [ i ] ) mark [ j - low ] = false ; } for ( int i = low ; i < high ; i ++ ) if ( mark [ i - low ] == true ) cout << i << " ▁ " ; low = low + limit ; high = high + limit ; } } int main ( ) { int n = 100000 ; cout << " Primes ▁ smaller ▁ than ▁ " << n << " : n " ; segmentedSieve ( n ) ; return 0 ; }
Find the smallest twins in given range | C ++ program to find the smallest twin in given range ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the smallest twin in range ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printTwins ( int low , int high ) { bool prime [ high + 1 ] , twin = false ; memset ( prime , true , sizeof ( prime ) ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p <= floor ( sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= high ; i += p ) prime [ i ] = false ; } } for ( int i = low ; i <= high ; i ++ ) { if ( prime [ i ] && prime [ i + 2 ] ) { cout << " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " << i << " , ▁ " << i + 2 << " ) " ; twin = true ; break ; } } if ( twin == false ) cout << " No ▁ such ▁ pair ▁ exists " << endl ; } int main ( ) { printTwins ( 10 , 100 ) ; return 0 ; }
Check if a given number is Fancy | C ++ program to find if a given number is fancy or not . ; To store mappings of fancy pair characters . For example 6 is paired with 9 and 9 is paired with 6. ; Find number of digits in given number ; Traverse from both ends , and compare characters one by one ; If current characters at both ends are not fancy pairs ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isFancy ( string & num ) { map < char , char > fp ; fp [ '0' ] = '0' ; fp [ '1' ] = '1' ; fp [ '6' ] = '9' ; fp [ '8' ] = '8' ; fp [ '9' ] = '6' ; int n = num . length ( ) ; int l = 0 , r = n - 1 ; while ( l <= r ) { if ( fp . find ( num [ l ] ) == fp . end ( ) fp [ num [ l ] ] != num [ r ] ) return false ; l ++ ; r -- ; } return true ; } int main ( ) { string str = "9088806" ; isFancy ( str ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Find Next Sparse Number | C ++ program to find next sparse number ; Find binary representation of x and store it in bin [ ] . bin [ 0 ] contains least significant bit ( LSB ) , next bit is in bin [ 1 ] , and so on . ; There my be extra bit in result , so add one extra bit ; The position till which all bits are finalized ; Start from second bit ( next to LSB ) ; If current bit and its previous bit are 1 , but next bit is not 1. ; Make the next bit 1 ; Make all bits before current bit as 0 to make sure that we get the smallest next number ; Store position of the bit set so that this bit and bits before it are not changed next time . ; Find decimal equivalent of modified bin [ ] ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nextSparse ( int x ) { vector < bool > bin ; while ( x != 0 ) { bin . push_back ( x & 1 ) ; x >>= 1 ; } bin . push_back ( 0 ) ; int last_final = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( bin [ i ] == 1 && bin [ i - 1 ] == 1 && bin [ i + 1 ] != 1 ) { bin [ i + 1 ] = 1 ; for ( int j = i ; j >= last_final ; j -- ) bin [ j ] = 0 ; last_final = i + 1 ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += bin [ i ] * ( 1 << i ) ; return ans ; } int main ( ) { int x = 38 ; cout << " Next ▁ Sparse ▁ Number ▁ is ▁ " << nextSparse ( x ) ; return 0 ; }
Count number of subsets of a set with GCD equal to a given number | C ++ program to count number of subsets with given GCDs ; n is size of arr [ ] and m is sizeof gcd [ ] ; Map to store frequency of array elements ; Map to store number of subsets with given gcd ; Initialize maximum element . Assumption : all array elements are positive . ; Find maximum element in array and fill frequency map . ; Run a loop from max element to 1 to find subsets with all gcds ; Run a loop for all multiples of i ; Sum the frequencies of every element which is a multiple of i ; Excluding those subsets which have gcd > i but not i i . e . which have gcd as multiple of i in the subset for ex : { 2 , 3 , 4 } considering i = 2 and subset we need to exclude are those having gcd as 4 ; Number of subsets with GCD equal to ' i ' is pow ( 2 , add ) - 1 - sub ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ccountSubsets ( int arr [ ] , int n , int gcd [ ] , int m ) { unordered_map < int , int > freq ; unordered_map < int , int > subsets ; int arrMax = 0 ; for ( int i = 0 ; i < n ; i ++ ) { arrMax = max ( arrMax , arr [ i ] ) ; freq [ arr [ i ] ] ++ ; } for ( int i = arrMax ; i >= 1 ; i -- ) { int sub = 0 ; int add = freq [ i ] ; for ( int j = 2 ; j * i <= arrMax ; j ++ ) { add += freq [ j * i ] ; sub += subsets [ j * i ] ; } subsets [ i ] = ( 1 << add ) - 1 - sub ; } for ( int i = 0 ; i < m ; i ++ ) cout << " Number ▁ of ▁ subsets ▁ with ▁ gcd ▁ " << gcd [ i ] << " ▁ is ▁ " << subsets [ gcd [ i ] ] << endl ; } int main ( ) { int gcd [ ] = { 2 , 3 } ; int arr [ ] = { 9 , 6 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int m = sizeof ( gcd ) / sizeof ( gcd [ 0 ] ) ; ccountSubsets ( arr , n , gcd , m ) ; return 0 ; }
Sum of bit differences among all pairs | C ++ program to compute sum of pairwise bit differences ; traverse over all bits ; count number of elements with i 'th bit set ; Add " count ▁ * ▁ ( n ▁ - ▁ count ) ▁ * ▁ 2" to the answer ; Driver prorgram
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumBitDifferences ( int arr [ ] , int n ) { for ( int i = 0 ; i < 32 ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( ( arr [ j ] & ( 1 << i ) ) ) count ++ ; ans += ( count * ( n - count ) * 2 ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 } ; int n = sizeof arr / sizeof arr [ 0 ] ; cout << sumBitDifferences ( arr , n ) << endl ; return 0 ; }
Print all non | C ++ program to generate all non - increasing sequences of sum equals to x ; Utility function to print array arr [ 0. . n - 1 ] ; Recursive Function to generate all non - increasing sequences with sum x arr [ ] -- > Elements of current sequence curr_sum -- > Current Sum curr_idx -- > Current index in arr [ ] ; If current sum is equal to x , then we found a sequence ; Try placing all numbers from 1 to x - curr_sum at current index ; The placed number must also be smaller than previously placed numbers and it may be equal to the previous stored value , i . e . , arr [ curr_idx - 1 ] if there exists a previous number ; Place number at curr_idx ; Recur ; Try next number ; A wrapper over generateUtil ( ) ; Array to store sequences on by one ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } void generateUtil ( int x , int arr [ ] , int curr_sum , int curr_idx ) { if ( curr_sum == x ) { printArr ( arr , curr_idx ) ; return ; } int num = 1 ; while ( num <= x - curr_sum && ( curr_idx == 0 num <= arr [ curr_idx - 1 ] ) ) { arr [ curr_idx ] = num ; generateUtil ( x , arr , curr_sum + num , curr_idx + 1 ) ; num ++ ; } } void generate ( int x ) { int arr [ x ] ; generateUtil ( x , arr , 0 , 0 ) ; } int main ( ) { int x = 5 ; generate ( x ) ; return 0 ; }
Perfect Number | C ++ program to check if a given number is perfect or not ; Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Driver program
#include <iostream> 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 main ( ) { cout << " Below ▁ are ▁ all ▁ perfect ▁ numbers ▁ till ▁ 10000 STRNEWLINE " ; for ( int n = 2 ; n < 10000 ; n ++ ) if ( isPerfect ( n ) ) cout << n << " ▁ is ▁ a ▁ perfect ▁ number STRNEWLINE " ; return 0 ; }