codeparrot/xlcost-text-to-code huggingface dataset:
Smallest element present in every subarray of all possible lengths | Java program for the above approach ; Function to add count of numbers in the map for a subarray of length k ; Set to store unique elements ; Add elements to the set ; Iterator of the set ; Adding count in map ; Function to check if there is any number which repeats itself in every subarray of length K ; Check all number starting from 1 ; Check if i occurred n - k + 1 times ; Print the smallest number ; Print - 1 , if no such number found ; Function to count frequency of each number in each subarray of length K ; Traverse all subarrays of length K ; Check and print the smallest number present in every subarray and print it ; Function to generate the value of K ; Function call ; Driver code ; Given array ; Size of array ; Function call
import java . util . * ; import java . lang . * ; class GFG { static void uniqueElements ( int arr [ ] , int start , int K , Map < Integer , Integer > mp ) { Set < Integer > st = new HashSet < > ( ) ; for ( int i = 0 ; i < K ; i ++ ) st . add ( arr [ start + i ] ) ; Iterator itr = st . iterator ( ) ; while ( itr . hasNext ( ) ) { Integer t = ( Integer ) itr . next ( ) ; mp . put ( t , mp . getOrDefault ( t , 0 ) + 1 ) ; } } static void checkAnswer ( Map < Integer , Integer > mp , int N , int K ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( mp . containsKey ( i ) ) if ( mp . get ( i ) == ( N - K + 1 ) ) { System . out . print ( i + " ▁ " ) ; return ; } } System . out . print ( - 1 + " ▁ " ) ; } static void smallestPresentNumber ( int arr [ ] , int N , int K ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i <= N - K ; i ++ ) { uniqueElements ( arr , i , K , mp ) ; } checkAnswer ( mp , N , K ) ; } static void generateK ( int arr [ ] , int N ) { for ( int k = 1 ; k <= N ; k ++ ) smallestPresentNumber ( arr , N , k ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 } ; int N = arr . length ; generateK ( arr , N ) ; } }
Deepest right leaf node in a binary tree | Iterative approach | Java program to find deepest right leaf node of binary tree ; tree node ; returns a new tree Node ; return the deepest right leaf node of binary tree ; create a queue for level order traversal ; traverse until the queue is empty ; Since we go level by level , the last stored right leaf node is deepest one ; Driver code ; construct a tree
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node getDeepestRightLeafNode ( Node root ) { if ( root == null ) return null ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; Node result = null ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . poll ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; if ( temp . right . left == null && temp . right . right == null ) result = temp . right ; } } return result ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . right = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; Node result = getDeepestRightLeafNode ( root ) ; if ( result != null ) System . out . println ( " Deepest ▁ Right ▁ Leaf ▁ Node ▁ : : ▁ " + result . data ) ; else System . out . println ( "No result, right leaf not foundNEW_LINE"); } }
Check if two strings can be made equal by reversing a substring of one of the strings | Java 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
import java . util . * ; class GFG { static void checkString ( String X , String Y ) { int L = - 1 ; int R = - 1 ; for ( int i = 0 ; i < X . length ( ) ; ++ i ) { if ( X . charAt ( i ) != Y . charAt ( i ) ) { L = i ; break ; } } for ( int i = X . length ( ) - 1 ; i > 0 ; -- i ) { if ( X . charAt ( i ) != Y . charAt ( i ) ) { R = i ; break ; } } X = X . substring ( 0 , L ) + reverse ( X . substring ( L , R + 1 ) ) + X . substring ( R + 1 ) ; if ( X . equals ( Y ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String X = " adcbef " , Y = " abcdef " ; checkString ( X , Y ) ; } }
Maximum length of same indexed subarrays from two given arrays satisfying the given condition | Java 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
import java . io . * ; import java . util . * ; class GFG { public static 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 ; } public static boolean possible ( int a [ ] , int b [ ] , int n , int c , int k ) { Deque < Integer > dq = new LinkedList < Integer > ( ) ; int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; while ( dq . size ( ) > 0 && b [ i ] > b [ dq . peekLast ( ) ] ) dq . pollLast ( ) ; dq . addLast ( i ) ; } int total_cost = sum * k + b [ dq . peekFirst ( ) ] ; 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 . peekFirst ( ) <= i - k ) dq . pollFirst ( ) ; while ( dq . size ( ) > 0 && b [ i ] > b [ dq . peekLast ( ) ] ) dq . pollLast ( ) ; dq . add ( i ) ; total_cost = sum * k + b [ dq . peekFirst ( ) ] ; if ( total_cost <= c ) return true ; } return false ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 } ; int B [ ] = { 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 } ; int N = A . length ; int C = 40 ; System . out . println ( maxLength ( A , B , N , C ) ) ; } }
Program to find the Nth natural number with exactly two bits set | Set 2 | Java 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
import java . util . * ; class GFG { static void findNthNum ( int N ) { int a = 0 , b , left ; int right , mid ; 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 ; System . out . print ( ( 1 << a ) + ( 1 << b ) ) ; } public static void main ( String [ ] args ) { int N = 15 ; findNthNum ( N ) ; } }
Longest subsequence having maximum sum | Java 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
import java . util . * ; class GFG { static void longestSubWithMaxSum ( int arr [ ] , int N ) { int Max = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; if ( Max < 0 ) { System . out . print ( Max ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , - 4 , - 2 , 3 , 0 } ; int N = arr . length ; longestSubWithMaxSum ( arr , N ) ; } }
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
import java . io . * ; import java . util . * ; class GFG { static int digiSum ( int a ) { int sum = 0 ; while ( a != 0 ) { sum += a % 10 ; a = a / 10 ; } return sum ; } static boolean isPrime ( int r ) { boolean s = true ; for ( int i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } static boolean 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 ; } static void FirstKMorannumber ( int [ ] a , int n , int k ) { int X = k ; Arrays . sort ( a ) ; TreeSet < Integer > s = new TreeSet < Integer > ( ) ; for ( int i = n - 1 ; i >= 0 && k > 0 ; i -- ) { if ( isMorannumber ( a [ i ] ) ) { s . add ( a [ i ] ) ; k -- ; } } if ( k > 0 ) { System . out . println ( X + " ▁ Moran ▁ numbers ▁ are " + " ▁ not ▁ present ▁ in ▁ the ▁ array " ) ; return ; } for ( int value : s ) System . out . print ( value + " , ▁ " ) ; System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] A = { 34 , 198 , 21 , 42 , 63 , 45 , 22 , 44 , 43 } ; int K = 4 ; int N = A . length ; FirstKMorannumber ( A , N , K ) ; } }
Number of pairs whose sum is a power of 2 | Set 2 | Java program to implement the above approach ; Function to count all pairs whose sum is power of two ; Stores the frequency of each element of the array ; Update the frequency of array elements ; Stores the count of pairs ; Current power of 2 ; Traverse the array ; Increment count of pairs ; Return count of pairs ; Driver Code
import java . util . * ; class GFG { static int countPair ( int [ ] arr , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) m . put ( arr [ i ] , m . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; int ans = 0 ; for ( int i = 0 ; i < 31 ; i ++ ) { int key = ( int ) Math . pow ( 2 , i ) ; for ( int j = 0 ; j < arr . length ; j ++ ) { int k = key - arr [ j ] ; ans += m . getOrDefault ( k , 0 ) ; if ( k == arr [ j ] ) ans ++ ; } } return ans / 2 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , - 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( countPair ( arr , n ) ) ; } }
Longest Subsequence from a numeric String divisible by K | Java 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
import java . util . * ; import java . lang . * ; class GFG { static boolean isdivisible ( StringBuilder newstr , long K ) { long num = 0 ; for ( int i = 0 ; i < newstr . length ( ) ; i ++ ) { num = num * 10 + newstr . charAt ( i ) - '0' ; } if ( num % K == 0 ) return true ; else return false ; } static void findLargestNo ( String str , StringBuilder newstr , StringBuilder ans , int index , long K ) { if ( index == str . length ( ) ) { if ( isdivisible ( newstr , K ) ) { if ( ( newstr . toString ( ) . compareTo ( ans . toString ( ) ) > 0 && ans . length ( ) == newstr . length ( ) ) || newstr . length ( ) > ans . length ( ) ) { ans . setLength ( 0 ) ; ans . append ( newstr ) ; } } return ; } StringBuilder x = new StringBuilder ( newstr . toString ( ) + str . charAt ( index ) ) ; findLargestNo ( str , x , ans , index + 1 , K ) ; findLargestNo ( str , newstr , ans , index + 1 , K ) ; } public static void main ( String [ ] args ) { String str = "121400" ; StringBuilder ans = new StringBuilder ( ) , newstr = new StringBuilder ( ) ; long K = 8 ; findLargestNo ( str , newstr , ans , 0 , K ) ; if ( ans . toString ( ) != " " ) System . out . println ( ans ) ; else System . out . println ( - 1 ) ; } }
Counting Rock Samples | TCS Codevita 2020 | Java program of the above approach ; Function to find the rock samples in the ranges ; Iterate over the ranges ; Driver Code ; Function call
import java . util . * ; import java . io . * ; class GFG { static ArrayList < Integer > findRockSample ( int ranges [ ] [ ] , int n , int r , int arr [ ] ) { ArrayList < Integer > a = new ArrayList < > ( ) ; 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 . length ; j ++ ) { if ( l <= arr [ j ] && arr [ j ] <= h ) c += 1 ; } a . add ( c ) ; } return a ; } public static void main ( String args [ ] ) { int n = 5 ; int r = 2 ; int arr [ ] = { 400 , 567 , 890 , 765 , 987 } ; int ranges [ ] [ ] = { { 300 , 380 } , { 800 , 1000 } } ; ArrayList < Integer > answer = new ArrayList < > ( ) ; answer = findRockSample ( ranges , n , r , arr ) ; for ( int i = 0 ; i < answer . size ( ) ; i ++ ) System . out . print ( answer . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } }
Depth of the deepest odd level node in Binary Tree | Java 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
class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return ( temp ) ; } static boolean isleaf ( Node curr_node ) { return ( curr_node . left == null && curr_node . right == null ) ; } static 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 Math . max ( deepestOddLevelDepthUtil ( curr_node . left , curr_level ) , deepestOddLevelDepthUtil ( curr_node . right , curr_level ) ) ; } static int deepestOddLevelDepth ( Node curr_node ) { return deepestOddLevelDepthUtil ( curr_node , 0 ) ; } public static void main ( String [ ] args ) { 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 ) ; System . out . println ( deepestOddLevelDepth ( root ) ) ; } }
N digit numbers having difference between the first and last digits as K | Java 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
import java . util . * ; @ SuppressWarnings ( " unchecked " ) class GFG { static void findNumbers ( String st , ArrayList result , int prev , int n , int K ) { if ( st . length ( ) == n ) { result . add ( Integer . parseInt ( st ) ) ; return ; } if ( st . length ( ) == n - 1 ) { if ( prev - K >= 0 ) { String pt = " " ; pt += ( char ) ( prev - K + 48 ) ; findNumbers ( st + pt , result , prev - K , n , K ) ; } if ( K != 0 && prev + K < 10 ) { String pt = " " ; pt += ( char ) ( prev + K + 48 ) ; findNumbers ( st + pt , result , prev + K , n , K ) ; } } else { for ( int j = 1 ; j <= 9 ; j ++ ) { String pt = " " ; pt += ( char ) ( j + 48 ) ; findNumbers ( st + pt , result , prev , n , K ) ; } } } static ArrayList numDifference ( int N , int K ) { ArrayList res = new ArrayList ( ) ; String st = " " ; if ( N == 1 && K == 0 ) { res . add ( 0 ) ; } else if ( N == 1 && K > 0 ) { return res ; } for ( int i = 1 ; i < 10 ; i ++ ) { String temp = " " ; temp += ( char ) ( 48 + i ) ; findNumbers ( st + temp , res , i , N , K ) ; st = " " ; } return res ; } static void numDifferenceUtil ( int N , int K ) { ArrayList res = new ArrayList ( ) ; res = numDifference ( N , K ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { System . out . print ( res . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 2 , K = 9 ; numDifferenceUtil ( N , K ) ; } }
Minimum Number of Bullets required to penetrate all bricks | Java program for above approach ; 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
import java . util . * ; class GFG { static int findMinBulletShots ( int [ ] [ ] points ) { Arrays . sort ( points , ( a , b ) -> a [ 1 ] - b [ 1 ] ) ; if ( points . length == 0 ) return 0 ; int cnt = 1 ; int curr = points [ 0 ] [ 1 ] ; for ( int j = 1 ; j < points . length ; j ++ ) { if ( curr < points [ j ] [ 0 ] ) { cnt ++ ; curr = points [ j ] [ 1 ] ; } } return cnt ; } public static void main ( String [ ] args ) { int [ ] [ ] bricks = { { 5000 , 900000 } , { 1 , 100 } , { 150 , 499 } } ; System . out . print ( findMinBulletShots ( bricks ) ) ; } }
Count array elements that can be maximized by adding any permutation of first N natural numbers | Java program for the above approach ; 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
import java . util . * ; class GFG { static void countMaximum ( Integer [ ] a , int n ) { Arrays . sort ( a , Collections . reverseOrder ( ) ) ; int count = 0 ; int mark = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( a [ i ] + n >= mark ) ) { count += 1 ; } mark = Math . max ( mark , a [ i ] + i + 1 ) ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 8 , 9 , 6 } ; int N = arr . length ; countMaximum ( arr , N ) ; } }
Kth character from the Nth string obtained by the given operations | Java program for the above approach ; String reverse ; 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
import java . util . * ; class GFG { static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } static char findKthChar ( int n , int k ) { String prev = " A " ; String cur = " " ; if ( n == 1 ) { return ' A ' ; } for ( int j = 2 ; j <= n ; j ++ ) { cur = prev + " B " ; for ( int i = 0 ; i < prev . length ( ) ; i ++ ) { if ( prev . charAt ( i ) == ' A ' ) { prev . replace ( prev . charAt ( i ) , ' B ' ) ; } else { prev . replace ( prev . charAt ( i ) , ' A ' ) ; } } prev = reverse ( prev ) ; cur += prev ; prev = cur ; } return cur . charAt ( k ) ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 3 ; System . out . print ( findKthChar ( N , K ) ) ; } }
Maximize length of subarray of equal elements by performing at most K increment operations | Java Program for above approach ; Function to find the maximum length of subarray of equal elements after performing at most K increments ; Length of array ; 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 ; Function call
import java . util . * ; class GFG { static int maxSubarray ( int [ ] a , int k ) { int n = a . length ; int answer = 0 ; int start = 0 ; long s = 0 ; Deque < Integer > dq = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; while ( ! dq . isEmpty ( ) && a [ dq . peek ( ) ] <= x ) dq . poll ( ) ; dq . add ( i ) ; s += x ; long cost = ( long ) a [ dq . peekFirst ( ) ] * ( answer + 1 ) - s ; if ( cost <= ( long ) k ) answer ++ ; else { if ( dq . peekFirst ( ) == start ) dq . pollFirst ( ) ; s -= a [ start ++ ] ; } } return answer ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 2 , 4 } ; int k = 10 ; System . out . println ( maxSubarray ( a , k ) ) ; } }