codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the minimum difference between any element pair by selecting K elements from given Array | C ++ implementation for the above approach ; To check if selection of K elements is possible such that difference between them is greater than dif ; Selecting first element in the sorted array ; prev is the previously selected element initially at index 0 as first element is already selected ; Check if selection of K - 1 elements from array with a minimum difference of dif is possible ; If the current element is atleast dif difference apart from the previously selected element then select the current element and increase the count ; If selection of K elements with a min difference of dif is possible then return true ; Prev will become current element for the next iteration ; If selection of K elements with minimum difference of dif is not possible then return false ; Minimum largest difference possible is 1 ; Check if selection of K elements is possible with a minimum difference of dif ; If dif is greater than previous ans we update ans ; Continue to search for better answer . Try finding greater dif ; K elements cannot be selected ; Driver code ; arr should be in a sorted order
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossibleToSelect ( int arr [ ] , int N , int dif , int K ) { int count = 1 ; int prev = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] >= ( prev + dif ) ) { count ++ ; if ( count == K ) return true ; prev = arr [ i ] ; } } return false ; } int binarySearch ( int arr [ ] , int left , int right , int K , int N ) { int ans = 1 ; while ( left <= right ) { int dif = left + ( right - left ) / 2 ; if ( isPossibleToSelect ( arr , N , dif , K ) ) { ans = max ( ans , dif ) ; left = dif + 1 ; } else right = dif - 1 ; } return ans ; } int main ( ) { int N , K ; N = 7 , K = 4 ; int arr [ ] = { 1 , 4 , 9 , 0 , 2 , 13 , 3 } ; sort ( arr , arr + N ) ; cout << binarySearch ( arr , 0 , arr [ N - 1 ] , K , N ) << " STRNEWLINE " ; return 0 ; }
Find maximum height to cut all chocolates horizontally such that at least K amount remains | C ++ program for the above approach ; Function to find the sum of remaining chocolate after making the horizontal cut at height mid ; Stores the sum of chocolates ; Traverse the array arr [ ] ; If the height is at least mid ; Return the possible sum ; Function to find the maximum horizontal cut made to all the chocolates such that the sum of the remaining element is at least K ; Ranges of Binary Search ; Perform the Binary Search ; Find the sum of removed after making cut at height mid ; If the chocolate removed is same as the chocolate needed then return the height ; If the chocolate removed is less than chocolate needed then shift to the left range ; Otherwise , shift to the right range ; Return the possible cut ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cal ( vector < int > arr , int mid ) { int chocolate = 0 ; for ( auto i : arr ) { if ( i >= mid ) chocolate += i - mid ; } return chocolate ; } int maximumCut ( vector < int > arr , int K ) { int low = 0 ; int high = * max_element ( arr . begin ( ) , arr . end ( ) ) ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int chocolate = cal ( arr , mid ) ; if ( chocolate == K ) return mid ; else if ( chocolate < K ) high = mid - 1 ; else { low = mid + 1 ; if ( mid > high ) high = mid ; } } return high ; } int main ( ) { int N = 4 ; int K = 7 ; vector < int > arr { 15 , 20 , 8 , 17 } ; cout << ( maximumCut ( arr , K ) ) ; }
Find MEX of every subtree in given Tree | C ++ program for the above approach ; Stores the edges of the tree ; Function to add edges ; Function to merge two sorted vectors ; To store the result ; Iterating both vectors ; Pushing remaining elements of vector a ; Pushing remaining elements of vector b ; Function to perform the DFS Traversal that returns the subtree of node in sorted manner ; Iterate the childrens ; All values of subtree i in sorted manner ; Binary search to find MEX ; Find the mid ; Update the ranges ; Update the MEX for the current tree node ; Function to find MEX of each subtree of tree ; Function Call ; Printe the ans for each nodes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > edges ; void add_edge ( int x , int y ) { edges . push_back ( { x , y } ) ; } vector < int > merge ( vector < int > & a , vector < int > & b ) { vector < int > res ; int i = 0 , j = 0 ; int n = a . size ( ) , m = b . size ( ) ; while ( i < n && j < m ) { if ( a [ i ] < b [ j ] ) res . push_back ( a [ i ++ ] ) ; else if ( b [ j ] < a [ i ] ) res . push_back ( b [ j ++ ] ) ; } while ( i < n ) res . push_back ( a [ i ++ ] ) ; while ( j < m ) res . push_back ( b [ j ++ ] ) ; return res ; } vector < int > help ( vector < int > tree [ ] , int x , int p , vector < int > & c , vector < int > & sol ) { vector < int > res ; res . push_back ( c [ x ] ) ; for ( auto i : tree [ x ] ) { if ( i != p ) { vector < int > tmp = help ( tree , i , x , c , sol ) ; res = merge ( res , tmp ) ; } } int l = 0 , r = res . size ( ) - 1 ; int ans = res . size ( ) ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( res [ mid ] > mid ) r = mid - 1 ; else { ans = mid + 1 ; l = mid + 1 ; } } if ( res [ 0 ] != 0 ) ans = 0 ; sol [ x ] = ans ; return res ; } void solve ( int A , vector < int > C ) { int n = A ; vector < int > tree [ n + 1 ] ; for ( auto i : edges ) { tree [ i [ 0 ] ] . push_back ( i [ 1 ] ) ; tree [ i [ 1 ] ] . push_back ( i [ 0 ] ) ; } vector < int > sol ( n , 0 ) ; help ( tree , 0 , -1 , C , sol ) ; for ( auto i : sol ) cout << i << " ▁ " ; } int main ( ) { int N = 6 ; add_edge ( 0 , 1 ) ; add_edge ( 1 , 2 ) ; add_edge ( 0 , 3 ) ; add_edge ( 3 , 4 ) ; add_edge ( 3 , 5 ) ; vector < int > val = { 4 , 3 , 5 , 1 , 0 , 2 } ; solve ( N , val ) ; return 0 ; }
Koko Eating Bananas | C ++ implementation for the above approach ; to get the ceil value ; in case of odd number ; in case of even number ; check if time is less than or equals to given hour ; as minimum speed of eating must be 1 ; Maximum speed of eating is the maximum bananas in given piles ; Check if the mid ( hours ) is valid ; If valid continue to search lower speed ; If cant finish bananas in given hours , then increase the speed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( vector < int > & bananas , int mid_val , int H ) { int time = 0 ; for ( int i = 0 ; i < bananas . size ( ) ; i ++ ) { if ( bananas [ i ] % mid_val != 0 ) { time += ( ( bananas [ i ] / mid_val ) + 1 ) ; } else { time += ( bananas [ i ] / mid_val ) ; } } if ( time <= H ) { return true ; } else { return false ; } } int minEatingSpeed ( vector < int > & piles , int H ) { int start = 1 ; int end = * max_element ( piles . begin ( ) , piles . end ( ) ) ; while ( start < end ) { int mid = start + ( end - start ) / 2 ; if ( ( check ( piles , mid , H ) ) == true ) { end = mid ; } else { start = mid + 1 ; } } return end ; } int main ( ) { vector < int > piles = { 30 , 11 , 23 , 4 , 20 } ; int H = 6 ; cout << minEatingSpeed ( piles , H ) ; return 0 ; }
Minimize the sum of pair which upon removing divides the Array into 3 subarrays | C ++ implementation of the above approach ; Function to find minimum possible sum of pair which breaks the array into 3 non - empty subarrays ; prefixMin [ i ] contains minimum element till i ; Driver Code ; Given array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSumPair ( int arr [ ] , int N ) { if ( N < 5 ) { return -1 ; } int prefixMin [ N ] ; prefixMin [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N - 1 ; i ++ ) { prefixMin [ i ] = min ( arr [ i ] , prefixMin [ i - 1 ] ) ; } int ans = INT_MAX ; for ( int i = 3 ; i < N - 1 ; i ++ ) { ans = min ( ans , arr [ i ] + prefixMin [ i - 2 ] ) ; } return ans ; } int main ( ) { int arr [ ] = { 5 , 2 , 4 , 6 , 3 , 7 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << minSumPair ( arr , N ) << endl ; return 0 ; }
Minimum time remaining for safety alarm to start | C ++ program for the above approach ; Function to check if the value of mid as the minimum number of hours satisfies the condition ; Stores the sum of speed ; Iterate over the range [ 0 , N ] ; Find the value of speed ; If the bike is considered to be fast add it in sum ; Return the resultant sum ; Function to find the minimum number of time required ; Stores the range of Binary Search ; Stores the minimum number of time required ; Find the value of mid ; If the mid is the resultant speed required ; Update the ans and high ; Otherwise ; Return the minimum number of hours ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long check ( long H [ ] , long A [ ] , long mid , long N , long M , long L ) { long sum = 0 ; for ( long i = 0 ; i < N ; i ++ ) { long speed = mid * A [ i ] + H [ i ] ; if ( speed >= L ) { sum += speed ; } } return sum ; } long buzzTime ( long N , long M , long L , long H [ ] , long A [ ] ) { long low = 0 , high = 1e10 ; long ans = 0 ; while ( high >= low ) { long mid = low + ( high - low ) / 2 ; if ( check ( H , A , mid , N , M , L ) >= M ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } int main ( ) { long M = 400 , L = 120 ; long H [ ] = { 20 , 50 , 20 } ; long A [ ] = { 20 , 70 , 90 } ; long N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << buzzTime ( N , M , L , H , A ) ; return 0 ; }
Count of different numbers divisible by 3 that can be obtained by changing at most one digit | C ++ program fo the above approach ; Function to count the number of possible numbers divisible by 3 ; Calculate the sum ; Store the answer ; Iterate over the range ; Decreasing the sum ; Iterate over the range ; Checking if the new sum is divisible by 3 or not ; If yes increment the value of the count ; Driver Code ; Given number
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findCount ( string number ) { int sum = 0 ; for ( int i = 0 ; i < number . length ( ) ; ++ i ) { sum += number [ i ] - 48 ; } int count = 0 ; for ( int i = 0 ; i < number . length ( ) ; ++ i ) { int remaining_sum = sum - ( number [ i ] - 48 ) ; for ( int j = 0 ; j <= 9 ; ++ j ) { if ( ( remaining_sum + j ) % 3 == 0 && j != number [ i ] - 48 ) { ++ count ; } } } cout << count ; } int main ( ) { string number = "235" ; findCount ( number ) ; }
Maximum number of teams of size K possible with each player from different country | C ++ program for the above approach ; Function to find if T number of teams can be formed or not ; Store the sum of array elements ; Traverse the array teams [ ] ; Required Condition ; Function to find the maximum number of teams possible ; Lower and Upper part of the range ; Perform the Binary Search ; Find the value of mid ; Perform the Binary Search ; Otherwise , update the search range ; Otherwise , update the search range ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool is_possible ( vector < int > & teams , int T , int k ) { int sum = 0 ; for ( int i = 0 ; i < teams . size ( ) ; i ++ ) { sum += min ( T , teams [ i ] ) ; } return ( sum >= ( T * k ) ) ; } int countOfTeams ( vector < int > & teams_list , int N , int K ) { int lb = 0 , ub = 1e9 ; while ( lb <= ub ) { int mid = lb + ( ub - lb ) / 2 ; if ( is_possible ( teams_list , mid , K ) ) { if ( ! is_possible ( teams_list , mid + 1 , K ) ) { return mid ; } else { lb = mid + 1 ; } } else { ub = mid - 1 ; } } return 0 ; } int main ( ) { vector < int > arr = { 2 , 3 , 4 } ; int K = 2 ; int N = arr . size ( ) ; cout << countOfTeams ( arr , N , K ) ; return 0 ; }
Minimum number of decrements by 1 required to reduce all elements of a circular array to 0 | C ++ program for the above approach ; Function to find minimum operation require to make all elements 0 ; Stores the maximum element and its position in the array ; Traverse the array ; Update the maximum element and its index ; Print the minimum number of operations required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumOperations ( int arr [ ] , int N ) { int mx = 0 , pos = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= mx ) { mx = arr [ i ] ; pos = i ; } } cout << ( mx - 1 ) * N + pos + 1 ; } int main ( ) { int arr [ ] = { 2 , 0 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumOperations ( arr , N ) ; return 0 ; }
Maximize the smallest array element by incrementing all elements in a K | C ++ program for above approach ; Function to check if the smallest value of v is achievable or not ; Create array to store previous moves ; Remove previous moves ; Add balance to ans ; Update contiguous subarray of length k ; Number of moves should not exceed m ; Function to find the maximum value of the smallest array element that can be obtained ; Perform Binary search ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll n , m , k , l , r , i ; ll check ( ll v , vector < ll > & a ) { ll tec = 0 , ans = 0 ; vector < ll > b ( n + k + 1 ) ; for ( i = 0 ; i < n ; i ++ ) { tec -= b [ i ] ; if ( a [ i ] + tec < v ) { ll mov = v - a [ i ] - tec ; ans = ans + mov ; tec += mov ; b [ i + k ] = mov ; } } return ( ans <= m ) ; } ll FindLargest ( vector < ll > a ) { l = 1 ; r = pow ( 10 , 10 ) ; while ( r - l > 0 ) { ll tm = ( l + r + 1 ) / 2 ; if ( check ( tm , a ) ) l = tm ; else r = tm - 1 ; } return l ; } int main ( ) { vector < ll > a { 2 , 2 , 2 , 2 , 1 , 1 } ; m = 2 ; k = 3 ; n = a . size ( ) ; cout << FindLargest ( a ) ; return 0 ; }
Altitude of largest Triangle that can be inscribed in a Rectangle | C ++ program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Variables to perform binary search ; Stores the maximum altitude possible ; Iterate until low is less than high ; Stores the mid value ; If mide is less than or equal to the B / 2 ; Update res ; Update low ; Update high ; Print the result ; Driver Code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestAltitude ( int L , int B ) { if ( L > B ) { swap ( B , L ) ; } int low = 0 , high = L ; int res = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( mid <= ( B / 2 ) ) { res = mid ; low = mid + 1 ; } else high = mid - 1 ; } return res ; } int main ( ) { int L = 3 ; int B = 4 ; cout << largestAltitude ( L , B ) ; return 0 ; }
Find the frequency of each element in a sorted array | C ++ program for the above approach ; Function to print the frequency of each element of the sorted array ; Stores the frequency of an element ; Traverse the array arr [ ] ; If the current element is equal to the previous element ; Increment the freq by 1 ; Otherwise , ; Update freq ; Print the frequency of the last element ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printFreq ( vector < int > & arr , int N ) { int freq = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { freq ++ ; } else { cout << " Frequency ▁ of ▁ " << arr [ i - 1 ] << " ▁ is : ▁ " << freq << endl ; freq = 1 ; } } cout << " Frequency ▁ of ▁ " << arr [ N - 1 ] << " ▁ is : ▁ " << freq << endl ; } int main ( ) { vector < int > arr = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int N = arr . size ( ) ; printFreq ( arr , N ) ; return 0 ; }
Count pairs from a given array whose sum lies from a given range | C ++ program for the above approach ; Function to count pairs whose sum lies over the range [ L , R ] ; Sort the given array ; Iterate until right > 0 ; Starting index of element whose sum with arr [ right ] >= L ; Ending index of element whose sum with arr [ right ] <= R ; Update the value of end ; Add the count of elements to the variable count ; Return the value of count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairSum ( int arr [ ] , int L , int R , int N ) { sort ( arr , arr + N ) ; int right = N - 1 , count = 0 ; while ( right > 0 ) { auto it1 = lower_bound ( arr , arr + N , L - arr [ right ] ) ; int start = it1 - arr ; auto it2 = upper_bound ( arr , arr + N , R - arr [ right ] ) ; -- it2 ; int end = it2 - arr ; end = min ( end , right - 1 ) ; if ( end - start >= 0 ) { count += ( end - start + 1 ) ; } right -- ; } return count ; } int main ( ) { int arr [ ] = { 5 , 1 , 2 , 4 , 3 } ; int L = 5 , R = 8 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairSum ( arr , L , R , N ) ; return 0 ; }
Maximize median of a KxK sub | C ++ program for the above approach ; Function to determine if a given value can be median ; Stores the prefix sum array ; Traverse the matrix arr [ ] [ ] ; Update Pre [ i + 1 ] [ j + 1 ] ; If arr [ i ] [ j ] is less than or equal to mid ; Stores the count of elements should be less than mid ; Stores if the median mid can be possible or not ; Iterate over the range [ K , N ] ; Iterate over the range [ K , N ] ; Stores count of elements less than or equal to the value mid in submatrix with bottom right vertices at ( i , j ) ; If X is less than or equal to required ; Return flag ; Function to find the maximum median of a subsquare of the given size ; Stores the range of the search space ; Iterate until low is less than high ; Stores the mid value of the range [ low , high ] ; If the current median can be possible ; Update the value of low ; Update the value of high ; Return value stored in low as answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isMaximumMedian ( vector < vector < int > > & arr , int N , int K , int mid ) { vector < vector < int > > Pre ( N + 5 , vector < int > ( N + 5 , 0 ) ) ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { Pre [ i + 1 ] [ j + 1 ] = Pre [ i + 1 ] [ j ] + Pre [ i ] [ j + 1 ] - Pre [ i ] [ j ] ; if ( arr [ i ] [ j ] <= mid ) Pre [ i + 1 ] [ j + 1 ] ++ ; } } int required = ( K * K + 1 ) / 2 ; bool flag = 0 ; for ( int i = K ; i <= N ; ++ i ) { for ( int j = K ; j <= N ; ++ j ) { int X = Pre [ i ] [ j ] - Pre [ i - K ] [ j ] - Pre [ i ] [ j - K ] + Pre [ i - K ] [ j - K ] ; if ( X < required ) flag = 1 ; } } return flag ; } int maximumMedian ( vector < vector < int > > & arr , int N , int K ) { int low = 0 , high = 1e9 ; while ( low < high ) { int mid = low + ( high - low ) / 2 ; if ( isMaximumMedian ( arr , N , K , mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } int main ( ) { vector < vector < int > > arr = { { 1 , 5 , 12 } , { 6 , 7 , 11 } , { 8 , 9 , 10 } } ; int N = arr . size ( ) ; int K = 2 ; cout << maximumMedian ( arr , N , K ) ; return 0 ; }
Minimum days to make Array elements with value at least K sum at least X | C ++ program for the above approach ; Function to find the minimum number of days such that the sum of array elements >= K is at least X ; Initialize the boundaries of search space ; Perform the binary search ; Find the value of mid ; Traverse the array , arr [ ] ; Find the value of arr [ i ] after mid number of days ; Check if temp is not less than K ; Update the value of sum ; Check if the value of sum is greater than X ; Update value of high ; Update the value of low ; Print the minimum number of days ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinDays ( int arr [ ] , int R [ ] , int N , int X , int K ) { int low = 0 , high = X ; int minDays ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int temp = arr [ i ] + R [ i ] * mid ; if ( temp >= K ) { sum += temp ; } } if ( sum >= X ) { minDays = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } cout << minDays ; } int main ( ) { int X = 100 , K = 45 ; int arr [ ] = { 2 , 5 , 2 , 6 } ; int R [ ] = { 10 , 13 , 15 , 12 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMinDays ( arr , R , N , X , K ) ; return 0 ; }
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | C ++ program for the above approach ; Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Generate all possible pairs from the array arr [ ] ; If the given condition satisfy then increment the value of count ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPairsCount ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( a [ i ] * a [ j ] ) == abs ( i - j ) ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getPairsCount ( arr , N ) ; return 0 ; }
Lexicographically smallest string formed repeatedly deleting character from substring 10 | C ++ program for the above approach ; Function to find smallest lexicogra - phically smallest string ; Stores the index of last occuring 0 ; Stores the lexicographically smallest string ; Traverse the string S ; If str [ i ] is 0 ; Assign i to lastZe ; Traverse the string str ; If i is less than or equal to lastZe and str [ i ] is 0 ; If i is greater than lastZe ; Return ans ; Driver Code ; Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string lexicographicallySmallestString ( string S , int N ) { int LastZe = -1 ; string ans ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == '0' ) { LastZe = i ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( i <= LastZe && S [ i ] == '0' ) ans += S [ i ] ; else if ( i > LastZe ) ans += S [ i ] ; } return ans ; } int main ( ) { string S = "11001101" ; int N = S . size ( ) ; cout << lexicographicallySmallestString ( S , N ) ; return 0 ; }
Count of Nodes at distance K from S in its subtree for Q queries | C ++ program for the above approach ; Function to add edges ; Function to perform Depth First Search ; Stores the entry time of a node ; Stores the entering time of a node at depth d ; Iterate over the children of node ; Stores the Exit time of a node ; Function to find number of nodes at distance K from node S in the subtree of S ; Distance from root node ; Index of node with greater tin value then tin [ S ] ; Index of node with greater tout value then tout [ S ] ; Answer to the Query ; Function for performing DFS and answer to queries ; DFS function call ; Traverse the array Q [ ] ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int tin [ 100 ] , tout [ 100 ] , depth [ 100 ] ; int t = 0 ; void Add_edge ( int parent , int child , vector < vector < int > > & adj ) { adj [ parent ] . push_back ( child ) ; adj [ child ] . push_back ( parent ) ; } void dfs ( int node , int parent , vector < vector < int > > & adj , vector < vector < int > > & levels , int d ) { tin [ node ] = t ++ ; levels [ d ] . push_back ( tin [ node ] ) ; depth [ node ] = d ; for ( auto x : adj [ node ] ) { if ( x != parent ) dfs ( x , node , adj , levels , d + 1 ) ; } tout [ node ] = t ++ ; } void numberOfNodes ( int node , int dist , vector < vector < int > > & levels ) { dist += depth [ node ] ; int start = lower_bound ( levels [ dist ] . begin ( ) , levels [ dist ] . end ( ) , tin [ node ] ) - levels [ dist ] . begin ( ) ; int ed = lower_bound ( levels [ dist ] . begin ( ) , levels [ dist ] . end ( ) , tout [ node ] ) - levels [ dist ] . begin ( ) ; cout << ed - start << endl ; } void numberOfNodesUtil ( pair < int , int > Q [ ] , int M , int N ) { vector < vector < int > > adj ( N + 5 ) , levels ( N + 5 ) ; Add_edge ( 1 , 2 , adj ) ; Add_edge ( 1 , 3 , adj ) ; Add_edge ( 2 , 4 , adj ) ; Add_edge ( 2 , 5 , adj ) ; Add_edge ( 2 , 6 , adj ) ; t = 1 ; dfs ( 1 , 1 , adj , levels , 0 ) ; for ( int i = 0 ; i < M ; ++ i ) { numberOfNodes ( Q [ i ] . first , Q [ i ] . second , levels ) ; } } int main ( ) { int N = 6 ; pair < int , int > Q [ ] = { { 2 , 1 } , { 1 , 1 } } ; int M = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; numberOfNodesUtil ( Q , M , N ) ; }