codeparrot/xlcost-text-to-code huggingface dataset:
Check if two strings can be made equal by reversing a substring of one of the strings | C ++ program for the above approach ; Function to check if the strings can be made equal or not by reversing a substring of X ; Store the first index from the left which contains unequal characters in both the strings ; Store the first element from the right which contains unequal characters in both the strings ; Checks for the first index from left in which characters in both the strings are unequal ; Store the current index ; Break out of the loop ; Checks for the first index from right in which characters in both the strings are unequal ; Store the current index ; Break out of the loop ; Reverse the substring X [ L , R ] ; If X and Y are equal ; Otherwise ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkString ( string X , string Y ) { int L = -1 ; int R = -1 ; for ( int i = 0 ; i < X . length ( ) ; ++ i ) { if ( X [ i ] != Y [ i ] ) { L = i ; break ; } } for ( int i = X . length ( ) - 1 ; i > 0 ; -- i ) { if ( X [ i ] != Y [ i ] ) { R = i ; break ; } } reverse ( X . begin ( ) + L , X . begin ( ) + R + 1 ) ; if ( X == Y ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { string X = " adcbef " , Y = " abcdef " ; checkString ( X , Y ) ; return 0 ; }
Maximum length of same indexed subarrays from two given arrays satisfying the given condition | C ++ program for the above approach ; Function to find maximum length of subarray such that sum of maximum element in subarray in brr [ ] and sum of subarray in arr [ ] * K is at most C ; Base Case ; Let maximum length be 0 ; Perform Binary search ; Find mid value ; Check if the current mid satisfy the given condition ; If yes , then store length ; Otherwise ; Return maximum length stored ; Function to check if it is possible to have such a subarray of length K ; Finds the maximum element in each window of size k ; Check for window of size K ; For all possible subarrays of length k ; Until deque is empty ; Calculate the total cost and check if less than equal to c ; Find sum of current subarray and the total cost ; Include the new element of current subarray ; Discard the element of last subarray ; Remove all the elements in the old window ; Calculate total cost and check <= c ; If current subarray length satisfies ; If it is not possible ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLength ( int a [ ] , int b [ ] , int n , int c ) { if ( n == 0 ) return 0 ; int max_length = 0 ; int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } bool possible ( int a [ ] , int b [ ] , int n , int c , int k ) { deque < int > dq ; int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; while ( dq . size ( ) > 0 && b [ i ] > b [ dq . back ( ) ] ) dq . pop_back ( ) ; dq . push_back ( i ) ; } int total_cost = sum * k + b [ dq . front ( ) ] ; if ( total_cost <= c ) return true ; for ( int i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; while ( dq . size ( ) > 0 && dq . front ( ) <= i - k ) dq . pop_front ( ) ; while ( dq . size ( ) > 0 && b [ i ] > b [ dq . back ( ) ] ) dq . pop_back ( ) ; dq . push_back ( i ) ; total_cost = sum * k + b [ dq . front ( ) ] ; if ( total_cost <= c ) return true ; } return false ; } int main ( ) { int A [ ] = { 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 } ; int B [ ] = { 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int C = 40 ; cout << maxLength ( A , B , N , C ) ; return 0 ; }
Program to find the Nth natural number with exactly two bits set | Set 2 | C ++ program for the above approach ; Function to find the Nth number with exactly two bits set ; Initialize variables ; Initialize the range in which the value of ' a ' is present ; Perform Binary Search ; Find the mid value ; Update the range using the mid value t ; Find b value using a and N ; Print the value 2 ^ a + 2 ^ b ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNthNum ( long long int N ) { long long int a , b , left ; long long int right , mid ; long long int t , last_num = 0 ; left = 1 , right = N ; while ( left <= right ) { mid = left + ( right - left ) / 2 ; t = ( mid * ( mid + 1 ) ) / 2 ; if ( t < N ) { left = mid + 1 ; } else if ( t == N ) { a = mid ; break ; } else { a = mid ; right = mid - 1 ; } } t = a - 1 ; b = N - ( t * ( t + 1 ) ) / 2 - 1 ; cout << ( 1 << a ) + ( 1 << b ) ; } int main ( ) { long long int N = 15 ; findNthNum ( N ) ; return 0 ; }
Longest subsequence having maximum sum | C ++ program to implement the above approach ; Function to find the longest subsequence from the given array with maximum sum ; Stores the largest element of the array ; If Max is less than 0 ; Print the largest element of the array ; Traverse the array ; If arr [ i ] is greater than or equal to 0 ; Print elements of the subsequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void longestSubWithMaxSum ( int arr [ ] , int N ) { int Max = * max_element ( arr , arr + N ) ; if ( Max < 0 ) { cout << Max ; return ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 ) { cout << arr [ i ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 1 , 2 , -4 , -2 , 3 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; longestSubWithMaxSum ( arr , N ) ; return 0 ; }
Check if string S2 can be obtained by appending subsequences of string S1 | C ++ Program to implement the above approach ; Function for finding minimum number of operations ; Stores the length of strings ; Stores frequency of characters in string s ; Update frequencies of character in s ; Traverse string s1 ; If any character in s1 is not present in s ; Stores the indices of each character in s ; Traverse string s ; Store indices of characters ; Stores index of last appended character ; Traverse string s1 ; Find the index of next character that can be appended ; Check if the current character be included in the current subsequence ; Otherwise ; Start a new subsequence ; Update index of last character appended ; Driver Code ; If S2 cannot be obtained from subsequences of S1 ; Otherwise
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int findMinimumOperations ( string s , string s1 ) { int n = s . length ( ) , m = s1 . length ( ) ; int frequency [ 26 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) frequency [ s [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < m ; i ++ ) { if ( frequency [ s1 [ i ] - ' a ' ] == 0 ) { return -1 ; } } set < int > indices [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { indices [ s [ i ] - ' a ' ] . insert ( i ) ; } int ans = 1 ; int last = ( * indices [ s1 [ 0 ] - ' a ' ] . begin ( ) ) ; for ( int i = 1 ; i < m ; i ++ ) { int ch = s1 [ i ] - ' a ' ; auto it = indices [ ch ] . upper_bound ( last ) ; if ( it != indices [ ch ] . end ( ) ) { last = ( * it ) ; } else { ans ++ ; last = ( * indices [ ch ] . begin ( ) ) ; } } return ans ; } int main ( ) { string S1 = " acebcd " , S2 = " acbcde " ; int ans = findMinimumOperations ( S1 , S2 ) ; if ( ans == -1 ) { cout << " NO STRNEWLINE " ; } else { cout << " YES STRNEWLINE " << ans ; } return 0 ; }
Sink Odd nodes in Binary Tree | Program to sink odd nodes to the bottom of binary tree ; A binary tree node ; Helper function to allocates a new node ; Helper function to check if node is leaf node ; A recursive method to sink a tree with odd root This method assumes that the subtrees are already sinked . This method is similar to Heapify of Heap - Sort ; If NULL or is a leaf , do nothing ; if left subtree exists and left child is even ; swap root 's data with left child and  fix left subtree ; if right subtree exists and right child is even ; swap root 's data with right child and  fix right subtree ; Function to sink all odd nodes to the bottom of binary tree . It does a postorder traversal and calls sink ( ) if any odd node is found ; If NULL or is a leaf , do nothing ; Process left and right subtrees before this node ; If root is odd , sink it ; Helper function to do Level Order Traversal of Binary Tree level by level . This function is used here only for showing modified tree . ; Do Level order traversal ; Print one level at a time ; Line separator for levels ; Driver program to test above functions ; Constructed binary tree is 1 / \ 5 8 / \ / \ 2 4 9 10
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newnode ( int data ) { Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } bool isLeaf ( Node * root ) { return ( root -> left == NULL && root -> right == NULL ) ; } void sink ( Node * & root ) { if ( root == NULL || isLeaf ( root ) ) return ; if ( root -> left && ! ( root -> left -> data & 1 ) ) { swap ( root -> data , root -> left -> data ) ; sink ( root -> left ) ; } else if ( root -> right && ! ( root -> right -> data & 1 ) ) { swap ( root -> data , root -> right -> data ) ; sink ( root -> right ) ; } } void sinkOddNodes ( Node * & root ) { if ( root == NULL || isLeaf ( root ) ) return ; sinkOddNodes ( root -> left ) ; sinkOddNodes ( root -> right ) ; if ( root -> data & 1 ) sink ( root ) ; } void printLevelOrder ( Node * root ) { queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { int nodeCount = q . size ( ) ; while ( nodeCount ) { Node * node = q . front ( ) ; printf ( " % d ▁ " , node -> data ) ; q . pop ( ) ; if ( node -> left != NULL ) q . push ( node -> left ) ; if ( node -> right != NULL ) q . push ( node -> right ) ; nodeCount -- ; } printf ( " STRNEWLINE " ) ; } } int main ( ) { Node * root = newnode ( 1 ) ; root -> left = newnode ( 5 ) ; root -> right = newnode ( 8 ) ; root -> left -> left = newnode ( 2 ) ; root -> left -> right = newnode ( 4 ) ; root -> right -> left = newnode ( 9 ) ; root -> right -> right = newnode ( 10 ) ; sinkOddNodes ( root ) ; printf ( " Level ▁ order ▁ traversal ▁ of ▁ modified ▁ tree STRNEWLINE " ) ; printLevelOrder ( root ) ; return 0 ; }
Print first K distinct Moran numbers from a given array |  ; Function to calculate the sum of digits of a number ; Stores the sum of digits ; Add the digit to sum ; Remove digit ; Returns the sum of digits ; Function to check if a number is prime or not ; If r has any divisor ; Set r as non - prime ; Function to check if a number is moran number ; Calculate sum of digits ; Check if n is divisible by the sum of digits ; Calculate the quotient ; If the quotient is prime ; Function to print the first K Moran numbers from the array ; Sort the given array ; Initialise a set ; Traverse the array from the end ; If the current array element is a Moran number ; Insert into the set ; Driver Code
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE #include <set> NEW_LINE using namespace std ; int digiSum ( int a ) { int sum = 0 ; while ( a ) { sum += a % 10 ; a = a / 10 ; } return sum ; } bool isPrime ( int r ) { bool s = true ; for ( int i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } bool isMorannumber ( int n ) { int dup = n ; int sum = digiSum ( dup ) ; if ( n % sum == 0 ) { int c = n / sum ; if ( isPrime ( c ) ) { return true ; } } return false ; } void FirstKMorannumber ( int a [ ] , int n , int k ) { int X = k ; sort ( a , a + n ) ; set < int > s ; for ( int i = n - 1 ; i >= 0 && k > 0 ; i -- ) { if ( isMorannumber ( a [ i ] ) ) { s . insert ( a [ i ] ) ; k -- ; } } if ( k > 0 ) { cout << X << " ▁ Moran ▁ numbers ▁ are " << " ▁ not ▁ present ▁ in ▁ the ▁ array " << endl ; return ; } set < int > :: iterator it ; for ( it = s . begin ( ) ; it != s . end ( ) ; ++ it ) { cout << * it << " , ▁ " ; } cout << endl ; } int main ( ) { int A [ ] = { 34 , 198 , 21 , 42 , 63 , 45 , 22 , 44 , 43 } ; int K = 4 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; FirstKMorannumber ( A , N , K ) ; return 0 ; }
Number of pairs whose sum is a power of 2 | Set 2 | C ++ program to implement the above approach ; Function to count all pairs whose sum is a power of two ; Stores the frequency of each element of the array ; Update frequency of array elements ; Stores count of required pairs ; Current power of 2 ; Traverse the array ; If pair does not exist ; Increment count of pairs ; Return the count of pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPair ( int arr [ ] , int n ) { map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; int ans = 0 ; for ( int i = 0 ; i < 31 ; i ++ ) { int key = pow ( 2 , i ) ; for ( int j = 0 ; j < n ; j ++ ) { int k = key - arr [ j ] ; if ( m . find ( k ) == m . end ( ) ) continue ; else ans += m [ k ] ; if ( k == arr [ j ] ) ans ++ ; } } return ans / 2 ; } int main ( ) { int arr [ ] = { 1 , 8 , 2 , 10 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPair ( arr , n ) << endl ; return 0 ; }
Longest Subsequence from a numeric String divisible by K | C ++ program for the above approach ; Function to if the integer representation of the current string is divisible by K ; Stores the integer representation of the string ; Check if the num is divisible by K ; Function to find the longest subsequence which is divisible by K ; If the number is divisible by K ; If current number is the maximum obtained so far ; Include the digit at current index ; Exclude the digit at current index ; Driver Code ; Printing the largest number which is divisible by K ; If no number is found to be divisible by K
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isdivisible ( string & newstr , long long K ) { long long num = 0 ; for ( int i = 0 ; i < newstr . size ( ) ; i ++ ) { num = num * 10 + newstr [ i ] - '0' ; } if ( num % K == 0 ) return true ; else return false ; } void findLargestNo ( string & str , string & newstr , string & ans , int index , long long K ) { if ( index == ( int ) str . length ( ) ) { if ( isdivisible ( newstr , K ) ) { if ( ( ans < newstr && ans . length ( ) == newstr . length ( ) ) || newstr . length ( ) > ans . length ( ) ) { ans = newstr ; } } return ; } string x = newstr + str [ index ] ; findLargestNo ( str , x , ans , index + 1 , K ) ; findLargestNo ( str , newstr , ans , index + 1 , K ) ; } int main ( ) { string str = "121400" ; string ans = " " , newstr = " " ; long long K = 8 ; findLargestNo ( str , newstr , ans , 0 , K ) ; if ( ans != " " ) cout << ans << endl ; else cout << -1 << endl ; }
Counting Rock Samples | TCS Codevita 2020 | C ++ program of the above approach ; Function to find the rock samples in the ranges ; Iterate over the ranges ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRockSample ( vector < vector < int > > ranges , int n , int r , vector < int > arr ) { vector < int > a ; for ( int i = 0 ; i < r ; i ++ ) { int c = 0 ; int l = ranges [ i ] [ 0 ] ; int h = ranges [ i ] [ 1 ] ; for ( int j = 0 ; j < arr . size ( ) ; j ++ ) { if ( l <= arr [ j ] && arr [ j ] <= h ) c += 1 ; } a . push_back ( c ) ; } for ( auto i : a ) cout << i << " ▁ " ; } int main ( ) { int n = 5 ; int r = 2 ; vector < int > arr = { 400 , 567 , 890 , 765 , 987 } ; vector < vector < int > > ranges = { { 300 , 380 } , { 800 , 1000 } } ; findRockSample ( ranges , n , r , arr ) ; }
Depth of the deepest odd level node in Binary Tree | C ++ program to find depth of the deepest odd level node ; A Tree node ; Utility function to create a new node ; Utility function which returns whether the current node is a leaf or not ; function to return the longest odd level depth if it exists otherwise 0 ; Base case return from here ; increment current level ; if curr_level is odd and its a leaf node ; A wrapper over deepestOddLevelDepth ( ) ; 10 / \ 28 13 / \ 14 15 / \ 23 24 Let us create Binary Tree shown in above example
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } bool isleaf ( Node * curr_node ) { return ( curr_node -> left == NULL && curr_node -> right == NULL ) ; } int deepestOddLevelDepthUtil ( Node * curr_node , int curr_level ) { if ( curr_node == NULL ) return 0 ; curr_level += 1 ; if ( curr_level % 2 != 0 && isleaf ( curr_node ) ) return curr_level ; return max ( deepestOddLevelDepthUtil ( curr_node -> left , curr_level ) , deepestOddLevelDepthUtil ( curr_node -> right , curr_level ) ) ; } int deepestOddLevelDepth ( Node * curr_node ) { return deepestOddLevelDepthUtil ( curr_node , 0 ) ; } int main ( ) { Node * root = newNode ( 10 ) ; root -> left = newNode ( 28 ) ; root -> right = newNode ( 13 ) ; root -> right -> left = newNode ( 14 ) ; root -> right -> right = newNode ( 15 ) ; root -> right -> right -> left = newNode ( 23 ) ; root -> right -> right -> right = newNode ( 24 ) ; cout << deepestOddLevelDepth ( root ) << endl ; return 0 ; }
N digit numbers having difference between the first and last digits as K | C ++ program for the above approach ; Function to store and check the difference of digits ; Base Case ; Last digit of the number to check the difference from the first digit ; Condition to avoid repeated values ; Update the string pt ; Recursive Call ; Update the string pt ; Recursive Call ; Any number can come in between first and last except the zero ; Recursive Call ; Function to place digit of the number ; When N is 1 and K > 0 , then the single number will be the first and last digit it cannot have difference greater than 0 ; This loop place the digit at the starting ; Recursive Call ; Vector to store results ; Generate all the resultant number ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( string st , vector < int > & result , int prev , int n , int K ) { if ( st . length ( ) == n ) { result . push_back ( stoi ( st ) ) ; return ; } if ( st . size ( ) == n - 1 ) { if ( prev - K >= 0 ) { string pt = " " ; pt += prev - K + 48 ; findNumbers ( st + pt , result , prev - K , n , K ) ; } if ( K != 0 && prev + K < 10 ) { string pt = " " ; pt += prev + K + 48 ; findNumbers ( st + pt , result , prev + K , n , K ) ; } } else { for ( int j = 1 ; j <= 9 ; j ++ ) { string pt = " " ; pt += j + 48 ; findNumbers ( st + pt , result , prev , n , K ) ; } } } vector < int > numDifference ( int N , int K ) { vector < int > res ; string st = " " ; if ( N == 1 && K == 0 ) { res . push_back ( 0 ) ; } else if ( N == 1 && K > 0 ) { return res ; } for ( int i = 1 ; i < 10 ; i ++ ) { string temp = " " ; temp += 48 + i ; findNumbers ( st + temp , res , i , N , K ) ; st = " " ; } return res ; } void numDifferenceUtil ( int N , int K ) { vector < int > res ; res = numDifference ( N , K ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { cout << res [ i ] << " ▁ " ; } } int main ( ) { int N = 2 , K = 9 ; numDifferenceUtil ( N , K ) ; return 0 ; }
Minimum Number of Bullets required to penetrate all bricks | C ++ program for the above approach ; Custom comparator function ; Function to find the minimum number of bullets required to penetrate all bricks ; Sort the points in ascending order ; Check if there are no points ; Iterate through all the points ; Increase the count ; Return the count ; Driver Code ; Given coordinates of bricks ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( vector < int > & a , vector < int > & b ) { return a [ 1 ] < b [ 1 ] ; } int findMinBulletShots ( vector < vector < int > > & points ) { sort ( points . begin ( ) , points . end ( ) , compare ) ; if ( points . size ( ) == 0 ) return 0 ; int cnt = 1 ; int curr = points [ 0 ] [ 1 ] ; for ( int j = 1 ; j < points . size ( ) ; j ++ ) { if ( curr < points [ j ] [ 0 ] ) { cnt ++ ; curr = points [ j ] [ 1 ] ; } } return cnt ; } int main ( ) { vector < vector < int > > bricks { { 5000 , 900000 } , { 1 , 100 } , { 150 , 499 } } ; cout << findMinBulletShots ( bricks ) ; return 0 ; }
Count array elements that can be maximized by adding any permutation of first N natural numbers | C ++ program for the above approach ; Comparator function to sort the given array ; Function to get the count of values that can have the maximum value ; Sort array in decreasing order ; Stores the answer ; mark stores the maximum value till each index i ; Check if arr [ i ] can be maximum ; Update the mark ; Print the total count ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool cmp ( int x , int y ) { return x > y ; } void countMaximum ( int * a , int n ) { sort ( a , a + n , cmp ) ; int count = 0 ; int mark = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( a [ i ] + n >= mark ) ) { count += 1 ; } mark = max ( mark , a [ i ] + i + 1 ) ; } cout << count ; } int main ( ) { int arr [ ] = { 8 , 9 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countMaximum ( arr , N ) ; }
Kth character from the Nth string obtained by the given operations | C ++ program for the above approach ; Function to return Kth character from recursive string ; If N is 1 then return A ; Iterate a loop and generate the recursive string ; Update current string ; Change A to B and B to A ; Reverse the previous string ; Return the kth character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char findKthChar ( int n , int k ) { string prev = " A " ; string cur = " " ; if ( n == 1 ) { return ' A ' ; } for ( int i = 2 ; i <= n ; i ++ ) { cur = prev + " B " ; for ( int i = 0 ; i < prev . length ( ) ; i ++ ) { if ( prev [ i ] == ' A ' ) { prev [ i ] = ' B ' ; } else { prev [ i ] = ' A ' ; } } reverse ( prev . begin ( ) , prev . end ( ) ) ; cur += prev ; prev = cur ; } return cur [ k - 1 ] ; } int main ( ) { int N = 4 ; int K = 3 ; cout << findKthChar ( N , K ) ; return 0 ; }
Maximize length of subarray of equal elements by performing at most K increment operations | C ++ 14 program for above approach ; Function to find the maximum length of subarray of equal elements after performing at most K increments ; Stores the size of required subarray ; Starting point of a window ; Stores the sum of window ; Iterate over array ; Current element ; Remove index of minimum elements from deque which are less than the current element ; Insert current index in deque ; Update current window sum ; Calculate required operation to make current window elements equal ; If cost is less than k ; Shift window start pointer towards right and update current window sum ; Return answer ; Driver Code ; Length of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define newl  " NEW_LINE " int maxSubarray ( int a [ ] , int k , int n ) { int answer = 0 ; int start = 0 ; long int s = 0 ; deque < int > dq ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; while ( ! dq . empty ( ) && a [ dq . front ( ) ] <= x ) dq . pop_front ( ) ; dq . push_back ( i ) ; s += x ; long int cost = ( long int ) a [ dq . front ( ) ] * ( answer + 1 ) - s ; if ( cost <= ( long int ) k ) answer ++ ; else { if ( dq . front ( ) == start ) dq . pop_front ( ) ; s -= a [ start ++ ] ; } } return answer ; } int main ( ) { int a [ ] = { 2 , 2 , 4 } ; int k = 10 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << ( maxSubarray ( a , k , n ) ) ; return 0 ; }