codeparrot/xlcost-text-to-code huggingface dataset:
Klee 's Algorithm (Length Of Union Of Segments of a line) | C ++ program to implement Klee 's algorithm ; Returns sum of lengths covered by union of given segments ; Initialize empty points container ; Create a vector to store starting and ending points ; Sorting all points by point value ; Initialize result ; To keep track of counts of current open segments ( Starting point is processed , but ending point is not ) ; Traverse through all points ; If there are open points , then we add the difference between previous and current point . ; If this is an ending point , reduce , count of open points . ; Driver program for the above code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int segmentUnionLength ( const vector < pair < int , int > > & seg ) { int n = seg . size ( ) ; vector < pair < int , bool > > points ( n * 2 ) ; for ( int i = 0 ; i < n ; i ++ ) { points [ i * 2 ] = make_pair ( seg [ i ] . first , false ) ; points [ i * 2 + 1 ] = make_pair ( seg [ i ] . second , true ) ; } sort ( points . begin ( ) , points . end ( ) ) ; int result = 0 ; int Counter = 0 ; for ( unsigned i = 0 ; i < n * 2 ; i ++ ) { if ( Counter ) result += ( points [ i ] . first - points [ i - 1 ] . first ) ; ( points [ i ] . second ) ? Counter -- : Counter ++ ; } return result ; } int main ( ) { vector < pair < int , int > > segments ; segments . push_back ( make_pair ( 2 , 5 ) ) ; segments . push_back ( make_pair ( 4 , 8 ) ) ; segments . push_back ( make_pair ( 9 , 12 ) ) ; cout << segmentUnionLength ( segments ) << endl ; return 0 ; }
Find all duplicate and missing numbers in given permutation array of 1 to N | C ++ program for the above approach ; Function to find the duplicate and the missing elements over the range [ 1 , N ] ; Stores the missing and duplicate numbers in the array arr [ ] ; Making an iterator for set ; Traverse the given array arr [ ] ; Check if the current element is not same as the element at index arr [ i ] - 1 , then swap ; Otherwise , increment the index ; Traverse the array again ; If the element is not at its correct position ; Stores the missing and the duplicate elements ; Print the Missing Number ; Print the Duplicate Number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findElements ( int arr [ ] , int N ) { int i = 0 ; vector < int > missing ; set < int > duplicate ; set < int > :: iterator it ; while ( i != N ) { cout << i << " ▁ # ▁ " ; if ( arr [ i ] != arr [ arr [ i ] - 1 ] ) { swap ( arr [ i ] , arr [ arr [ i ] - 1 ] ) ; } else { i ++ ; } } for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != i + 1 ) { missing . push_back ( i + 1 ) ; duplicate . insert ( arr [ i ] ) ; } } cout << " Missing ▁ Numbers : ▁ " ; for ( auto & it : missing ) cout << it << ' ▁ ' ; cout << " Duplicate Numbers : " for ( auto & it : duplicate ) cout << it << ' ▁ ' ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 2 , 4 , 5 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findElements ( arr , N ) ; return 0 ; }
Check if elements of given array can be rearranged such that ( arr [ i ] + i * K ) % N = i for all values of i in range [ 0 , N | C ++ program for the above approach ; Function to check if it is possible to generate all numbers in range [ 0 , N - 1 ] using the sum of elements in the multiset A and B mod N ; If no more pair of elements can be selected ; If the number of elements in C = N , then return true ; Otherwise return false ; Stores the value of final answer ; Iterate through all the pairs in the given multiset A and B ; Stores the set A without x ; Stores the set B without y ; Stores the set C with ( x + y ) % N ; Recursive call ; Return Answer ; Function to check if it is possible to rearrange array elements such that ( arr [ i ] + i * K ) % N = i ; Stores the values of arr [ ] modulo N ; Stores all the values of i * K modulo N ; Print Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( multiset < int > A , multiset < int > B , set < int > C , int N ) { if ( A . size ( ) == 0 || B . size ( ) == 0 ) { if ( C . size ( ) == N ) { return true ; } else { return false ; } } bool ans = false ; for ( auto x : A ) { for ( auto y : B ) { multiset < int > _A = A ; _A . erase ( _A . find ( x ) ) ; multiset < int > _B = B ; _B . erase ( _B . find ( y ) ) ; set < int > _C = C ; _C . insert ( ( x + y ) % N ) ; ans = ( ans || isPossible ( _A , _B , _C , N ) ) ; } } return ans ; } void rearrangeArray ( int arr [ ] , int N , int K ) { multiset < int > A ; for ( int i = 0 ; i < N ; i ++ ) { A . insert ( arr [ i ] % N ) ; } multiset < int > B ; for ( int i = 0 ; i < N ; i ++ ) { B . insert ( ( i * K ) % N ) ; } set < int > C ; if ( isPossible ( A , B , C , N ) ) { cout << " YES " ; } else { cout << " NO " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 0 } ; int K = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rearrangeArray ( arr , N , K ) ; return 0 ; }
Height of Factor Tree for a given number | C ++ program for the above approach ; Function to find the height of the Factor Tree of the integer N ; Stores the height of Factor Tree ; Loop to iterate over values of N ; Stores if there exist a factor of N or not ; Loop to find the smallest factor of N ; If i is a factor of N ; Increment the height ; If there are no factors of N i . e , N is prime , break loop ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorTree ( int N ) { int height = 0 ; while ( N > 1 ) { bool flag = false ; for ( int i = 2 ; i <= sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { N = N / i ; flag = true ; break ; } } height ++ ; if ( ! flag ) { break ; } } return height ; } int main ( ) { int N = 48 ; cout << factorTree ( N ) ; return 0 ; }
Maximize the largest number K such that bitwise and of K till N is 0 | C ++ program for above approach ; Function to find maximum value of k which makes bitwise AND zero . ; Finding the power less than N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxK ( int N ) { int p = log2 ( N ) ; return pow ( 2 , p ) ; } int main ( ) { int N = 5 ; cout << findMaxK ( N ) - 1 << endl ; return 0 ; }
Count of Ks in the Array for a given range of indices after array updates for Q queries | C ++ program for the above approach ; Function to build the segment tree ; Base case ; Since the count of zero is required set leaf node as 1 ; If the value in array is not zero , store 0 in the leaf node ; Find the mid ; Recursive call for left subtree ; Recursive call for right subtree ; Parent nodes contains the count of zero in range tl to tr ; Function to find the count of 0 s in range l to r ; Base Case ; Case when no two segment are combining ; Finding the mid ; When it is required to combine left subtree and right subtree to get the range l to r ; Function that updates the segment tree nodes ; Base Case ; If array element is 0 ; If array element is not 0 ; Otherwise ; Find the mid ; Update the tree or count which is stored in parent node ; Function to solve all the queries ; When query type is 1 ; When query type is 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void build_tree ( vector < int > & a , vector < int > & seg_tree , int v , int tl , int tr ) { if ( tl == tr ) { if ( a [ tl ] == 0 ) seg_tree [ v ] = 1 ; else seg_tree [ v ] = 0 ; } else { int tm = ( tl + tr ) / 2 ; build_tree ( a , seg_tree , v * 2 , tl , tm ) ; build_tree ( a , seg_tree , v * 2 + 1 , tm + 1 , tr ) ; seg_tree [ v ] = seg_tree [ v * 2 ] + seg_tree [ v * 2 + 1 ] ; } } int frequency_zero ( int v , int tl , int tr , int l , int r , vector < int > & seg_tree ) { if ( l > r ) return 0 ; if ( l == tl && r == tr ) { return seg_tree [ v ] ; } int tm = ( tl + tr ) / 2 ; return frequency_zero ( v * 2 , tl , tm , l , min ( r , tm ) , seg_tree ) + frequency_zero ( v * 2 + 1 , tm + 1 , tr , max ( l , tm + 1 ) , r , seg_tree ) ; } void update ( int v , int tl , int tr , int pos , int new_val , vector < int > & seg_tree ) { if ( tl == tr ) { if ( new_val == 0 ) seg_tree [ v ] = 1 ; else seg_tree [ v ] = 0 ; } else { int tm = ( tl + tr ) / 2 ; if ( pos <= tm ) update ( v * 2 , tl , tm , pos , new_val , seg_tree ) ; else update ( v * 2 + 1 , tm + 1 , tr , pos , new_val , seg_tree ) ; seg_tree [ v ] = seg_tree [ v * 2 ] + seg_tree [ v * 2 + 1 ] ; } } void solve ( int n , int q , vector < int > & arr , vector < vector < int > > & query ) { vector < int > seg_tree ( 4 * n + 1 , 0 ) ; build_tree ( arr , seg_tree , 1 , 0 , n - 1 ) ; for ( int i = 1 ; i <= q ; i ++ ) { if ( query [ i - 1 ] [ 0 ] == 1 ) { int l = query [ i - 1 ] [ 1 ] ; int r = query [ i - 1 ] [ 2 ] ; cout << frequency_zero ( 1 , 0 , n - 1 , l , r , seg_tree ) << ' ' ; } else { arr [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] ; int pos = query [ i - 1 ] [ 1 ] ; int new_val = query [ i - 1 ] [ 2 ] ; update ( 1 , 0 , n - 1 , pos , new_val , seg_tree ) ; } } } int main ( ) { vector < int > arr = { 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 } ; int Q = 5 ; vector < vector < int > > query = { { 1 , 5 , 14 } , { 2 , 6 , 1 } , { 1 , 0 , 8 } , { 2 , 13 , 0 } , { 1 , 6 , 18 } } ; int N = arr . size ( ) ; solve ( N , Q , arr , query ) ; return 0 ; }
Find a number in minimum steps | C ++ program to find a number in minimum steps ; To represent data of a node in tree ; Prints level of node n ; Create a queue and insert root ; Do level order traversal ; Remove a node from queue ; To avoid infinite loop ; Check if dequeued number is same as n ; Insert children of dequeued node to queue ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define InF  99999 NEW_LINE struct number { int no ; int level ; public : number ( ) { } number ( int n , int l ) : no ( n ) , level ( l ) { } } ; void findnthnumber ( int n ) { queue < number > q ; struct number r ( 0 , 1 ) ; q . push ( r ) ; while ( ! q . empty ( ) ) { struct number temp = q . front ( ) ; q . pop ( ) ; if ( temp . no >= InF temp . no <= - InF ) break ; if ( temp . no == n ) { cout << " Found ▁ number ▁ n ▁ at ▁ level ▁ " << temp . level - 1 ; break ; } q . push ( number ( temp . no + temp . level , temp . level + 1 ) ) ; q . push ( number ( temp . no - temp . level , temp . level + 1 ) ) ; } } int main ( ) { findnthnumber ( 13 ) ; return 0 ; }
A Space Optimized DP solution for 0 | C ++ program of a space optimized DP solution for 0 - 1 knapsack problem . ; val [ ] is for storing maximum profit for each weight wt [ ] is for storing weights n number of item W maximum capacity of bag mat [ 2 ] [ W + 1 ] to store final result ; matrix to store final result ; iterate through all items ; one by one traverse each element ; traverse all weights j <= W ; if i is odd that mean till now we have odd number of elements so we store result in 1 th indexed row ; check for each value ; include element ; exclude element ; if i is even that mean till now we have even number of elements so we store result in 0 th indexed row ; Return mat [ 0 ] [ W ] if n is odd , else mat [ 1 ] [ W ] ; Driver program to test the cases
#include <bits/stdc++.h> NEW_LINE using namespace std ; int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int mat [ 2 ] [ W + 1 ] ; memset ( mat , 0 , sizeof ( mat ) ) ; int i = 0 ; while ( i < n ) { int j = 0 ; if ( i % 2 != 0 ) { while ( ++ j <= W ) { if ( wt [ i ] <= j ) mat [ 1 ] [ j ] = max ( val [ i ] + mat [ 0 ] [ j - wt [ i ] ] , mat [ 0 ] [ j ] ) ; else mat [ 1 ] [ j ] = mat [ 0 ] [ j ] ; } } else { while ( ++ j <= W ) { if ( wt [ i ] <= j ) mat [ 0 ] [ j ] = max ( val [ i ] + mat [ 1 ] [ j - wt [ i ] ] , mat [ 1 ] [ j ] ) ; else mat [ 0 ] [ j ] = mat [ 1 ] [ j ] ; } } i ++ ; } return ( n % 2 != 0 ) ? mat [ 0 ] [ W ] : mat [ 1 ] [ W ] ; } int main ( ) { int val [ ] = { 7 + D2 : N7 , 8 , 4 } , wt [ ] = { 3 , 8 , 6 } , W = 10 , n = 3 ; cout << KnapSack ( val , wt , n , W ) << endl ; return 0 ; }
Check if all 3 Candy bags can be emptied by removing 2 candies from any one bag and 1 from the other two repeatedly | C ++ code for the above approach ; If total candies are not multiple of 4 then its not possible to be left with 0 candies ; If minimum candies of three bags are less than number of operations required then the task is not possible ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; bool can_empty ( ll a , ll b , ll c ) { if ( ( a + b + c ) % 4 != 0 ) return false ; else { int m = min ( a , min ( b , c ) ) ; if ( m < ( ( a + b + c ) / 4 ) ) return false ; } return true ; } int main ( ) { ll a = 4 , b = 2 , c = 2 ; cout << ( can_empty ( a , b , c ) ? " true " : " false " ) << endl ; a = 3 , b = 4 , c = 2 ; cout << ( can_empty ( a , b , c ) ? " true " : " false " ) << endl ; return 0 ; }
Difference between maximum and minimum average of all K | C ++ program for the above approach ; Function to find the difference between averages of the maximum and the minimum subarrays of length k ; Stores min and max sum ; Iterate through starting points ; Sum up next K elements ; Update max and min moving sum ; Return the difference between max and min average ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double Avgdifference ( double arr [ ] , int N , int K ) { double min = 1000000 , max = -1 ; for ( int i = 0 ; i <= N - K ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < K ; j ++ ) { sum += arr [ i + j ] ; } if ( min > sum ) min = sum ; if ( max < sum ) max = sum ; } return ( max - min ) / K ; } int main ( ) { double arr [ ] = { 3 , 8 , 9 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << Avgdifference ( arr , N , K ) ; return 0 ; }
Find a number in minimum steps | C ++ program to Find a number in minimum steps ; Steps sequence ; Current sum ; Sign of the number ; Basic steps required to get sum >= required value . ; If we have reached ahead to destination . ; If the last step was an odd number , then it has following mechanism for negating a particular number and decreasing the sum to required number Also note that it may require 1 more step in order to reach the sum . ; If the current time instance is even and sum is odd than it takes 2 more steps and few negations in previous elements to reach there . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > find ( int n ) { vector < int > ans ; int sum = 0 ; int i ; int sign = ( n >= 0 ? 1 : -1 ) ; n = abs ( n ) ; for ( i = 1 ; sum < n ; i ++ ) { ans . push_back ( sign * i ) ; sum += i ; } if ( sum > sign * n ) { if ( i % 2 ) { sum -= n ; if ( sum % 2 ) { ans . push_back ( sign * i ) ; sum += i ++ ; } ans [ ( sum / 2 ) - 1 ] *= -1 ; } else { sum -= n ; if ( sum % 2 ) { sum -- ; ans . push_back ( sign * i ) ; ans . push_back ( sign * -1 * ( i + 1 ) ) ; } ans [ ( sum / 2 ) - 1 ] *= -1 ; } } return ans ; } int main ( ) { int n = 20 ; if ( n == 0 ) cout << " Minimum ▁ number ▁ of ▁ Steps : ▁ 0 STRNEWLINE Step ▁ sequence : STRNEWLINE 0" ; else { vector < int > a = find ( n ) ; cout << " Minimum ▁ number ▁ of ▁ Steps : ▁ " << a . size ( ) << " Step sequence : " for ( int i : a ) cout << i << " ▁ " ; } return 0 ; }
Maximum range length such that A [ i ] is maximum in given range for all i from [ 1 , N ] | C ++ program for the above approach ; Function to find maximum range for each i such that arr [ i ] is max in range ; Vector to store the left and right index for each i such that left [ i ] > arr [ i ] and right [ i ] > arr [ i ] ; Traverse the array ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify left [ i ] ; Clear the stack ; Traverse the array to find right [ i ] for each i ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify right [ i ] ; Print the value range for each i ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MaxRange ( vector < int > A , int n ) { vector < int > left ( n ) , right ( n ) ; stack < pair < int , int > > s ; s . push ( { INT_MAX , -1 } ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( s . top ( ) . first < A [ i ] ) s . pop ( ) ; left [ i ] = s . top ( ) . second ; s . push ( { A [ i ] , i } ) ; } while ( ! s . empty ( ) ) s . pop ( ) ; s . push ( make_pair ( INT_MAX , n ) ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( s . top ( ) . first < A [ i ] ) s . pop ( ) ; right [ i ] = s . top ( ) . second ; s . push ( { A [ i ] , i } ) ; } for ( int i = 0 ; i < n ; i ++ ) { cout << left [ i ] + 1 << ' ▁ ' << right [ i ] - 1 << " STRNEWLINE " ; } } int main ( ) { vector < int > arr { 1 , 3 , 2 } ; int n = arr . size ( ) ; MaxRange ( arr , n ) ; return 0 ; }
Find Nth smallest number having exactly 4 divisors | C ++ program for the above approach ; Function to find the nth number which has exactly 4 divisors ; The divs [ ] array to store number of divisors of every element ; The vis [ ] array to check if given number is considered or not ; The cnt stores number of elements having exactly 4 divisors ; Iterate while cnt less than n ; If i is a prime ; Iterate in the range [ 2 * i , 1000000 ] with increment of i ; If the number j is already considered ; Dividing currNum by i until currNum % i is equal to 0 ; Case a single prime in its factorization ; Case of two distinct primes which divides j exactly once each ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nthNumber ( int n ) { int divs [ 1000000 ] ; bool vis [ 1000000 ] ; int cnt = 0 ; for ( int i = 2 ; cnt < n ; i ++ ) { if ( divs [ i ] == 0 ) { for ( int j = 2 * i ; j < 1000000 ; j += i ) { if ( vis [ j ] ) { continue ; } vis [ j ] = 1 ; int currNum = j ; int count = 0 ; while ( currNum % i == 0 ) { divs [ j ] ++ ; currNum = currNum / i ; count ++ ; } if ( currNum == 1 && count == 3 && divs [ j ] == 3 ) { cnt ++ ; } else if ( currNum != 1 && divs [ currNum ] == 0 && count == 1 && divs [ j ] == 1 ) { cnt ++ ; } if ( cnt == n ) { return j ; } } } } return -1 ; } int main ( ) { int N = 24 ; cout << nthNumber ( N ) << endl ; return 0 ; }
Maximum number formed from the digits of given three numbers | C ++ program for the above approach ; Function to find the maximum number formed by taking the maximum digit at the same position from each number ; Stores the result ; Stores the position value of a digit ; Stores the digit at the unit place ; Stores the digit at the unit place ; Stores the digit at the unit place ; Update A , B and C ; Stores the maximum digit ; Increment ans cur * a ; Update cur ; Return ans ; Driver Code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findkey ( int A , int B , int C ) { int ans = 0 ; int cur = 1 ; while ( A > 0 ) { int a = A % 10 ; int b = B % 10 ; int c = C % 10 ; A = A / 10 ; B = B / 10 ; C = C / 10 ; int m = max ( a , max ( c , b ) ) ; ans += cur * m ; cur = cur * 10 ; } return ans ; } int main ( ) { int A = 3521 , B = 2452 , C = 1352 ; cout << findkey ( A , B , C ) ; return 0 ; }
Count of distinct GCDs among all the non | C ++ program for the above approach ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; calculate largest number in A and mapping A to Mp ; iterate over all possible values of GCD ; variable to check current GCD ; iterate over all multiples of i ; If j is present in A ; calculate gcd of all encountered multiples of i ; current GCD is possible ; return answer ; Driver code ; Input ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctGCDs ( int arr [ ] , int N ) { int M = -1 , ans = 0 ; map < int , int > Mp ; for ( int i = 0 ; i < N ; i ++ ) { M = max ( M , arr [ i ] ) ; Mp [ arr [ i ] ] = 1 ; } for ( int i = 1 ; i <= M ; i ++ ) { int currGcd = 0 ; for ( int j = i ; j <= M ; j += i ) { if ( Mp [ j ] ) { currGcd = __gcd ( currGcd , j ) ; if ( currGcd == i ) { ans ++ ; break ; } } } } return ans ; } int main ( ) { int arr [ ] = { 3 , 11 , 14 , 6 , 12 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << distinctGCDs ( arr , N ) << endl ; return 0 ; }
Compress a Binary Tree into an integer diagonally |  ; Function to compress the elements in an array into an integer ; Check for each bit position ; Update the count of set and non - set bits ; If number of set bits exceeds the number of non - set bits , then add set bits value to ans ; Perform Inorder Traversal on the Binary Tree ; Store all nodes of the same line together as a vector ; Increase the vertical distance of left child ; Vertical distance remains same for right child ; Function to compress a given Binary Tree into an integer ; Declare a map ; Store all the compressed values of diagonal elements in an array ; Compress the array into an integer ; Driver Code Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct TreeNode { int val ; TreeNode * left , * right ; TreeNode ( int v ) { val = v ; left = NULL ; right = NULL ; } } ; int findCompressValue ( vector < int > arr ) { int ans = 0 ; int getBit = 1 ; for ( int i = 0 ; i < 32 ; i ++ ) { int S = 0 ; int NS = 0 ; for ( int j : arr ) { if ( getBit & j ) S += 1 ; else NS += 1 ; } if ( S > NS ) ans += pow ( 2 , i ) ; getBit <<= 1 ; } return ans ; } void diagonalOrder ( TreeNode * root , int d , map < int , vector < int > > & mp ) { if ( ! root ) return ; mp [ d ] . push_back ( root -> val ) ; diagonalOrder ( root -> left , d + 1 , mp ) ; diagonalOrder ( root -> right , d , mp ) ; } int findInteger ( TreeNode * root ) { map < int , vector < int > > mp ; diagonalOrder ( root , 0 , mp ) ; vector < int > arr ; for ( auto i : mp ) arr . push_back ( findCompressValue ( i . second ) ) ; return findCompressValue ( arr ) ; } int main ( ) { TreeNode * root = new TreeNode ( 6 ) ; root -> left = new TreeNode ( 5 ) ; root -> right = new TreeNode ( 3 ) ; root -> left -> left = new TreeNode ( 3 ) ; root -> left -> right = new TreeNode ( 5 ) ; root -> right -> left = new TreeNode ( 3 ) ; root -> right -> right = new TreeNode ( 4 ) ; cout << findInteger ( root ) ; return 0 ; }
Egg Dropping Puzzle | DP |  ; A utility function to get maximum of two integers ; Function to get minimum number of trials needed in worst case with n eggs and k floors ; If there are no floors , then no trials needed . OR if there is one floor , one trial needed . ; We need k trials for one egg and k floors ; Consider all droppings from 1 st floor to kth floor and return the minimum of these values plus 1. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max ( int a , int b ) { return ( a > b ) ? a : b ; } int eggDrop ( int n , int k ) { if ( k == 1 k == 0 ) return k ; if ( n == 1 ) return k ; int min = INT_MAX , x , res ; for ( x = 1 ; x <= k ; x ++ ) { res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ; if ( res < min ) min = res ; } return min + 1 ; } int main ( ) { int n = 2 , k = 10 ; cout << " Minimum ▁ number ▁ of ▁ trials ▁ " " in ▁ worst ▁ case ▁ with ▁ " << n << " ▁ eggs ▁ and ▁ " << k << " ▁ floors ▁ is ▁ " << eggDrop ( n , k ) << endl ; 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 ; Divide the number n by 10 upto k - 1 times ; If the number n is equal 0 ; Print the right most digit ; 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 ; } while ( ( k - 1 ) > 0 && n > 0 ) { n = n / 10 ; k -- ; } if ( n == 0 ) { cout << -1 << endl ; } else { cout << n % 10 << endl ; } } int main ( ) { int n = 2354 ; int k = 2 ; kthDigitFromLast ( n , k ) ; }