codeparrot/xlcost-text-to-code huggingface dataset:
Smallest element in an array that is repeated exactly ' k ' times . | C ++ program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] that is repeated k times ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; int findDuplicate ( int arr [ ] , int n , int k ) { int freq [ MAX ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 1 && arr [ i ] > MAX ) { cout << " Out ▁ of ▁ range " ; return -1 ; } freq [ arr [ i ] ] += 1 ; } for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] == k ) return i ; } return -1 ; } int main ( ) { int arr [ ] = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = sizeof ( arr ) / ( sizeof ( arr [ 0 ] ) ) ; cout << findDuplicate ( arr , n , k ) ; return 0 ; }
Find the Sub | C ++ program to find subarray with sum closest to 0 ; Sort on the basis of sum ; Returns subarray with sum closest to 0. ; To consider the case of subarray starting from beginning of the array ; Store prefix sum with index ; Sort on the basis of sum ; Find two consecutive elements with minimum difference ; Update minimum difference and starting and ending indexes ; Return starting and ending indexes ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct prefix { int sum ; int index ; } ; bool comparison ( prefix a , prefix b ) { return a . sum < b . sum ; } pair < int , int > findSubArray ( int arr [ ] , int n ) { int start , end , min_diff = INT_MAX ; prefix pre_sum [ n + 1 ] ; pre_sum [ 0 ] . sum = 0 ; pre_sum [ 0 ] . index = -1 ; for ( int i = 1 ; i <= n ; i ++ ) { pre_sum [ i ] . sum = pre_sum [ i - 1 ] . sum + arr [ i - 1 ] ; pre_sum [ i ] . index = i - 1 ; } sort ( pre_sum , pre_sum + ( n + 1 ) , comparison ) ; for ( int i = 1 ; i <= n ; i ++ ) { int diff = pre_sum [ i ] . sum - pre_sum [ i - 1 ] . sum ; if ( min_diff > diff ) { min_diff = diff ; start = pre_sum [ i - 1 ] . index ; end = pre_sum [ i ] . index ; } } pair < int , int > p = make_pair ( start + 1 , end ) ; return p ; } int main ( ) { int arr [ ] = { 2 , 3 , -4 , -1 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pair < int , int > point = findSubArray ( arr , n ) ; cout << " Subarray ▁ starting ▁ from ▁ " ; cout << point . first << " ▁ to ▁ " << point . second ; return 0 ; }
Check whether a given array is a k sorted array or not | C ++ implementation to check whether the given array is a k sorted array or not ; function to find index of element ' x ' in sorted ' arr ' uses binary search technique ; function to check whether the given array is a ' k ' sorted array or not ; auxiliary array ' aux ' ; copy elements of ' arr ' to ' aux ' ; sort ' aux ' ; for every element of ' arr ' at index ' i ' , find its index ' j ' in ' aux ' ; index of arr [ i ] in sorted array ' aux ' ; if abs ( i - j ) > k , then that element is not at - most k distance away from its target position . Thus , ' arr ' is not a k sorted array ; ' arr ' is a k sorted array ; Driver program to test above
#include <bits / stdc++.h> NEW_LINE using namespace std ; int binarySearch ( int arr [ ] , int low , int high , int x ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] > x ) high = mid - 1 ; else low = mid + 1 ; } } string isKSortedArray ( int arr [ ] , int n , int k ) { int aux [ n ] ; for ( int i = 0 ; i < n ; i ++ ) aux [ i ] = arr [ i ] ; sort ( aux , aux + n ) ; for ( int i = 0 ; i < n ; i ++ ) { int j = binarySearch ( aux , 0 , n - 1 , arr [ i ] ) ; if ( abs ( i - j ) > k ) return " No " ; } return " Yes " ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 , 5 , 6 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; cout << " Is ▁ it ▁ a ▁ k ▁ sorted ▁ array ? : ▁ " << isKSortedArray ( arr , n , k ) ; return 0 ; }
TimSort | C ++ program to perform TimSort . ; This function sorts array from left index to to right index which is of size atmost RUN ; Merge function merges the sorted runs ; Original array is broken in two parts left and right array ; After comparing , we merge those two array in larger sub array ; Copy remaining elements of left , if any ; Copy remaining element of right , if any ; Iterative Timsort function to sort the array [ 0. . . n - 1 ] ( similar to merge sort ) ; Sort individual subarrays of size RUN ; Start merging from size RUN ( or 32 ) . It will merge to form size 64 , then 128 , 256 and so on ... . ; pick starting point of left sub array . We are going to merge arr [ left . . left + size - 1 ] and arr [ left + size , left + 2 * size - 1 ] After every merge , we increase left by 2 * size ; find ending point of left sub array mid + 1 is starting point of right sub array ; merge sub array arr [ left ... . . mid ] & arr [ mid + 1. ... right ] ; Utility function to print the Array ; Driver program to test above function ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int RUN = 32 ; void insertionSort ( int arr [ ] , int left , int right ) { for ( int i = left + 1 ; i <= right ; i ++ ) { int temp = arr [ i ] ; int j = i - 1 ; while ( j >= left && arr [ j ] > temp ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } void merge ( int arr [ ] , int l , int m , int r ) { int len1 = m - l + 1 , len2 = r - m ; int left [ len1 ] , right [ len2 ] ; for ( int i = 0 ; i < len1 ; i ++ ) left [ i ] = arr [ l + i ] ; for ( int i = 0 ; i < len2 ; i ++ ) right [ i ] = arr [ m + 1 + i ] ; int i = 0 ; int j = 0 ; int k = l ; while ( i < len1 && j < len2 ) { if ( left [ i ] <= right [ j ] ) { arr [ k ] = left [ i ] ; i ++ ; } else { arr [ k ] = right [ j ] ; j ++ ; } k ++ ; } while ( i < len1 ) { arr [ k ] = left [ i ] ; k ++ ; i ++ ; } while ( j < len2 ) { arr [ k ] = right [ j ] ; k ++ ; j ++ ; } } void timSort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i += RUN ) insertionSort ( arr , i , min ( ( i + RUN - 1 ) , ( n - 1 ) ) ) ; for ( int size = RUN ; size < n ; size = 2 * size ) { for ( int left = 0 ; left < n ; left += 2 * size ) { int mid = left + size - 1 ; int right = min ( ( left + 2 * size - 1 ) , ( n - 1 ) ) ; if ( mid < right ) merge ( arr , left , mid , right ) ; } } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; printf ( " STRNEWLINE " ) ; } int main ( ) { int arr [ ] = { -2 , 7 , 15 , -14 , 0 , 15 , 0 , 7 , -7 , -4 , -13 , 5 , 8 , -14 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Given ▁ Array ▁ is STRNEWLINE " ) ; printArray ( arr , n ) ; timSort ( arr , n ) ; printf ( " After ▁ Sorting ▁ Array ▁ is STRNEWLINE " ) ; printArray ( arr , n ) ; return 0 ; }
Program to print an array in Pendulum Arrangement | C ++ program for pendulum arrangement of numbers ; Prints pendulum arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; Driver function ; input Array ; calling pendulum function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pendulumArrangement ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int op [ n ] ; int mid = ( n - 1 ) / 2 ; int j = 1 , i = 1 ; op [ mid ] = arr [ 0 ] ; for ( i = 1 ; i <= mid ; i ++ ) { op [ mid + i ] = arr [ j ++ ] ; op [ mid - i ] = arr [ j ++ ] ; } if ( n % 2 == 0 ) op [ mid + i ] = arr [ j ] ; cout << " Pendulum ▁ arrangement : " << endl ; for ( i = 0 ; i < n ; i ++ ) cout << op [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 14 , 6 , 19 , 21 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pendulumArrangement ( arr , n ) ; return 0 ; }
Maximum width of a binary tree | C ++ program to calculate width of binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; A utility function to get height of a binary tree ; Function to get the maximum width of a binary tree ; Create an array that will store count of nodes at each level ; Fill the count array using preorder traversal ; Return the maximum value from count array ; A function that fills count array with count of nodes at every level of given binary tree ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Return the maximum value from count array ; Driver code ; Constructed bunary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; } ; int height ( node * node ) ; node * newNode ( int data ) ; void getMaxWidthRecur ( node * root , int count [ ] , int level ) ; int getMax ( int arr [ ] , int n ) ; int getMaxWidth ( node * root ) { int width ; int h = height ( root ) ; int * count = new int [ h ] ; int level = 0 ; getMaxWidthRecur ( root , count , level ) ; return getMax ( count , h ) ; } void getMaxWidthRecur ( node * root , int count [ ] , int level ) { if ( root ) { count [ level ] ++ ; getMaxWidthRecur ( root -> left , count , level + 1 ) ; getMaxWidthRecur ( root -> right , count , level + 1 ) ; } } int height ( node * node ) { if ( node == NULL ) return 0 ; else { int lHeight = height ( node -> left ) ; int rHeight = height ( node -> right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } node * newNode ( int data ) { node * Node = new node ( ) ; Node -> data = data ; Node -> left = NULL ; Node -> right = NULL ; return ( Node ) ; } int getMax ( int arr [ ] , int n ) { int max = arr [ 0 ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> right -> left = newNode ( 6 ) ; root -> right -> right -> right = newNode ( 7 ) ; cout << " Maximum ▁ width ▁ is ▁ " << getMaxWidth ( root ) << endl ; return 0 ; }
Minimize the sum of product of two arrays with permutations allowed | C ++ program to calculate minimum sum of product of two arrays . ; Returns minimum sum of product of two arrays with permutations allowed ; Sort A and B so that minimum and maximum value can easily be fetched . ; Multiplying minimum value of A and maximum value of B ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int minValue ( int A [ ] , int B [ ] , int n ) { sort ( A , A + n ) ; sort ( B , B + n ) ; long long int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( A [ i ] * B [ n - i - 1 ] ) ; return result ; } int main ( ) { int A [ ] = { 3 , 1 , 1 } ; int B [ ] = { 6 , 5 , 4 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minValue ( A , B , n ) << endl ; return 0 ; }
Count of numbers in range [ L , R ] which can be represented as sum of two perfect powers | C ++ program for the above approach ; Function to find the number of numbers that can be expressed in the form of the sum of two perfect powers ; Stores all possible powers ; Push 1 and 0 in it ; Iterate over all the exponents ; Iterate over all possible numbers ; This loop will run for a maximum of sqrt ( R ) times ; Push this power in the array pows [ ] ; Increase the number ; Stores if i can be expressed as the sum of perfect power or not ; Iterate over all possible pairs of the array pows [ ] ; The number is valid ; Find the prefix sum of the array ok [ ] ; Return the count of required number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int TotalPerfectPowerSum ( long long L , long long R ) { vector < long long > pows ; pows . push_back ( 0 ) ; pows . push_back ( 1 ) ; for ( int p = 2 ; p < 25 ; p ++ ) { long long int num = 2 ; while ( ( long long int ) ( pow ( num , p ) + 0.5 ) <= R ) { pows . push_back ( ( long long int ) ( pow ( num , p ) + 0.5 ) ) ; num ++ ; } } int ok [ R + 1 ] ; memset ( ok , 0 , sizeof ( ok ) ) ; for ( int i = 0 ; i < pows . size ( ) ; i ++ ) { for ( int j = 0 ; j < pows . size ( ) ; j ++ ) { if ( pows [ i ] + pows [ j ] <= R and pows [ i ] + pows [ j ] >= L ) { ok [ pows [ i ] + pows [ j ] ] = 1 ; } } } for ( int i = 0 ; i <= R ; i ++ ) { ok [ i ] += ok [ i - 1 ] ; } return ok [ R ] - ok [ L - 1 ] ; } signed main ( ) { int L = 5 , R = 8 ; cout << TotalPerfectPowerSum ( L , R ) ; return 0 ; }
Vertical width of Binary tree | Set 1 | CPP program to print vertical width of a tree ; A Binary Tree Node ; get vertical width ; traverse left ; if curr is decrease then get value in minimum ; if curr is increase then get value in maximum ; traverse right ; 1 is added to include root in the width ; Utility function to create a new tree node ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; void lengthUtil ( Node * root , int & maximum , int & minimum , int curr = 0 ) { if ( root == NULL ) return ; lengthUtil ( root -> left , maximum , minimum , curr - 1 ) ; if ( minimum > curr ) minimum = curr ; if ( maximum < curr ) maximum = curr ; lengthUtil ( root -> right , maximum , minimum , curr + 1 ) ; } int getLength ( Node * root ) { int maximum = 0 , minimum = 0 ; lengthUtil ( root , maximum , minimum , 0 ) ; return ( abs ( minimum ) + maximum ) + 1 ; } Node * newNode ( int data ) { Node * curr = new Node ; curr -> data = data ; curr -> left = curr -> right = NULL ; return curr ; } int main ( ) { Node * root = newNode ( 7 ) ; root -> left = newNode ( 6 ) ; root -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 2 ) ; root -> right -> right = newNode ( 1 ) ; cout << getLength ( root ) << " STRNEWLINE " ; return 0 ; }
Maximize Array sum after changing sign of any elements for exactly M times | C ++ program for the above approach ; Function to find the maximum sum with M flips ; Declare a priority queue i . e . min heap ; Declare the sum as zero ; Push all elements of the array in it ; Iterate for M times ; Get the top element ; Flip the sign of the top element ; Remove the top element ; Update the sum ; Push the temp into the queue ; Driver program ; Given input
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaximumSumWithMflips ( int arr [ ] , int N , int M ) { priority_queue < int , vector < int > , greater < int > > pq ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { pq . push ( arr [ i ] ) ; sum += arr [ i ] ; } while ( M -- ) { sum -= pq . top ( ) ; int temp = -1 * pq . top ( ) ; pq . pop ( ) ; sum += temp ; pq . push ( temp ) ; } cout << sum ; } int main ( ) { int arr [ ] = { -3 , 7 , -1 , -5 , -3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = 4 ; findMaximumSumWithMflips ( arr , N , M ) ; return 0 ; }
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other | C ++ program for the above approach ; Function to find the minimum number of operations to make all the elements of the array equal ; Store the sum of the array arr [ ] ; Traverse through the array ; If it is not possible to make all array element equal ; Store the minimum number of operations needed ; Traverse through the array ; Finally , print the minimum number operation to make array elements equal ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void miniOperToMakeAllEleEqual ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % n ) { cout << -1 ; return ; } int valueAfterDivision = sum / n ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( abs ( valueAfterDivision - arr [ i ] ) % k != 0 ) { cout << -1 ; return ; } count += abs ( valueAfterDivision - arr [ i ] ) / k ; } cout << count / 2 << endl ; } int main ( ) { int n = 3 , k = 3 ; int arr [ 3 ] = { 5 , 8 , 11 } ; miniOperToMakeAllEleEqual ( arr , n , k ) ; return 0 ; }
Minimum operations to make product of adjacent element pair of prefix sum negative | C ++ code for the above approach ; Function to find minimum operations needed to make the product of any two adjacent elements in prefix sum array negative ; Stores the minimum operations ; Stores the prefix sum and number of operations ; Traverse the array ; Update the value of sum ; Check if i + r is odd ; Check if prefix sum is not positive ; Update the value of ans and sum ; Check if prefix sum is not negative ; Update the value of ans and sum ; Update the value of res ; Print the value of res ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperations ( vector < int > a ) { int res = INT_MAX ; int N = a . size ( ) ; for ( int r = 0 ; r < 2 ; r ++ ) { int sum = 0 , ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( ( i + r ) % 2 ) { if ( sum <= 0 ) { ans += - sum + 1 ; sum = 1 ; } } else { if ( sum >= 0 ) { ans += sum + 1 ; sum = -1 ; } } } res = min ( res , ans ) ; } cout << res ; } int main ( ) { vector < int > a { 1 , -3 , 1 , 0 } ; minOperations ( a ) ; return 0 ; }
Minimize sum of product of same | C ++ program to implement the above approach ; Function to print1 the arrays ; Function to reverse1 the subarray ; Function to calculate the minimum product of same - indexed elements of two given arrays ; Calculate initial product ; Traverse all odd length subarrays ; Remove the previous product ; Add the current product ; Check if current product is minimum or not ; Traverse all even length subarrays ; Remove the previous product ; Add to the current product ; Check if current product is minimum or not ; Update the pointers ; reverse1 the subarray ; print1 the subarray ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print1 ( vector < int > & a , vector < int > & b ) { int minProd = 0 ; for ( int i = 0 ; i < a . size ( ) ; ++ i ) { cout << a [ i ] << " ▁ " ; } cout << endl ; for ( int i = 0 ; i < b . size ( ) ; ++ i ) { cout << b [ i ] << " ▁ " ; minProd += a [ i ] * b [ i ] ; } cout << endl ; cout << minProd ; } void reverse1 ( int left , int right , vector < int > & arr ) { while ( left < right ) { int temp = arr [ left ] ; arr [ left ] = arr [ right ] ; arr [ right ] = temp ; ++ left ; -- right ; } } void minimumProdArray ( vector < int > & a , vector < int > & b , int l ) { int total = 0 ; for ( int i = 0 ; i < a . size ( ) ; ++ i ) { total += a [ i ] * b [ i ] ; } int min = INT_MAX ; int first = 0 ; int second = 0 ; for ( int i = 0 ; i < a . size ( ) ; ++ i ) { int left = i - 1 ; int right = i + 1 ; int total1 = total ; while ( left >= 0 && right < a . size ( ) ) { total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; if ( min >= total1 ) { min = total1 ; first = left ; second = right ; } -- left ; ++ right ; } } for ( int i = 0 ; i < a . size ( ) ; ++ i ) { int left = i ; int right = i + 1 ; int total1 = total ; while ( left >= 0 && right < a . size ( ) ) { total1 -= a [ left ] * b [ left ] + a [ right ] * b [ right ] ; total1 += a [ left ] * b [ right ] + a [ right ] * b [ left ] ; if ( min >= total1 ) { min = total1 ; first = left ; second = right ; } -- left ; ++ right ; } } if ( min < total ) { reverse1 ( first , second , a ) ; print1 ( a , b ) ; } else { print1 ( a , b ) ; } } int main ( ) { int n = 4 ; vector < int > a { 2 , 3 , 1 , 5 } ; vector < int > b { 8 , 2 , 4 , 3 } ; minimumProdArray ( a , b , n ) ; }
Maximize cost of removing all occurrences of substrings " ab " and " ba " | C ++ program for the above approach : ; Function to find the maximum cost of removing substrings " ab " and " ba " from S ; MaxStr is the substring char array with larger cost ; MinStr is the substring char array with smaller cost ; ; Denotes larger point ; Denotes smaller point ; Stores cost scored ; Stack to keep track of characters ; Traverse the string ; If the substring is maxstr ; Pop from the stack ; Add maxp to cost ; Push the character to the stack ; Remaining string after removing maxstr ; Find remaining string ; Reversing the string retrieved from the stack ; Removing all occurences of minstr ; If the substring is minstr ; Pop from the stack ; Add minp to the cost ; Otherwise ; Return the maximum cost ; Driver Code ; Input String ; Costs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxCollection ( string S , int P , int Q ) { char maxstr [ 2 ] ; string x = ( P >= Q ? " ab " : " ba " ) ; strcpy ( maxstr , x . c_str ( ) ) ; char minstr [ 2 ] ; x = ( P >= Q ? " ba " : " ab " ) ; strcpy ( minstr , x . c_str ( ) ) ; int maxp = max ( P , Q ) ; int minp = min ( P , Q ) ; int cost = 0 ; stack < char > stack1 ; char s [ S . length ( ) ] ; strcpy ( s , S . c_str ( ) ) ; for ( auto & ch : s ) { if ( ! stack1 . empty ( ) && ( stack1 . top ( ) == maxstr [ 0 ] && ch == maxstr [ 1 ] ) ) { stack1 . pop ( ) ; cost += maxp ; } else { stack1 . push ( ch ) ; } } string sb = " " ; while ( stack1 . size ( ) > 0 ) { sb = sb + stack1 . top ( ) ; stack1 . pop ( ) ; } reverse ( sb . begin ( ) , sb . end ( ) ) ; for ( auto & ch : sb ) { if ( ! stack1 . empty ( ) && ( stack1 . top ( ) == minstr [ 0 ] && ch == minstr [ 1 ] ) ) { stack1 . pop ( ) ; cost += minp ; } else { stack1 . push ( ch ) ; } } return cost ; } int main ( ) { string S = " cbbaabbaab " ; int P = 6 ; int Q = 4 ; cout << MaxCollection ( S , P , Q ) ; return 0 ; }
Vertical width of Binary tree | Set 2 | CPP code to find vertical width of a binary tree ; Tree class ; Function to fill hd in set . ; Third parameter is horizontal distance ; Driver Code ; Creating the above tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * left , * right ; Node ( int data_new ) { data = data_new ; left = right = NULL ; } } ; void fillSet ( Node * root , unordered_set < int > & s , int hd ) { if ( ! root ) return ; fillSet ( root -> left , s , hd - 1 ) ; s . insert ( hd ) ; fillSet ( root -> right , s , hd + 1 ) ; } int verticalWidth ( Node * root ) { unordered_set < int > s ; fillSet ( root , s , 0 ) ; return s . size ( ) ; } int main ( ) { Node * root = NULL ; root = new Node ( 1 ) ; root -> left = new Node ( 2 ) ; root -> right = new Node ( 3 ) ; root -> left -> left = new Node ( 4 ) ; root -> left -> right = new Node ( 5 ) ; root -> right -> left = new Node ( 6 ) ; root -> right -> right = new Node ( 7 ) ; root -> right -> left -> right = new Node ( 8 ) ; root -> right -> right -> right = new Node ( 9 ) ; cout << verticalWidth ( root ) << " STRNEWLINE " ; return 0 ; }
Maximum value of ( arr [ i ] * arr [ j ] ) + ( arr [ j ] | C ++ program for the above approach ; Function to find the value of the expression a * b + ( b - a ) ; Function to find the maximum value of the expression a * b + ( b - a ) possible for any pair ( a , b ) ; Sort the vector in ascending order ; Stores the maximum value ; Update ans by choosing the pair of the minimum and 2 nd minimum ; Update ans by choosing the pair of maximum and 2 nd maximum ; Return the value of ans ; Driver Code ; Given inputs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calc ( int a , int b ) { return a * b + ( b - a ) ; } int findMaximum ( vector < int > arr , int N ) { sort ( arr . begin ( ) , arr . end ( ) ) ; int ans = -1e9 ; ans = max ( ans , calc ( arr [ 0 ] , arr [ 1 ] ) ) ; ans = max ( ans , calc ( arr [ N - 2 ] , arr [ N - 1 ] ) ) ; return ans ; } int main ( ) { vector < int > arr = { 0 , -47 , 12 } ; int N = ( int ) arr . size ( ) ; cout << findMaximum ( arr , N ) ; return 0 ; }