codeparrot/xlcost-text-to-code huggingface dataset:
Maximize count of odd | C ++ code implementation for above approach ; To find maximum number of pairs in array with conversion of at most one element ; Initialize count of even elements ; Initialize count of odd elements ; If current number is even then increment x by 1 ; If current number is odd then increment y by 1 ; Initialize the answer by min ( x , y ) ; If difference in count of odd and even is more than 2 than increment answer ; Return final answer ; Driver code ; Given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumNumberofpairs ( int n , int arr [ ] ) { int x = 0 ; int y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { x ++ ; } else { y ++ ; } } int answer = min ( x , y ) ; if ( abs ( x - y ) >= 2 ) { answer ++ ; } return answer ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 6 , 5 , 10 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumNumberofpairs ( n , arr ) << endl ; return 0 ; }
Sum of product of all unordered pairs in given range with update queries | C ++ Program for the above approach ; Function to calculate the Pairwise Product Sum in range from L to R ; Loop to iterate over all possible pairs from L to R ; Print answer ; Function to update the Array element at index P to X ; Update the value at Pth index in the array ; Function to solve Q queries ; If Query is of type 1 ; If Query is of type 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pairwiseProductSum ( int a [ ] , int l , int r ) { int sum = 0 ; for ( int j = l - 1 ; j <= r - 1 ; j ++ ) { for ( int k = j + 1 ; k <= r - 1 ; k ++ ) { sum += ( a [ j ] * a [ k ] ) ; } } cout << sum << endl ; } void updateArray ( int * a , int p , int x ) { a [ p - 1 ] = x ; } void solveQueries ( int * a , int n , int Q , int query [ ] [ 3 ] ) { for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } int main ( ) { int A [ ] = { 5 , 7 , 2 , 3 , 1 } ; int N = sizeof ( A ) / sizeof ( int ) ; int Q = 3 ; int query [ Q ] [ 3 ] = { { 1 , 1 , 3 } , { 2 , 2 , 5 } , { 1 , 2 , 5 } } ; solveQueries ( A , N , Q , query ) ; return 0 ; }
Check if final remainder is present in original Array by reducing it based on given conditions | C ++ program to implement above approach ; copying original array ; loop till length of array become 2. ; find middle element ; pop element from front and rear ; find remainder ; append remainder to a ; now since length of array is 2 take product and divide it by n ; if remainder is present is original array return 1 , else return 0 ; calling function Reduced ; if x = 1 print YES else NO
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Reduced ( vector < int > a , int n ) { vector < int > original_array ; original_array = a ; while ( a . size ( ) != 2 ) { int mid = a . size ( ) / 2 ; int mid_ele = a [ mid ] ; int start = a [ 0 ] ; a . erase ( a . begin ( ) ) ; int end = a [ a . size ( ) - 1 ] ; a . pop_back ( ) ; int rmd = ( start * end ) % mid_ele ; a . push_back ( rmd ) ; int remainder = ( a [ 0 ] * a [ 1 ] ) % n ; for ( int i = 0 ; i < original_array . size ( ) ; i ++ ) { if ( original_array [ i ] == remainder ) { return 1 ; } } } return 0 ; } int main ( ) { vector < int > Arr = { 2 , 3 , 4 , 8 , 5 , 7 } ; int N = Arr . size ( ) ; int x = Reduced ( Arr , N ) ; if ( x ) cout << ( " YES " ) ; else cout << ( " NO " ) ; return 0 ; }
Find two numbers from their sum and OR | C ++ program for the above approach ; Function to find the two integers from the given sum and Bitwise OR value ; Check if Z is non negative ; Iterate through all the bits ; Find the kth bit of A & B ; Find the kth bit of A | B ; If bit1 = 1 and bit2 = 0 , then there will be no possible pairs ; Print the possible pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define MaxBit  32 NEW_LINE using namespace std ; int possiblePair ( int X , int Y ) { int Z = Y - X ; if ( Z < 0 ) { cout << " - 1" ; return 0 ; } for ( int k = 0 ; k < MaxBit ; k ++ ) { int bit1 = ( Z >> k ) & 1 ; int bit2 = ( Z >> k ) & 1 ; if ( bit1 && ! bit2 ) { cout << " - 1" ; return 0 ; } } cout << Z << ' ▁ ' << X ; return 0 ; } int main ( ) { int X = 7 , Y = 11 ; possiblePair ( X , Y ) ; return 0 ; }
Maximum frequencies in each M | C ++ program for the above approach ; Function to find the frequency of the most common element in each M length subarrays ; Stores frequency of array element ; Stores the maximum frequency ; Iterate for the first sub - array and store the maximum ; Print the maximum frequency for the first subarray ; Iterate over the range [ M , N ] ; Subtract the A [ i - M ] and add the A [ i ] in the map ; Find the maximum frequency ; Print the maximum frequency for the current subarray ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxFrequencySubarrayUtil ( vector < int > A , int N , int M ) { int i = 0 ; unordered_map < int , int > m ; int val = 0 ; for ( ; i < M ; i ++ ) { m [ A [ i ] ] ++ ; val = max ( val , m [ A [ i ] ] ) ; } cout << val << " ▁ " ; for ( i = M ; i < N ; i ++ ) { m [ A [ i - M ] ] -- ; m [ A [ i ] ] ++ ; val = 0 ; for ( auto x : m ) { val = max ( val , x . second ) ; } cout << val << " ▁ " ; } } int main ( ) { vector < int > A = { 1 , 1 , 2 , 2 , 3 , 5 } ; int N = A . size ( ) ; int M = 4 ; maxFrequencySubarrayUtil ( A , N , M ) ; return 0 ; }
Maximum number of pairs of distinct array elements possible by including each element in only one pair | C ++ program for the above approach ; Function to count the maximum number of pairs having different element from the given array ; Stores the frequency of array element ; Stores maximum count of pairs ; Increasing the frequency of every element ; Stores the frequencies of array element from highest to lowest ; Pushing the frequencies to the priority queue ; Iterate until size of PQ > 1 ; Stores the top two element ; Form the pair between the top two pairs ; Decrement the frequencies ; Insert updated frequencies if it is greater than 0 ; Return the total count of resultant pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumPairs ( int a [ ] , int n ) { map < int , int > freq ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) freq [ a [ i ] ] ++ ; priority_queue < int > pq ; for ( auto itr = freq . begin ( ) ; itr != freq . end ( ) ; itr ++ ) { pq . push ( itr -> second ) ; } while ( pq . size ( ) > 1 ) { int freq1 = pq . top ( ) ; pq . pop ( ) ; int freq2 = pq . top ( ) ; pq . pop ( ) ; count ++ ; freq1 -- ; freq2 -- ; if ( freq1 > 0 ) pq . push ( freq1 ) ; if ( freq2 > 0 ) pq . push ( freq2 ) ; } return count ; } int main ( ) { int arr [ ] = { 4 , 2 , 4 , 1 , 4 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumPairs ( arr , N ) ; return 0 ; }
Generate an N | C ++ program for above approach ; Function to print target array ; Sort the given array ; Seeking for index of elements with minimum diff . ; Seeking for index ; To store target array ; Copying element ; Copying remaining element ; Printing target array ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int minDifference = INT_MAX ; int minIndex = -1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( minDifference > abs ( arr [ i ] - arr [ i - 1 ] ) ) { minDifference = abs ( arr [ i ] - arr [ i - 1 ] ) ; minIndex = i - 1 ; } } int Arr [ n ] ; Arr [ 0 ] = arr [ minIndex ] ; Arr [ n - 1 ] = arr [ minIndex + 1 ] ; int pos = 1 ; for ( int i = minIndex + 2 ; i < n ; i ++ ) { Arr [ pos ++ ] = arr [ i ] ; } for ( int i = 0 ; i < minIndex ; i ++ ) { Arr [ pos ++ ] = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { cout << Arr [ i ] << " ▁ " ; } } int main ( ) { int N = 8 ; int arr [ ] = { 4 , 6 , 2 , 6 , 8 , 2 , 6 , 4 } ; printArr ( arr , N ) ; return 0 ; }
Construct a Perfect Binary Tree from Preorder Traversal | C ++ program for the above approach ; Structure of the tree ; Function to create a new node with the value val ; Return the newly created node ; Function to create the Perfect Binary Tree ; If preStart > preEnd return NULL ; Initialize root as pre [ preStart ] ; If the only node is left , then return node ; Parameters for further recursion ; Recursive Call to build the subtree of root node ; Return the created root ; Function to build Perfect Binary Tree ; Function to print the Inorder of the given Tree ; Base Case ; Left Recursive Call ; Print the data ; Right Recursive Call ; Driver Code ; Function Call ; Print Inorder Traversal
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; Node ( int val ) { data = val ; left = right = NULL ; } } ; Node * getNewNode ( int val ) { Node * newNode = new Node ( val ) ; newNode -> data = val ; newNode -> left = newNode -> right = NULL ; return newNode ; } Node * buildPerfectBT_helper ( int preStart , int preEnd , int pre [ ] ) { if ( preStart > preEnd ) return NULL ; Node * root = getNewNode ( pre [ preStart ] ) ; ; if ( preStart == preEnd ) return root ; int leftPreStart = preStart + 1 ; int rightPreStart = leftPreStart + ( preEnd - leftPreStart + 1 ) / 2 ; int leftPreEnd = rightPreStart - 1 ; int rightPreEnd = preEnd ; root -> left = buildPerfectBT_helper ( leftPreStart , leftPreEnd , pre ) ; root -> right = buildPerfectBT_helper ( rightPreStart , rightPreEnd , pre ) ; return root ; } Node * buildPerfectBT ( int pre [ ] , int size ) { return buildPerfectBT_helper ( 0 , size - 1 , pre ) ; } void printInorder ( Node * root ) { if ( ! root ) return ; printInorder ( root -> left ) ; cout << root -> data << " ▁ " ; printInorder ( root -> right ) ; } int main ( ) { int pre [ ] = { 1 , 2 , 4 , 5 , 3 , 6 , 7 } ; int N = sizeof ( pre ) / sizeof ( pre [ 0 ] ) ; Node * root = buildPerfectBT ( pre , N ) ; cout << " Inorder traversal of the tree : " ; printInorder ( root ) ; return 0 ; }
Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number | C ++ program for the above approach . ; Function to find the minimum number of moves to make N and M equal . ; Array to maintain the numbers included . ; pair of vertex , count ; run bfs from N ; if we reached goal ; Iterate in the range ; If i is a factor of aux ; If i is less than M - aux and is not included earlier . ; If aux / i is less than M - aux and is not included earlier . ; Not possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOperations ( int N , int M ) { bool visited [ 100001 ] ; fill ( visited , visited + 100001 , false ) ; queue < pair < int , int > > Q ; Q . push ( make_pair ( N , 0 ) ) ; visited [ N ] = true ; while ( ! Q . empty ( ) ) { int aux = Q . front ( ) . first ; int cont = Q . front ( ) . second ; Q . pop ( ) ; if ( aux == M ) return cont ; for ( int i = 2 ; i * i <= aux ; i ++ ) if ( aux % i == 0 ) { if ( aux + i <= M && ! visited [ aux + i ] ) { Q . push ( make_pair ( aux + i , cont + 1 ) ) ; visited [ aux + i ] = true ; } if ( aux + aux / i <= M && ! visited [ aux + aux / i ] ) { Q . push ( make_pair ( aux + aux / i , cont + 1 ) ) ; visited [ aux + aux / i ] = true ; } } } return -1 ; } int main ( ) { int N = 4 , M = 24 ; cout << countOperations ( N , M ) ; return 0 ; }
Finding Astronauts from different countries | C ++ program for the above approach ; Function to perform the DFS Traversal to find the count of connected components ; Marking vertex visited ; DFS call to neighbour vertices ; If the current node is not visited , then recursively call DFS ; Function to find the number of ways to choose two astronauts from the different countries ; Stores the Adjacency list ; Constructing the graph ; Stores the visited vertices ; Stores the size of every connected components ; DFS call to the graph ; Store size of every connected component ; Stores the total number of ways to count the pairs ; Traverse the array ; Print the value of ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( int v , vector < vector < int > > & adj , vector < bool > & visited , int & num ) { visited [ v ] = true ; num ++ ; for ( int i = 0 ; i < adj [ v ] . size ( ) ; i ++ ) { if ( ! visited [ adj [ v ] [ i ] ] ) { dfs ( adj [ v ] [ i ] , adj , visited , num ) ; } } } void numberOfPairs ( int N , vector < vector < int > > arr ) { vector < vector < int > > adj ( N ) ; for ( vector < int > & i : arr ) { adj [ i [ 0 ] ] . push_back ( i [ 1 ] ) ; adj [ i [ 1 ] ] . push_back ( i [ 0 ] ) ; } vector < bool > visited ( N ) ; vector < int > v ; int num = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { dfs ( i , adj , visited , num ) ; v . push_back ( num ) ; num = 0 ; } } int ans = N * ( N - 1 ) / 2 ; for ( int i : v ) { ans -= ( i * ( i - 1 ) / 2 ) ; } cout << ans ; } int main ( ) { int N = 6 ; vector < vector < int > > arr = { { 0 , 1 } , { 0 , 2 } , { 2 , 5 } } ; numberOfPairs ( N , arr ) ; return 0 ; }
Count of Perfect Numbers in given range for Q queries | C ++ program for the above approach ; Function to check whether a number is perfect Number ; Stores sum of divisors ; Itearate over the range [ 2 , sqrt ( N ) ] ; If sum of divisors is equal to N , then N is a perfect number ; Function to find count of perfect numbers in a given range ; Stores the count of perfect Numbers upto a every number less than MAX ; Iterate over the range [ 1 , MAX ] ; Traverse the array arr [ ] ; Print the count of perfect numbers in the range [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100005 ; 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 ; } void Query ( int arr [ ] [ 2 ] , int N ) { int prefix [ MAX + 1 ] = { 0 } ; for ( int i = 2 ; i <= MAX ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + isPerfect ( i ) ; } for ( int i = 0 ; i < N ; i ++ ) { cout << prefix [ arr [ i ] [ 1 ] ] - prefix [ arr [ i ] [ 0 ] - 1 ] << " ▁ " ; } } int main ( ) { int arr [ ] [ 2 ] = { { 1 , 1000 } , { 1000 , 2000 } , { 2000 , 3000 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Query ( arr , N ) ; }
2 Keys Keyboard Problem | C ++ program for the above approach ; Function to find the minimum number of steps required to form N number of A 's ; Stores the count of steps needed ; Traverse over the range [ 2 , N ] ; Iterate while N is divisible by d ; Increment the value of ans by d ; Divide N by d ; If N is not 1 ; Return the ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSteps ( int N ) { int ans = 0 ; for ( int d = 2 ; d * d <= N ; d ++ ) { while ( N % d == 0 ) { ans += d ; N /= d ; } } if ( N != 1 ) { ans += N ; } return ans ; } int main ( ) { int N = 3 ; cout << minSteps ( N ) ; return 0 ; }
Find any possible two coordinates of Rectangle whose two coordinates are given | C ++ program for the above approach ; Function to find the remaining two rectangle coordinates ; Pairs to store the position of given two coordinates of the rectangle . ; Pairs to store the remaining two coordinates of the rectangle . ; Traverse through matrix and find pairs p1 and p2 ; First Case ; Second Case ; Third Case ; Print the matrix ; Driver code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Create_Rectangle ( vector < string > arr , int n ) { pair < int , int > p1 = { -1 , -1 } ; pair < int , int > p2 = { -1 , -1 } ; pair < int , int > p3 ; pair < int , int > p4 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] [ j ] == '1' ) if ( p1 . first == -1 ) p1 = { i , j } ; else p2 = { i , j } ; } } p3 = p1 ; p4 = p2 ; if ( p1 . first == p2 . first ) { p3 . first = ( p1 . first + 1 ) % n ; p4 . first = ( p2 . first + 1 ) % n ; } else if ( p1 . second == p2 . second ) { p3 . second = ( p1 . second + 1 ) % n ; p4 . second = ( p2 . second + 1 ) % n ; } else { swap ( p3 . first , p4 . first ) ; } arr [ p3 . first ] [ p3 . second ] = '1' ; arr [ p4 . first ] [ p4 . second ] = '1' ; for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << endl ; } } int main ( ) { int n = 4 ; vector < string > arr { "0010" , "0000" , "1000" , "0000" } ; Create_Rectangle ( arr , n ) ; return 0 ; }
Sum of all the prime divisors of a number | Set 2 | C ++ program for the above approach ; Function to find sum of prime divisors of the given number N ; Add the number 2 if it divides N ; Traverse the loop from [ 3 , sqrt ( N ) ] ; If i divides N , add i and divide N ; This condition is to handle the case when N is a prime number greater than 2 ; Driver code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SumOfPrimeDivisors ( int n ) { int sum = 0 ; if ( n % 2 == 0 ) { sum = sum + 2 ; } while ( n % 2 == 0 ) { n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { sum = sum + i ; } while ( n % i == 0 ) { n = n / i ; } } if ( n > 2 ) { sum = sum + n ; } return sum ; } int main ( ) { int n = 10 ; cout << SumOfPrimeDivisors ( n ) ; return 0 ; }
Count bases which contains a set bit as the Most Significant Bit in the representation of N | C ++ program for the above approach ; Function to count bases having MSB of N as a set bit ; Store the required count ; Iterate over the range [ 2 , N ] ; Store the MSB of N ; If MSB is 1 , then increment the count by 1 ; Return the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfBase ( int N ) { int count = 0 ; for ( int i = 2 ; i <= N ; ++ i ) { int highestPower = ( int ) ( log ( N ) / log ( i ) ) ; int firstDigit = N / ( int ) pow ( i , highestPower ) ; if ( firstDigit == 1 ) { ++ count ; } } return count ; } int main ( ) { int N = 6 ; cout << countOfBase ( N ) ; return 0 ; }
K | c ++ program for the above approach ; Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Convert integer into string ; If k is greater than length of the string temp ; Print the k digit from last ; Driver code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kthDigitFromLast ( int n , int k ) { if ( k <= 0 ) { cout << -1 << endl ; return ; } string temp = to_string ( n ) ; if ( k > temp . length ( ) ) { cout << -1 << endl ; } else { cout << temp [ temp . length ( ) - k ] - '0' ; } } int main ( ) { int n = 2354 ; int k = 2 ; kthDigitFromLast ( n , k ) ; }
Count ways to split array into three non | C ++ program for the above approach ; Function to count ways to split array into three subarrays with equal Bitwise XOR ; Stores the XOR value of arr [ ] ; Update the value of arr_xor ; Stores the XOR value of prefix and suffix array respectively ; Stores the ending points of all the required prefix arrays ; Stores the count of suffix arrays whose XOR value is equal to the total XOR value at each index ; Find all prefix arrays with XOR value equal to arr_xor ; Update pref_xor ; Fill the values of suff_inds [ ] ; Update suff_xor ; Update suff_inds [ i ] ; Stores the total number of ways ; Count total number of ways ; Return the final count ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWays ( int arr [ ] , int N ) { int arr_xor = 0 ; for ( int i = 0 ; i < N ; i ++ ) arr_xor ^= arr [ i ] ; int pref_xor = 0 , suff_xor = 0 ; vector < int > pref_ind ; int suff_inds [ N + 1 ] ; memset ( suff_inds , 0 , sizeof suff_inds ) ; for ( int i = 0 ; i < N ; i ++ ) { pref_xor ^= arr [ i ] ; if ( pref_xor == arr_xor ) pref_ind . push_back ( i ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { suff_xor ^= arr [ i ] ; suff_inds [ i ] += suff_inds [ i + 1 ] ; if ( suff_xor == arr_xor ) suff_inds [ i ] ++ ; } int tot_ways = 0 ; for ( int idx : pref_ind ) { if ( idx < N - 1 ) tot_ways += suff_inds [ idx + 2 ] ; } return tot_ways ; } int main ( ) { int arr [ ] = { 7 , 0 , 5 , 2 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countWays ( arr , N ) ; return 0 ; }
Count pairs up to N having sum equal to their XOR | C ++ program for the above approach ; 2D array for memoization ; Recursive Function to count pairs ( x , y ) such that x + y = x ^ y ; If the string is traversed completely ; If the current subproblem is already calculated ; If bound = 1 and s [ i ] = = '0' , only ( 0 , 0 ) can be placed ; Otherwise ; Placing ( 0 , 1 ) and ( 1 , 0 ) are equivalent . Hence , multiply by 2. ; Place ( 0 , 0 ) at the current position . ; Return the answer ; Utility Function to convert N to its binary representation ; Function to count pairs ( x , y ) such that x + y = x ^ y ; Convert the number to equivalent binary representation ; Initialize dp array with - 1. ; Print answer returned by recursive function ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 1000 ] [ 2 ] ; int IsSumEqualsXor ( int i , int n , bool bound , string & s ) { if ( i == n ) return 1 ; if ( dp [ i ] [ bound ] != -1 ) return dp [ i ] [ bound ] ; int ans = 0 ; if ( bound and s [ i ] == '0' ) { ans = IsSumEqualsXor ( i + 1 , n , 1 , s ) ; } else { ans = 2 * IsSumEqualsXor ( i + 1 , n , bound & ( s [ i ] == '1' ) , s ) ; ans += IsSumEqualsXor ( i + 1 , n , 0 , s ) ; } return dp [ i ] [ bound ] = ans ; } string convertToBinary ( int n ) { string ans ; while ( n ) { char rem = char ( n % 2 + '0' ) ; ans . push_back ( rem ) ; n /= 2 ; } reverse ( ans . begin ( ) , ans . end ( ) ) ; return ans ; } void IsSumEqualsXorUtil ( int N ) { string s = convertToBinary ( N ) ; memset ( dp , -1 , sizeof dp ) ; cout << IsSumEqualsXor ( 0 , s . size ( ) , 1 , s ) << endl ; } int main ( ) { int N = 10 ; IsSumEqualsXorUtil ( N ) ; return 0 ; }
Mean of fourth powers of first N natural numbers | C ++ program for the above approach ; Function to find the average of the fourth power of first N natural numbers ; Stores the sum of the fourth powers of first N natural numbers ; Calculate the sum of fourth power ; Return the average ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findAverage ( int N ) { double S = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { S += i * i * i * i ; } return S / N ; } int main ( ) { int N = 3 ; cout << findAverage ( N ) ; return 0 ; }
Construct a graph from given degrees of all vertices | C ++ program to generate a graph for a given fixed degrees ; A function to print the adjacency matrix . ; n is number of vertices ; For each pair of vertex decrement the degree of both vertex . ; Print the result in specified format ; driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMat ( int degseq [ ] , int n ) { int mat [ n ] [ n ] ; memset ( mat , 0 , sizeof ( mat ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) { degseq [ i ] -- ; degseq [ j ] -- ; mat [ i ] [ j ] = 1 ; mat [ j ] [ i ] = 1 ; } } } cout << " STRNEWLINE " << setw ( 3 ) << " TABSYMBOL " ; for ( int i = 0 ; i < n ; i ++ ) cout << setw ( 3 ) << " ( " << i << " ) " ; cout << " STRNEWLINE STRNEWLINE " ; for ( int i = 0 ; i < n ; i ++ ) { cout << setw ( 4 ) << " ( " << i << " ) " ; for ( int j = 0 ; j < n ; j ++ ) cout << setw ( 5 ) << mat [ i ] [ j ] ; cout << " STRNEWLINE " ; } } int main ( ) { int degseq [ ] = { 2 , 2 , 1 , 1 , 1 } ; int n = sizeof ( degseq ) / sizeof ( degseq [ 0 ] ) ; printMat ( degseq , n ) ; return 0 ; }