codeparrot/xlcost-text-to-code huggingface dataset:
Minimum cost to merge numbers from 1 to N | C ++ program to find the Minimum cost to merge numbers from 1 to N . ; Function returns the minimum cost ; Min Heap representation ; Add all elements to heap ; First minimum ; Second minimum ; Multiply them ; Add to the cost ; Push the product into the heap again ; Return the optimal cost ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GetMinCost ( int N ) { priority_queue < int , vector < int > , greater < int > > pq ; for ( int i = 1 ; i <= N ; i ++ ) { pq . push ( i ) ; } int cost = 0 ; while ( pq . size ( ) > 1 ) { int mini = pq . top ( ) ; pq . pop ( ) ; int secondmini = pq . top ( ) ; pq . pop ( ) ; int current = mini * secondmini ; cost += current ; pq . push ( current ) ; } return cost ; } int main ( ) { int N = 5 ; cout << GetMinCost ( N ) ; }
Permutation of first N positive integers such that prime numbers are at prime indices | Set 2 | C ++ program to count permutations from 1 to N such that prime numbers occur at prime indices ;  ; Computing count of prime numbers using sieve ; Computing permutations for primes ; Computing permutations for non - primes ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; static const int MOD = 1e9 + 7 ; int numPrimeArrangements ( int n ) { vector < bool > prime ( n + 1 , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( prime [ i ] ) for ( int factor = 2 ; factor * i <= n ; factor ++ ) prime [ factor * i ] = false ; } int primeIndices = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( prime [ i ] ) primeIndices ++ ; int mod = 1e9 + 7 , res = 1 ; for ( int i = 1 ; i <= primeIndices ; i ++ ) res = ( 1LL * res * i ) % mod ; for ( int i = 1 ; i <= ( n - primeIndices ) ; i ++ ) res = ( 1LL * res * i ) % mod ; return res ; } int main ( ) { int N = 5 ; cout << numPrimeArrangements ( N ) ; return 0 ; }
Probability of getting K heads in N coin tosses | C ++ program to find probability of getting K heads in N coin tosses ; function to calculate factorial ; apply the formula ; Driver function ; call count_heads with n and r
#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 ; } double count_heads ( int n , int r ) { double output ; output = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; output = output / ( pow ( 2 , n ) ) ; return output ; } int main ( ) { int n = 4 , r = 3 ; cout << count_heads ( n , r ) ; return 0 ; }
Largest component size in a graph formed by connecting non |  ; mark this node as visited ; apply dfs and add nodes belonging to this component ; create graph and store in adjacency list form ; iterate over all pair of nodes ; if not co - prime ; build undirected graph ; visited array for dfs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dfs ( int u , vector < int > * adj , int vis [ ] ) { vis [ u ] = 1 ; int componentSize = 1 ; for ( auto it : adj [ u ] ) { if ( ! vis [ it ] ) { componentSize += dfs ( it , adj , vis ) ; } } return componentSize ; } int maximumComponentSize ( int a [ ] , int n ) { vector < int > adj [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( __gcd ( a [ i ] , a [ j ] ) > 1 ) adj [ i ] . push_back ( j ) ; adj [ j ] . push_back ( i ) ; } } int answer = 0 ; int vis [ n ] ; for ( int k = 0 ; k < n ; k ++ ) { vis [ k ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ! vis [ i ] ) { answer = max ( answer , dfs ( i , adj , vis ) ) ; } } return answer ; } int main ( ) { int n = 8 ; int A [ ] = { 2 , 3 , 6 , 7 , 4 , 12 , 21 , 39 } J2 : J6 ; cout << maximumComponentSize ( A , n ) ; return 0 ; }
Largest component size in a graph formed by connecting non |  ; smallest prime factor ; Sieve of Eratosthenes ; is spf [ i ] = 0 , then it 's prime ; smallest prime factor of all multiples is i ; Prime factorise n , and store prime factors in set s ; for implementing DSU ; root of component of node i ; finding root as well as applying path compression ; merging two components ; find roots of both components ; if already belonging to the same compenent ; Union by rank , the rank in this case is sizeContainer of the component . Smaller sizeContainer will be merged into larger , so the larger 's root will be  final root ; Function to find the maximum sized container ; intitalise the parents , and component sizeContainer ; intitally all component sizeContainers are 1 ans each node it parent of itself ; store prime factors of a [ i ] in s ; if this prime is seen as a factor for the first time ; if not then merge with that component in which this prime was previously seen ; maximum of sizeContainer of all components ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int spf [ 100005 ] ; void sieve ( ) { for ( int i = 2 ; i < 100005 ; i ++ ) { if ( spf [ i ] == 0 ) { spf [ i ] = i ; for ( int j = 2 * i ; j < 100005 ; j += i ) { if ( spf [ j ] == 0 ) spf [ j ] = i ; } } } } void factorize ( int n , set < int > & s ) { while ( n > 1 ) { int z = spf [ n ] ; s . insert ( z ) ; while ( n % z == 0 ) n /= z ; } } int id [ 100005 ] ; int par [ 100005 ] ; int sizeContainer [ 100005 ] ; int root ( int i ) { if ( par [ i ] == i ) return i ; else return par [ i ] = root ( par [ i ] ) ; } void merge ( int a , int b ) { int p = root ( a ) ; int q = root ( b ) ; if ( p == q ) return ; if ( sizeContainer [ p ] > sizeContainer [ q ] ) swap ( p , q ) ; par [ p ] = q ; sizeContainer [ q ] += sizeContainer [ p ] ; } int maximumComponentsizeContainer ( int a [ ] , int n ) { for ( int i = 0 ; i < 100005 ; i ++ ) { par [ i ] = i ; sizeContainer [ i ] = 1 ; } sieve ( ) ; for ( int i = 0 ; i < n ; i ++ ) { set < int > s ; factorize ( a [ i ] , s ) ; for ( auto it : s ) { if ( id [ it ] == 0 ) id [ it ] = i + 1 ; else merge ( i + 1 , id [ it ] ) ; } } int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) answer = max ( answer , sizeContainer [ i ] ) ; return answer ; } int main ( ) { int n = 8 ; int A [ ] = { 2 , 3 , 6 , 7 , 4 , 12 , 21 , 39 } ; cout << maximumComponentsizeContainer ( A , n ) ; return 0 ; }
Count of nodes in a Binary Tree whose child is its prime factors | C ++ program for Counting nodes whose immediate children are its factors ; To store all prime numbers ; Create a boolean array " prime [ 0 . . N ] " and initialize all its entries as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiples of p and are less than p ^ 2 are already marked . ; A Tree node ; Utility function to create a new node ; Function to check if immediate children of a node are its factors or not ; Function to get the count of full Nodes in a binary tree ; If tree is empty ; Initialize count of full nodes having children as their factors ; If only right child exist ; If only left child exist ; Both left and right child exist ; Check for left child ; Check for right child ; Driver Code ; Create Binary Tree as shown ; To save all prime numbers ; Print Count of all nodes having children as their factors
#include <bits/stdc++.h> NEW_LINE using namespace std ; int N = 1000000 ; vector < int > prime ; void SieveOfEratosthenes ( ) { bool check [ N + 1 ] ; memset ( check , true , sizeof ( check ) ) ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( check [ p ] == true ) { prime . push_back ( p ) ; for ( int i = p * p ; i <= N ; i += p ) check [ i ] = false ; } } } struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } bool IsChilrenPrimeFactor ( struct Node * parent , struct Node * a ) { if ( prime [ a -> key ] && ( parent -> key % a -> key == 0 ) ) return true ; else return false ; } unsigned int GetCount ( struct Node * node ) { if ( ! node ) return 0 ; queue < Node * > q ; int count = 0 ; q . push ( node ) ; while ( ! q . empty ( ) ) { struct Node * temp = q . front ( ) ; q . pop ( ) ; if ( temp -> left == NULL && temp -> right != NULL ) { if ( IsChilrenPrimeFactor ( temp , temp -> right ) ) count ++ ; } else { if ( temp -> right == NULL && temp -> left != NULL ) { if ( IsChilrenPrimeFactor ( temp , temp -> left ) ) count ++ ; } else { if ( temp -> left != NULL && temp -> right != NULL ) { if ( IsChilrenPrimeFactor ( temp , temp -> right ) && IsChilrenPrimeFactor ( temp , temp -> left ) ) count ++ ; } } } if ( temp -> left != NULL ) q . push ( temp -> left ) ; if ( temp -> right != NULL ) q . push ( temp -> right ) ; } return count ; } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 18 ) ; root -> right -> right = newNode ( 12 ) ; root -> right -> left -> left = newNode ( 2 ) ; root -> right -> left -> right = newNode ( 3 ) ; root -> right -> right -> left = newNode ( 3 ) ; root -> right -> right -> right = newNode ( 14 ) ; root -> right -> right -> right -> left = newNode ( 7 ) ; SieveOfEratosthenes ( ) ; cout << GetCount ( root ) << endl ; return 0 ; }
Check if original Array is retained after performing XOR with M exactly K times | C ++ implementation for the above mentioned problem ; Function to check if original Array can be retained by performing XOR with M exactly K times ; Check if O is present or not ; If K is odd and 0 is not present then the answer will always be No . ; Else it will be Yes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string check ( int Arr [ ] , int n , int M , int K ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Arr [ i ] == 0 ) flag = 1 ; } if ( K % 2 != 0 && flag == 0 ) return " No " ; else return " Yes " ; } int main ( ) { int Arr [ ] = { 1 , 1 , 2 , 4 , 7 , 8 } ; int M = 5 ; int K = 6 ; int n = sizeof ( Arr ) / sizeof ( Arr [ 0 ] ) ; cout << check ( Arr , n , M , K ) ; return 0 ; }
Dixon 's Factorization Method with implementation | C ++ implementation of Dixon factorization algo ; Function to find the factors of a number using the Dixon Factorization Algorithm ; Factor base for the given number ; Starting from the ceil of the root of the given number N ; Storing the related squares ; For every number from the square root Till N ; Finding the related squares ; If the two numbers are the related squares , then append them to the array ; For every pair in the array , compute the GCD such that ; If we find a factor other than 1 , then appending it to the final factor array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #include <vector> NEW_LINE void factor ( int n ) { int base [ 4 ] = { 2 , 3 , 5 , 7 } ; int start = int ( sqrt ( n ) ) ; vector < vector < int > > pairs ; int len = sizeof ( base ) / sizeof ( base [ 0 ] ) ; for ( int i = start ; i < n ; i ++ ) { vector < int > v ; for ( int j = 0 ; j < len ; j ++ ) { int lhs = ( ( int ) pow ( i , 2 ) ) % n ; int rhs = ( ( int ) pow ( base [ j ] , 2 ) ) % n ; if ( lhs == rhs ) { v . push_back ( i ) ; v . push_back ( base [ j ] ) ; pairs . push_back ( v ) ; } } } vector < int > newvec ; len = pairs . size ( ) ; for ( int i = 0 ; i < len ; i ++ ) { int factor = __gcd ( pairs [ i ] [ 0 ] - pairs [ i ] [ 1 ] , n ) ; if ( factor != 1 ) newvec . push_back ( factor ) ; } set < int > s ; for ( int i = 0 ; i < newvec . size ( ) ; i ++ ) s . insert ( newvec [ i ] ) ; for ( auto i = s . begin ( ) ; i != s . end ( ) ; i ++ ) cout << ( * i ) << " ▁ " ; } int main ( ) { factor ( 23449 ) ; }
Longest sub | C ++ program to find longest subarray of prime numbers ; To store the prime numbers ; Function that find prime numbers till limit ; Find primes using Sieve of Eratosthenes ; Function that finds all prime numbers in given range using Segmented Sieve ; Find the limit ; To store the prime numbers ; Comput all primes less than or equals to sqrt ( high ) using Simple Sieve ; Count the elements in the range [ low , high ] ; Declaring boolean for the range [ low , high ] ; Traverse the prime numbers till limit ; Find the minimum number in [ low . . high ] that is a multiple of prime [ i ] ; Mark the multiples of prime [ i ] in [ low , high ] as true ; Element which are not marked in range are Prime ; Function that finds longest subarray of prime numbers ; If element is Non - prime then updated current_max to 0 ; If element is prime , then update current_max and max_so_far ; Return the count of longest subarray ; Driver Code ; Find minimum and maximum element ; Find prime in the range [ min_el , max_el ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_set < int > allPrimes ; void simpleSieve ( int limit , vector < int > & prime ) { bool mark [ limit + 1 ] ; memset ( mark , false , sizeof ( mark ) ) ; for ( int i = 2 ; i <= limit ; ++ i ) { if ( mark [ i ] == false ) { prime . push_back ( i ) ; for ( int j = i ; j <= limit ; j += i ) { mark [ j ] = true ; } } } } void primesInRange ( int low , int high ) { int limit = floor ( sqrt ( high ) ) + 1 ; vector < int > prime ; simpleSieve ( limit , prime ) ; int n = high - low + 1 ; bool mark [ n + 1 ] ; memset ( mark , false , sizeof ( mark ) ) ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) { int loLim = floor ( low / prime [ i ] ) ; loLim *= prime [ i ] ; if ( loLim < low ) { loLim += prime [ i ] ; } if ( loLim == prime [ i ] ) { loLim += prime [ i ] ; } for ( int j = loLim ; j <= high ; j += prime [ i ] ) mark [ j - low ] = true ; } for ( int i = low ; i <= high ; i ++ ) { if ( ! mark [ i - low ] ) { allPrimes . insert ( i ) ; } } } int maxPrimeSubarray ( int arr [ ] , int n ) { int current_max = 0 ; int max_so_far = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! allPrimes . count ( arr [ i ] ) ) current_max = 0 ; else { current_max ++ ; max_so_far = max ( current_max , max_so_far ) ; } } return max_so_far ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 3 , 29 , 11 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int max_el = * max_element ( arr , arr + n ) ; int min_el = * min_element ( arr , arr + n ) ; primesInRange ( min_el , max_el ) ; cout << maxPrimeSubarray ( arr , n ) ; return 0 ; }
Sum of all armstrong numbers lying in the range [ L , R ] for Q queries | C ++ program to find the sum of all armstrong numbers in the given range ; pref [ ] array to precompute the sum of all armstrong number ; Function that return number num if num is armstrong else return 0 ; Function to precompute the sum of all armstrong numbers upto 100000 ; checkarmstrong ( ) return the number i if i is armstrong else return 0 ; Function to print the sum for each query ; Function to prsum of all armstrong numbers between [ L , R ] ; Function that pre computes the sum of all armstrong numbers ; Iterate over all Queries to print the sum ; Driver code ; Queries ; Function that print the the sum of all armstrong number in Range [ L , R ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pref [ 100001 ] = { 0 } ; static int checkArmstrong ( int x ) { int n = to_string ( x ) . size ( ) ; int sum1 = 0 ; int temp = x ; while ( temp > 0 ) { int digit = temp % 10 ; sum1 += pow ( digit , n ) ; temp /= 10 ; } if ( sum1 == x ) return x ; return 0 ; } void preCompute ( ) { for ( int i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + checkArmstrong ( i ) ; } } void printSum ( int L , int R ) { cout << ( pref [ R ] - pref [ L - 1 ] ) << endl ; } void printSumarmstrong ( int arr [ 2 ] [ 2 ] , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } int main ( ) { int Q = 2 ; int arr [ 2 ] [ 2 ] = { { 1 , 13 } , { 121 , 211 } } ; printSumarmstrong ( arr , Q ) ; return 0 ; }
Count of pairs in an array whose product is a perfect square | C ++ program to calculate the number of pairs with product is perfect square ; Prime [ ] array to calculate Prime Number ; Array k [ ] to store the value of k for each element in arr [ ] ; For value of k , Sieve function is implemented ; Initialize k [ i ] to i ; Prime Sieve ; If i is prime then remove all factors of prime from it ; Update that j is not prime ; Remove all square divisors i . e . if k [ j ] is divisible by i * i then divide it by i * i ; Function that return total count of pairs with perfect square product ; Map used to store the frequency of k ; Store the frequency of k ; The total number of pairs is the summation of ( fi * ( fi - 1 ) ) / 2 ; Driver code ; Size of arr [ ] ; To pre - compute the value of k ; Function that return total count of pairs with perfect square product
#include <bits/stdc++.h> NEW_LINE using namespace std ; int prime [ 100001 ] = { 0 } ; int k [ 100001 ] = { 0 } ; void Sieve ( ) { for ( int i = 1 ; i < 100001 ; i ++ ) k [ i ] = i ; for ( int i = 2 ; i < 100001 ; i ++ ) { if ( prime [ i ] == 0 ) for ( int j = i ; j < 100001 ; j += i ) { prime [ j ] = 1 ; while ( k [ j ] % ( i * i ) == 0 ) k [ j ] /= ( i * i ) ; } } } int countPairs ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) { freq [ k [ arr [ i ] ] ] ++ ; } int sum = 0 ; for ( auto i : freq ) { sum += ( ( i . second - 1 ) * i . second ) / 2 ; } return sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 8 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; Sieve ( ) ; cout << countPairs ( arr , n ) << endl ; return 0 ; }
Sum of all Perfect Cubes lying in the range [ L , R ] for Q queries | C ++ program to find the sum of all perfect cubes in the given range ; Array to precompute the sum of cubes from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect Cube or not ; Find floating point value of cube root of x . ; If cube root of x is cr return the x , else 0 ; Function to precompute the perfect Cubes upto 100000. ; Function to print the sum for each query ; Driver code ; To calculate the precompute function ; Calling the printSum function for every query
#include <bits/stdc++.h> NEW_LINE #define ll  int NEW_LINE using namespace std ; long long pref [ 100010 ] ; int isPerfectCube ( long long int x ) { long double cr = round ( cbrt ( x ) ) ; return ( cr * cr * cr == x ) ? x : 0 ; } void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectCube ( i ) ; } } void printSum ( int L , int R ) { int sum = pref [ R ] - pref [ L - 1 ] ; cout << sum << " ▁ " ; } int main ( ) { compute ( ) ; int Q = 4 ; int arr [ ] [ 2 ] = { { 1 , 10 } , { 1 , 100 } , { 2 , 25 } , { 4 , 50 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } return 0 ; }
Total number of Subsets of size at most K | C ++ code to find total number of Subsets of size at most K ; Function to compute the value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate sum of nCj from j = 1 to k ; Calling the nCr function for each value of j ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int count ( int n , int k ) { int sum = 0 ; for ( int j = 1 ; j <= k ; j ++ ) { sum = sum + binomialCoeff ( n , j ) ; } return sum ; } int main ( ) { int n = 3 , k = 2 ; cout << count ( n , k ) ; n = 5 , k = 2 ; cout << count ( n , k ) ; return 0 ; }
Tree Isomorphism Problem | A C ++ program to check if two given trees are isomorphic ; A binary tree node has data , pointer to left and right children ; Given a binary tree , print its nodes in reverse level order ; Both roots are NULL , trees isomorphic by definition ; Exactly one of the n1 and n2 is NULL , trees not isomorphic ; There are two possible cases for n1 and n2 to be isomorphic Case 1 : The subtrees rooted at these nodes have NOT been " Flipped " . Both of these subtrees have to be isomorphic , hence the && Case 2 : The subtrees rooted at these nodes have been " Flipped " ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver program to test above functions ; Let us create trees shown in above diagram
#include <iostream> NEW_LINE using namespace std ; struct node { int data ; struct node * left ; struct node * right ; } ; bool isIsomorphic ( node * n1 , node * n2 ) { if ( n1 == NULL && n2 == NULL ) return true ; if ( n1 == NULL n2 == NULL ) return false ; if ( n1 -> data != n2 -> data ) return false ; return ( isIsomorphic ( n1 -> left , n2 -> left ) && isIsomorphic ( n1 -> right , n2 -> right ) ) || ( isIsomorphic ( n1 -> left , n2 -> right ) && isIsomorphic ( n1 -> right , n2 -> left ) ) ; } node * newNode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return ( temp ) ; } int main ( ) { struct node * n1 = newNode ( 1 ) ; n1 -> left = newNode ( 2 ) ; n1 -> right = newNode ( 3 ) ; n1 -> left -> left = newNode ( 4 ) ; n1 -> left -> right = newNode ( 5 ) ; n1 -> right -> left = newNode ( 6 ) ; n1 -> left -> right -> left = newNode ( 7 ) ; n1 -> left -> right -> right = newNode ( 8 ) ; struct node * n2 = newNode ( 1 ) ; n2 -> left = newNode ( 3 ) ; n2 -> right = newNode ( 2 ) ; n2 -> right -> left = newNode ( 4 ) ; n2 -> right -> right = newNode ( 5 ) ; n2 -> left -> right = newNode ( 6 ) ; n2 -> right -> right -> left = newNode ( 8 ) ; n2 -> right -> right -> right = newNode ( 7 ) ; if ( isIsomorphic ( n1 , n2 ) == true ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find smallest number with given number of digits and sum of digits under given constraints | C ++ implementation of the approach ; Function to find the number having sum of digits as s and d number of digits such that the difference between the maximum and the minimum digit the minimum possible ; To store the final number ; To store the value that is evenly distributed among all the digits ; To store the remaining sum that still remains to be distributed among d digits ; rem stores the value that still remains to be distributed To keep the difference of digits minimum last rem digits are incremented by 1 ; In the last rem digits one is added to the value obtained by equal distribution ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findNumber ( int s , int d ) { string num = " " ; int val = s / d ; int rem = s % d ; int i ; for ( i = 1 ; i <= d - rem ; i ++ ) { num = num + to_string ( val ) ; } if ( rem ) { val ++ ; for ( i = d - rem + 1 ; i <= d ; i ++ ) { num = num + to_string ( val ) ; } } return num ; } int main ( ) { int s = 25 , d = 4 ; cout << findNumber ( s , d ) ; return 0 ; }
Find Kth smallest value for b such that a + b = a | b | C ++ program to find k 'th smallest value for b such that a + b = a | b ; Function to find the kth smallest value for b ; res will store final answer ; skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position will be 0. ; j 'th bit is set ; i 'th bit is set ; proceed to next bit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll kthSmallest ( ll a , ll k ) { ll res = 0 ; ll j = 0 ; for ( ll i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) ) j ++ ; if ( k & ( 1 << i ) ) res |= ( 1LL << j ) ; j ++ ; } return res ; } int main ( ) { ll a = 10 , k = 3 ; cout << kthSmallest ( a , k ) << " STRNEWLINE " ; return 0 ; }