codeparrot/xlcost-text-to-code huggingface dataset:
Check if all rows of a Binary Matrix have all ones placed adjacently or not | C ++ program for the above approach ; Function to check if all 1 s are placed adjacently in an array or not ; Base Case ; Stores the sum of XOR of all pair of adjacent elements ; Calculate sum of XOR of all pair of adjacent elements ; Check for corner cases ; Return true ; Function to check if all the rows have all 1 s grouped together or not ; Traverse each row ; Check if all 1 s are placed together in the ith row or not ; Function to check if all 1 s in a row are grouped together in a matrix or not ; Print the result ; Driver Code ; Given matrix ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkGroup ( vector < int > arr ) { if ( arr . size ( ) <= 2 ) return true ; int corner = arr [ 0 ] + arr [ ( int ) arr . size ( ) - 1 ] ; int xorSum = 0 ; for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) xorSum += ( arr [ i ] ^ arr [ i + 1 ] ) ; if ( ! corner ) if ( xorSum > 2 ) return false ; else if ( corner == 1 ) if ( xorSum > 1 ) return false ; else if ( xorSum > 0 ) return false ; return true ; } bool isInGroupUtil ( vector < vector < int > > mat ) { for ( auto i : mat ) { if ( ! checkGroup ( i ) ) return false ; } return true ; } void isInGroup ( vector < vector < int > > mat ) { bool ans = isInGroupUtil ( mat ) ; if ( ans ) printf ( " Yes " ) ; else printf ( " No " ) ; } int main ( ) { vector < vector < int > > mat = { { 0 , 1 , 1 , 0 } , { 1 , 1 , 0 , 0 } , { 0 , 0 , 0 , 1 } , { 1 , 1 , 1 , 0 } } ; isInGroup ( mat ) ; }
Print all numbers up to N having product of digits equal to K | C ++ program for the above approach ; Function to find the product of digits of a number ; Stores the product of digits of a number ; Return the product ; Function to print all numbers upto N having product of digits equal to K ; Stores whether any number satisfying the given conditions exists or not ; Iterate over the range [ 1 , N ] ; If product of digits of arr [ i ] is equal to K or not ; Print that number ; If no numbers are found ; Driver Code ; Given value of N & K ; Function call to print all numbers from [ 1 , N ] with product of digits K
#include <bits/stdc++.h> NEW_LINE using namespace std ; int productOfDigits ( int N ) { int product = 1 ; while ( N != 0 ) { product = product * ( N % 10 ) ; N = N / 10 ; } return product ; } void productOfDigitsK ( int N , int K ) { int flag = 0 ; for ( int i = 1 ; i <= N ; ++ i ) { if ( K == productOfDigits ( i ) ) { cout << i << " ▁ " ; flag = 1 ; } } if ( flag == 0 ) cout << " - 1" ; } int main ( ) { int N = 500 , K = 10 ; productOfDigitsK ( N , K ) ; }
Minimize difference between maximum and minimum array elements by removing a K | C ++ program for the above approach ; Function to minimize difference between maximum and minimum array elements by removing a K - length subarray ; Size of array ; Stores the maximum and minimum in the suffix subarray [ i . . N - 1 ] ; Traverse the array ; Stores the maximum and minimum in the prefix subarray [ 0 . . i - 1 ] ; Store the minimum difference ; Traverse the array ; If the suffix doesn 't exceed  the end of the array ; Store the maximum element in array after removing subarray of size K ; Stores the maximum element in array after removing subarray of size K ; Update minimum difference ; Updating the maxPrefix and minPrefix with current element ; Print the minimum difference ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimiseDifference ( vector < int > & arr , int K ) { int N = arr . size ( ) ; int maxSuffix [ N + 1 ] , minSuffix [ N + 1 ] ; maxSuffix [ N ] = -1e9 ; minSuffix [ N ] = 1e9 ; maxSuffix [ N - 1 ] = arr [ N - 1 ] ; minSuffix [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; -- i ) { maxSuffix [ i ] = max ( maxSuffix [ i + 1 ] , arr [ i ] ) ; minSuffix [ i ] = min ( minSuffix [ i + 1 ] , arr [ i ] ) ; } int maxPrefix = arr [ 0 ] ; int minPrefix = arr [ 0 ] ; int minDiff = maxSuffix [ K ] - minSuffix [ K ] ; for ( int i = 1 ; i < N ; ++ i ) { if ( i + K <= N ) { int maximum = max ( maxSuffix [ i + K ] , maxPrefix ) ; int minimum = min ( minSuffix [ i + K ] , minPrefix ) ; minDiff = min ( minDiff , maximum - minimum ) ; } maxPrefix = max ( maxPrefix , arr [ i ] ) ; minPrefix = min ( minPrefix , arr [ i ] ) ; } cout << minDiff << " STRNEWLINE " ; } int main ( ) { vector < int > arr = { 4 , 5 , 8 , 9 , 1 , 2 } ; int K = 2 ; minimiseDifference ( arr , K ) ; return 0 ; }
Minimize segments required to be removed such that at least one segment intersects with all remaining segments | C ++ program for the above approach ; Function to find the minimum number of segments required to be deleted ; Stores the start and end points ; Traverse segments and fill the startPoints and endPoints ; Sort the startPoints ; Sort the startPoints ; Store the minimum number of deletions required and initialize with ( N - 1 ) ; Traverse the array segments [ ] ; Store the starting point ; Store the ending point ; Store the number of segments satisfying the first condition of non - intersection ; Store the number of segments satisfying the second condition of non - intersection ; Update answer ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minSegments ( pair < int , int > segments [ ] , int n ) { int startPoints [ n ] , endPoints [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { startPoints [ i ] = segments [ i ] . first ; endPoints [ i ] = segments [ i ] . second ; } sort ( startPoints , startPoints + n ) ; sort ( endPoints , endPoints + n ) ; int ans = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int f = segments [ i ] . first ; int s = segments [ i ] . second ; int leftDelete = lower_bound ( endPoints , endPoints + n , f ) - endPoints ; int rightDelete = max ( 0 , n - ( int ) ( upper_bound ( startPoints , startPoints + n , s ) - startPoints ) ) ; ans = min ( ans , leftDelete + rightDelete ) ; } cout << ans ; } int main ( ) { pair < int , int > arr [ ] = { { 1 , 2 } , { 5 , 6 } , { 6 , 7 } , { 7 , 10 } , { 8 , 9 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minSegments ( arr , N ) ; return 0 ; }
Longest substring where all the characters appear at least K times | Set 3 | C ++ program for the above approach ; Function to find the length of the longest substring ; Store the required answer ; Create a frequency map of the characters of the string ; Store the length of the string ; Traverse the string , s ; Increment the frequency of the current character by 1 ; Stores count of unique characters ; Find the number of unique characters in string ; Iterate in range [ 1 , unique ] ; Initialize frequency of all characters as 0 ; Stores the start and the end of the window ; Stores the current number of unique characters and characters occuring atleast K times ; New unique character ; New character which occurs atleast k times ; Expand window by incrementing end by 1 ; Check if this character is present atleast k times ; Check if this character is unique ; Shrink the window by incrementing start by 1 ; If there are curr_unique characters and each character is atleast k times ; Update the overall maximum length ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubstring ( string s , int k ) { int ans = 0 ; int freq [ 26 ] = { 0 } ; int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) freq [ s [ i ] - ' a ' ] ++ ; int unique = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) if ( freq [ i ] != 0 ) unique ++ ; for ( int curr_unique = 1 ; curr_unique <= unique ; curr_unique ++ ) { memset ( freq , 0 , sizeof ( freq ) ) ; int start = 0 , end = 0 ; int cnt = 0 , count_k = 0 ; while ( end < n ) { if ( cnt <= curr_unique ) { int ind = s [ end ] - ' a ' ; if ( freq [ ind ] == 0 ) cnt ++ ; freq [ ind ] ++ ; if ( freq [ ind ] == k ) count_k ++ ; end ++ ; } else { int ind = s [ start ] - ' a ' ; if ( freq [ ind ] == k ) count_k -- ; freq [ ind ] -- ; if ( freq [ ind ] == 0 ) cnt -- ; start ++ ; } if ( cnt == curr_unique && count_k == curr_unique ) ans = max ( ans , end - start ) ; } } cout << ans ; } int main ( ) { string S = " aabbba " ; int K = 3 ; longestSubstring ( S , K ) ; return 0 ; }
Maximum number of intervals that an interval can intersect | C ++ program for the above approach ; Comparator function to sort in increasing order of second values in each pair ; Function to count maximum number of intervals that an interval can intersect ; Create a hashmap ; Sort by starting position ; Traverse all the intervals ; Initialize um [ v [ i ] ] as 0 ; Store the starting and ending point of the ith interval ; Set low and high ; Store the required number of intervals ; Apply binary search to find the number of intervals completely lying to the right of the ith interval ; Find the mid ; Condition for searching in the first half ; Otherwise search in the second half ; Increment um [ v [ i ] ] by n - ans if ans != - 1 ; Sort by ending position ; Traverse all the intervals ; Store the starting and ending point of the ith interval ; Set low and high ; Store the required number of intervals ; Apply binary search to find the number of intervals completely lying to the left of the ith interval ; Increment um [ v [ i ] ] by ans + 1 if ans != - 1 ; Store the answer ; Traverse the map ; Update the overall answer ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( pair < int , int > f , pair < int , int > s ) { return f . second < s . second ; } struct hash_pair { template < class T1 , class T2 > size_t operator() ( const pair < T1 , T2 > & p ) const { auto hash1 = hash < T1 > { } ( p . first ) ; auto hash2 = hash < T2 > { } ( p . second ) ; return hash1 ^ hash2 ; } } ; void findMaxIntervals ( vector < pair < int , int > > v , int n ) { unordered_map < pair < int , int > , int , hash_pair > um ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { um [ v [ i ] ] = 0 ; int start = v [ i ] . first ; int end = v [ i ] . second ; int low = i + 1 ; int high = n - 1 ; int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( v [ mid ] . first > end ) { ans = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } if ( ans != -1 ) um [ v [ i ] ] = n - ans ; } sort ( v . begin ( ) , v . end ( ) , compare ) ; for ( int i = 0 ; i < n ; i ++ ) { int start = v [ i ] . first ; int end = v [ i ] . second ; int low = 0 ; int high = i - 1 ; int ans = -1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( v [ mid ] . second < start ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } if ( ans != -1 ) um [ v [ i ] ] += ( ans + 1 ) ; } int res = 0 ; for ( auto it = um . begin ( ) ; it != um . end ( ) ; it ++ ) { res = max ( res , n - it -> second ) ; } cout << res ; } int main ( ) { vector < pair < int , int > > arr = { { 1 , 2 } , { 3 , 4 } , { 2 , 5 } } ; int N = arr . size ( ) ; findMaxIntervals ( arr , N ) ; return 0 ; }
Print matrix elements using DFS traversal | C ++ program for the above approach ; Direction vectors ; Function to check if current position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited or not ; Utility function to print matrix elements using DFS Traversal ; Mark the current cell visited ; Print the element at the cell ; Traverse all four adjacent cells of the current element ; Check if x and y is valid index or not ; Function to print the matrix elements ; Initialize a visiting matrix ; Function call to print matrix elements by DFS traversal ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dRow [ ] = { -1 , 0 , 1 , 0 } ; int dCol [ ] = { 0 , 1 , 0 , -1 } ; bool isValid ( vector < vector < bool > > & vis , int row , int col , int COL , int ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row ] [ col ] == true ) return false ; return true ; } void DFSUtil ( int row , int col , vector < vector < int > > grid , vector < vector < bool > > & vis , int M , int N ) { vis [ row ] [ col ] = true ; cout << grid [ row ] [ col ] << " ▁ " ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = row + dRow [ i ] ; int y = col + dCol [ i ] ; if ( isValid ( vis , x , y , M , N ) ) DFSUtil ( x , y , grid , vis , M , N ) ; } } void DFS ( int row , int col , vector < vector < int > > grid , int M , int N ) { vector < vector < bool > > vis ( M + 1 , vector < bool > ( N + 1 , false ) ) ; DFSUtil ( 0 , 0 , grid , vis , M , N ) ; } int main ( ) { vector < vector < int > > grid { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int M = grid . size ( ) ; int N = grid [ 0 ] . size ( ) ; DFS ( 0 , 0 , grid , M , N ) ; return 0 ; }
Print matrix elements using DFS traversal | C ++ program for the above approach ; Direction vectors ; Function to check if curruent position is valid or not ; Check if the cell is out of bounds ; Check if the cell is visited ; Function to print the matrix elements ; Stores if a position in the matrix been visited or not ; Initialize stack to implement DFS ; Push the first position of grid [ ] [ ] in the stack ; Mark the cell ( 0 , 0 ) visited ; Stores top element of stack ; Delete the top ( ) element of stack ; Print element at the cell ; Traverse in all four adjacent sides of current positions ; Check if x and y is valid position and then push the position of current cell in the stack ; Push the current cell ; Mark current cell visited ; Driver Code ; Given matrix ; Row of the matrix ; Column of the matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dRow [ ] = { -1 , 0 , 1 , 0 } ; int dCol [ ] = { 0 , 1 , 0 , -1 } ; bool isValid ( vector < vector < bool > > & vis , int row , int col , int COL , int ROW ) { if ( row < 0 col < 0 col > COL - 1 row > ROW - 1 ) return false ; if ( vis [ row ] [ col ] == true ) return false ; return true ; } void DFS_iterative ( vector < vector < int > > grid , int M , int N ) { vector < vector < bool > > vis ( M + 5 , vector < bool > ( N + 5 , false ) ) ; stack < pair < int , int > > st ; st . push ( { 0 , 0 } ) ; vis [ 0 ] [ 0 ] = true ; while ( ! st . empty ( ) ) { pair < int , int > p = st . top ( ) ; st . pop ( ) ; int row = p . first ; int col = p . second ; cout << grid [ row ] [ col ] << " ▁ " ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = row + dRow [ i ] ; int y = col + dCol [ i ] ; if ( isValid ( vis , x , y , M , N ) ) { st . push ( { x , y } ) ; vis [ x ] [ y ] = true ; } } } } int main ( ) { vector < vector < int > > grid { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int M = grid . size ( ) ; int N = grid [ 0 ] . size ( ) ; DFS_iterative ( grid , M , N ) ; return 0 ; }
Segregate 1 s and 0 s in separate halves of a Binary String | C ++ program for the above approach ; Function to count the minimum number of operations required to segregate all 1 s and 0 s in a binary string ; Stores the count of unequal pair of adjacent characters ; If an unequal pair of adjacent characters occurs ; For odd count ; For even count ; Driver Code ; Given string ; Length of the string ; Prints the minimum count of operations required
#include <bits/stdc++.h> NEW_LINE using namespace std ; #include <bits/stdc++.h> NEW_LINE using namespace std ; void minOps ( string s , int N ) { int ans = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( s [ i ] != s [ i - 1 ] ) { ans ++ ; } } if ( ans % 2 == 1 ) { cout << ( ans - 1 ) / 2 << endl ; return ; } cout << ( ans / 2 ) ; } int main ( ) { string str = "01011100" ; int N = str . size ( ) ; minOps ( str , N ) ; return 0 ; }
Count prime triplets upto N having sum of first two elements equal to the third number | C ++ program for the above approach ; Boolean array to mark prime numbers ; To count the prime triplets having the sum of the first two numbers equal to the third element ; Function to count prime triplets having sum of the first two elements equal to the third element ; Sieve of Eratosthenes ; Checks for the prime numbers having difference equal to2 ; Update count of triplets ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000001 NEW_LINE bool prime [ MAX ] ; int cntTriplet [ MAX ] ; void primeTriplet ( long N ) { memset ( prime , true , sizeof ( prime ) ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j <= N ; j += i ) { prime [ j ] = false ; } } } for ( int i = 3 ; i <= N ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) { cntTriplet [ i ] = cntTriplet [ i - 1 ] + 1 ; } else { cntTriplet [ i ] = cntTriplet [ i - 1 ] ; } } cout << cntTriplet [ N ] ; } int main ( ) { long N = 7 ; primeTriplet ( N ) ; return 0 ; }
Find index of the element differing in parity with all other array elements | C ++ program for the above approach ; Function to print the array element which differs in parity with the remaining array elements ; Multimaps to store even and odd numbers along with their indices ; Traverse the array ; If array element is even ; Otherwise ; If only one even element is present in the array ; If only one odd element is present in the array ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int OddOneOut ( int arr [ ] , int N ) { multimap < int , int > e , o ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { e . insert ( { arr [ i ] , i } ) ; } else { o . insert ( { arr [ i ] , i } ) ; } } if ( e . size ( ) == 1 ) { cout << e . begin ( ) -> second ; } else { cout << o . begin ( ) -> second ; } } int main ( ) { int arr [ ] = { 2 , 4 , 7 , 8 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; OddOneOut ( arr , N ) ; return 0 ; }
Count indices where the maximum in the prefix array is less than that in the suffix array | C ++ program for the above approach ; Function to print the count of indices in which the maximum in prefix arrays is less than that in the suffix array ; If size of array is 1 ; pre [ ] : Prefix array suf [ ] : Suffix array ; Stores the required count ; Find the maximum in prefix array ; Find the maximum in suffix array ; Traverse the array ; If maximum in prefix array is less than maximum in the suffix array ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void count ( int a [ ] , int n ) { if ( n == 1 ) { cout << 0 ; return ; } int pre [ n - 1 ] , suf [ n - 1 ] ; int max = a [ 0 ] ; int ans = 0 , i ; pre [ 0 ] = a [ 0 ] ; for ( i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; pre [ i ] = max ; } max = a [ n - 1 ] ; suf [ n - 2 ] = a [ n - 1 ] ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( a [ i ] > max ) max = a [ i ] ; suf [ i - 1 ] = max ; } for ( i = 0 ; i < n - 1 ; i ++ ) { if ( pre [ i ] < suf [ i ] ) ans ++ ; } cout << ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 8 , 1 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; count ( arr , N ) ; return 0 ; }
Length of the longest subsequence such that XOR of adjacent elements is equal to K | C ++ program for above approach ; Function to find maximum length of subsequence ; Stores maximum length of subsequence ; Dictionary to store the longest length of subsequence ending at an integer , say X ; Stores the maximum length of subsequence ending at index i ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Retrieve the longest length of subsequence ending at integer [ ] a ^ K ; If dpj is not NULL ; Update dp [ i ] ; Update ans ; Update the maximum length of subsequence ending at element is a [ i ] in Dictionary ; Return the ans if ans >= 2. Otherwise , return 0 ; Input ; Print the length of the longest subsequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorSubsequence ( int a [ ] , int n , int k ) { int ans = 0 ; map < int , int > map ; int dp [ n ] = { 0 } ; map [ a [ 0 ] ] = 1 ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int dpj ; if ( map . find ( a [ i ] ^ k ) != map . end ( ) ) { dpj = map [ a [ i ] ^ k ] ; } else { dpj = -1 ; } if ( dpj != 0 ) dp [ i ] = max ( dp [ i ] , dpj + 1 ) ; ans = max ( ans , dp [ i ] ) ; if ( map . find ( a [ i ] ) != map . end ( ) ) { map [ a [ i ] ] = max ( map [ a [ i ] ] + 1 , dp [ i ] ) ; } else { map [ a [ i ] ] = max ( 1 , dp [ i ] ) ; } } return ans >= 2 ? ans : 0 ; } int main ( ) { int arr [ ] = { 3 , 2 , 4 , 3 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 1 ; cout << ( xorSubsequence ( arr , N , K ) ) ; return 0 ; }
Sum of subtree depths for every node of a given Binary Tree | C ++ program for the above approach ; Binary tree node ; Function to allocate a new node with the given data and NULL in its left and right pointers ; DFS function to calculate the sum of depths of all subtrees depth sum ; Store total number of node in its subtree and total sum of depth in its subtree ; Check if left is not NULL ; Call recursively the DFS function for left child ; Increment the sum of depths by ptemp . first + p . temp . first ; Increment p . first by count of noded in left subtree ; Check if right is not NULL ; Call recursively the DFS function for right child ; Increment the sum of depths by ptemp . first + p . temp . first ; Increment p . first by count of nodes in right subtree ; Increment the result by total sum of depth in current subtree ; Return p ; Driver Code ; Given Tree ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; class TreeNode { public : int data ; TreeNode * left ; TreeNode * right ; } ; TreeNode * newNode ( int data ) { TreeNode * Node = new TreeNode ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } pair < int , int > sumofsubtree ( TreeNode * root , int & ans ) { pair < int , int > p = make_pair ( 1 , 0 ) ; if ( root -> left ) { pair < int , int > ptemp = sumofsubtree ( root -> left , ans ) ; p . second += ptemp . first + ptemp . second ; p . first += ptemp . first ; } if ( root -> right ) { pair < int , int > ptemp = sumofsubtree ( root -> right , ans ) ; p . second += ptemp . first + ptemp . second ; p . first += ptemp . first ; } ans += p . second ; return p ; } int main ( ) { TreeNode * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> left -> left -> left = newNode ( 8 ) ; root -> left -> left -> right = newNode ( 9 ) ; int ans = 0 ; sumofsubtree ( root , ans ) ; cout << ans ; return 0 ; }
Minimum time required to fill given N slots | C ++ program for the above approach ; Function to return the minimum time to fill all the slots ; Stores visited slots ; Checks if a slot is visited or not ; Insert all filled slots ; Iterate until queue is not empty ; Iterate through all slots in the queue ; Front index ; If previous slot is present and not visited ; If next slot is present and not visited ; Increment the time at each level ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minTime ( vector < int > arr , int N , int K ) { queue < int > q ; vector < bool > vis ( N + 1 , false ) ; int time = 0 ; for ( int i = 0 ; i < K ; i ++ ) { q . push ( arr [ i ] ) ; vis [ arr [ i ] ] = true ; } while ( q . size ( ) > 0 ) { for ( int i = 0 ; i < q . size ( ) ; i ++ ) { int curr = q . front ( ) ; q . pop ( ) ; if ( curr - 1 >= 1 && ! vis [ curr - 1 ] ) { vis [ curr - 1 ] = true ; q . push ( curr - 1 ) ; } if ( curr + 1 <= N && ! vis [ curr + 1 ] ) { vis [ curr + 1 ] = true ; q . push ( curr + 1 ) ; } } time ++ ; } cout << ( time - 1 ) ; } int main ( ) { int N = 6 ; vector < int > arr = { 2 , 6 } ; int K = arr . size ( ) ; minTime ( arr , N , K ) ; }