codeparrot/xlcost-text-to-code huggingface dataset:
Minimum increment or decrement required to sort the array | Top | Java program of the above approach ; Dp array to memoized the value recursive call ; Function to find the minimum increment or decrement needed to make the array sorted ; If only one element is present , then arr [ ] is sorted ; If dp [ N ] [ maxE ] is precalculated , then return the result ; Iterate from minE to maxE which placed at previous index ; Update the answer according to recurrence relation ; Memoized the value for dp [ N ] [ maxE ] ; Return the final result ; Driver Code ; Find the minimum and maximum element from the arr [ ] ; Function call
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 1000 ] [ 1000 ] ; static int minimumIncDec ( int arr [ ] , int N , int maxE , int minE ) { if ( N == 0 ) { return 0 ; } if ( dp [ N ] [ maxE ] != 0 ) return dp [ N ] [ maxE ] ; int ans = Integer . MAX_VALUE ; for ( int k = minE ; k <= maxE ; k ++ ) { int x = minimumIncDec ( arr , N - 1 , k , minE ) ; ans = Math . min ( ans , x + Math . abs ( arr [ N - 1 ] - k ) ) ; } dp [ N ] [ maxE ] = ans ; return dp [ N ] [ maxE ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . length ; int minE = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; int maxE = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; System . out . print ( minimumIncDec ( arr , N , maxE , minE ) ) ; } }
Sort the major diagonal of the matrix | Java implementation to sort the major diagonal of the matrix ; Function to sort the major diagonal of the matrix ; Loop to find the ith minimum element from the major diagonal ; Loop to find the minimum element from the unsorted matrix ; Swap to put the minimum element at the beginning of the major diagonal of matrix ; Loop to print the matrix ; Driven Code ; Sort the major Diagonal
class GFG { static void sortDiagonal ( int a [ ] [ ] , int M , int N ) { for ( int i = 0 ; i < M ; i ++ ) { int sm = a [ i ] [ i ] ; int pos = i ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( sm > a [ j ] [ j ] ) { sm = a [ j ] [ j ] ; pos = j ; } } swap ( a , i , i , pos , pos ) ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static void swap ( int [ ] [ ] a , int i , int i2 , int pos , int pos2 ) { int temp = a [ i ] [ i2 ] ; a [ i ] [ i2 ] = a [ pos ] [ pos2 ] ; a [ pos ] [ pos2 ] = temp ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 4 , 2 } , { 3 , 1 } } ; sortDiagonal ( a , 2 , 2 ) ; } }
Minimum cost to make an Array a permutation of first N natural numbers | Java program to calculate minimum cost to make an Array a permutation of first N natural numbers ; Function to calculate minimum cost for making permutation of size N ; sorting the array in ascending order ; To store the required answer ; Traverse the whole array ; Return the required answer ; Driver code ; Function call
import java . util . * ; class GFG { static int make_permutation ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += Math . abs ( i + 1 - arr [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 8 , 1 , 1 } ; int n = arr . length ; System . out . print ( make_permutation ( arr , n ) ) ; } }
Find maximum sum from top to bottom row with no adjacent diagonal elements | Java implementation to find maximum sum from top to bottom row with no adjacent diagonal elements ; Function to find the maximum path sum from top to bottom row ; Create an auxiliary array of next row with the element and it 's position ; Sort the auxiliary array ; Find maximum from row above to be added to the current element ; Find the maximum element from the next row that can be added to current row element ; Find the maximum sum ; Driver code ; Function to find maximum path
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int maxSum ( int [ ] [ ] V , int n , int m ) { int ans = 0 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { ArrayList < int [ ] > aux = new ArrayList < > ( ) ; for ( int j = 0 ; j < m ; ++ j ) { aux . add ( new int [ ] { V [ i + 1 ] [ j ] , j } ) ; } Collections . sort ( aux , ( a , b ) -> b [ 0 ] - a [ 0 ] ) ; for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < m ; ++ k ) { if ( aux . get ( k ) [ 1 ] - j == 0 || Math . abs ( aux . get ( k ) [ 1 ] - j ) > 1 ) { V [ i ] [ j ] += aux . get ( k ) [ 0 ] ; break ; } } } } for ( int i = 0 ; i < m ; ++ i ) { ans = Math . max ( ans , V [ 0 ] [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] V = { { 1 , 2 , 3 , 4 } , { 8 , 7 , 6 , 5 } , { 10 , 11 , 12 , 13 } } ; int n = V . length ; int m = V [ 0 ] . length ; System . out . println ( maxSum ( V , n , m ) ) ; } }
Minimum sum of product of elements of pairs of the given array | Java program to find the minimum product of sum of pair of element in array arr [ ] ; Function to find the minimum product ; Sort the array using STL sort ( ) function ; Initialise product to 1 ; Find product of sum of all pairs ; Return the product ; Driver code ; Function call to find product
import java . util . * ; class GFG { static int minimumProduct ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int product = 1 ; for ( int i = 0 ; i < n ; i += 2 ) { product *= ( arr [ i ] + arr [ i + 1 ] ) ; } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 3 , 1 , 7 , 8 } ; int n = arr . length ; System . out . print ( minimumProduct ( arr , n ) + "NEW_LINE"); } }
Check if elements of array can be arranged in AP , GP or HP | Java program to check if a given array form AP , GP or HP ; Returns true if arr [ 0. . n - 1 ] can form AP ; Base Case ; Sort array ; After sorting , difference between consecutive elements must be same . ; Traverse the given array and check if the difference between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form GP ; Base Case ; Sort array ; After sorting , common ratio between consecutive elements must be same . ; Traverse the given array and check if the common ratio between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form HP ; Base Case ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . ; Driver 's Code ; Function to check AP ; Function to check GP ; Function to check HP
import java . util . * ; class GFG { static boolean checkIsAP ( double arr [ ] , int n ) { if ( n == 1 ) return true ; Arrays . sort ( arr ) ; double d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] != d ) { return false ; } } return true ; } static boolean checkIsGP ( double arr [ ] , int n ) { if ( n == 1 ) return true ; Arrays . sort ( arr ) ; double r = arr [ 1 ] / arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } static boolean checkIsHP ( double arr [ ] , int n ) { if ( n == 1 ) { return true ; } double [ ] rec = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } if ( checkIsAP ( rec , n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { double arr [ ] = { 1.0 / 5.0 , 1.0 / 10.0 , 1.0 / 15.0 , 1.0 / 20.0 } ; int n = arr . length ; int flag = 0 ; if ( checkIsAP ( arr , n ) ) { System . out . print ( " Yes , ▁ An ▁ AP ▁ can ▁ be ▁ formed " + "NEW_LINE"); flag = 1 ; } if ( checkIsGP ( arr , n ) ) { System . out . print ( " Yes , ▁ A ▁ GP ▁ can ▁ be ▁ formed " + "NEW_LINE"); flag = 1 ; } if ( checkIsHP ( arr , n ) ) { System . out . print ( " Yes , ▁ A ▁ HP ▁ can ▁ be ▁ formed " + "NEW_LINE"); flag = 1 ; } else if ( flag == 0 ) { System . out . print ( " No " ) ; } } }
Sort the Array by reversing the numbers in it | Java implementation of the approach ; Function to return the reverse of n ; Function to sort the array according to the reverse of elements ; Vector to store the reverse with respective elements ; Inserting reverse with elements in the vector pair ; Sort the vector , this will sort the pair according to the reverse of elements ; Print the sorted vector content ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static void sortArr ( int arr [ ] , int n ) { ArrayList < int [ ] > vp = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { vp . add ( new int [ ] { reversDigits ( arr [ i ] ) , arr [ i ] } ) ; } Collections . sort ( vp , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) System . out . print ( vp . get ( i ) [ 1 ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 10 , 102 , 31 , 15 } ; int n = arr . length ; sortArr ( arr , n ) ; } }
Sort the given Matrix | Memory Efficient Approach | Java implementation to sort the given matrix in strict order ; Function to sort the matrix ; Number of elements in matrix ; Loop to sort the matrix using Bubble Sort ; Condition to check if the Adjacent elements ; Swap if previous value is greater ; Loop to print the matrix ; Driver Code ; Function call to sort ; Function call to print matrix
class GFG { static void sortMat ( int [ ] [ ] data , int row , int col ) { int size = row * col ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size - 1 ; j ++ ) { if ( data [ j / col ] [ j % col ] > data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] ) { int temp = data [ j / col ] [ j % col ] ; data [ j / col ] [ j % col ] = data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] ; data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] = temp ; } } } } static void printMat ( int [ ] [ ] mat , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 5 , 4 , 7 } , { 1 , 3 , 8 } , { 2 , 9 , 6 } } ; int row = mat . length ; int col = mat [ 0 ] . length ; sortMat ( mat , row , col ) ; printMat ( mat , row , col ) ; } }
Partition the array into two odd length groups with minimized absolute difference between their median | Java program to minimise the median between partition array ; Function to find minimise the median between partition array ; Sort the given array arr [ ] ; Return the difference of two middle element of the arr [ ] ; Driver Code ; Size of arr [ ] ; Function that returns the minimum the absolute difference between median of partition array
import java . util . * ; class GFG { static int minimiseMedian ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; return Math . abs ( arr [ n / 2 ] - arr [ ( n / 2 ) - 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 15 , 25 , 35 , 50 } ; int n = arr . length ; System . out . println ( minimiseMedian ( arr , n ) ) ; } }
Maximum number of overlapping Intervals | Java program that print maximum number of overlap among given ranges ; Function that print maximum overlap among ranges ; Variable to store the maximum count ; Storing the x and y coordinates in data vector ; Pushing the x coordinate ; pushing the y coordinate ; Sorting of ranges ; Traverse the data vector to count number of overlaps ; If x occur it means a new range is added so we increase count ; If y occur it means a range is ended so we decrease count ; Updating the value of ans after every traversal ; Printing the maximum value ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static class pair { int first ; char second ; pair ( int first , char second ) { this . first = first ; this . second = second ; } } static void overlap ( int [ ] [ ] v ) { int ans = 0 ; int count = 0 ; ArrayList < pair > data = new ArrayList < > ( ) ; for ( int i = 0 ; i < v . length ; i ++ ) { data . add ( new pair ( v [ i ] [ 0 ] , ' x ' ) ) ; data . add ( new pair ( v [ i ] [ 1 ] , ' y ' ) ) ; } Collections . sort ( data , ( a , b ) -> a . first - b . first ) ; for ( int i = 0 ; i < data . size ( ) ; i ++ ) { if ( data . get ( i ) . second == ' x ' ) count ++ ; if ( data . get ( i ) . second == ' y ' ) count -- ; ans = Math . max ( ans , count ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] [ ] v = { { 1 , 2 } , { 2 , 4 } , { 3 , 6 } } ; overlap ( v ) ; } }
Making three numbers equal with the given operations | Java implementation of the approach ; Function that returns true if a , b and c can be made equal with the given operations ; Sort the three numbers ; Find the sum of difference of the 3 rd and 2 nd element and the 3 rd and 1 st element ; Subtract the difference from k ; Check the required condition ; Driver code
import java . util . * ; class GFG { static boolean canBeEqual ( int a , int b , int c , int k ) { int [ ] arr = new int [ 3 ] ; arr [ 0 ] = a ; arr [ 1 ] = b ; arr [ 2 ] = c ; Arrays . sort ( arr ) ; int diff = 2 * arr [ 2 ] - arr [ 1 ] - arr [ 0 ] ; k = k - diff ; if ( k < 0 k % 3 != 0 ) return false ; return true ; } public static void main ( String [ ] args ) { int a1 = 6 , b1 = 3 , c1 = 2 , k1 = 7 ; if ( canBeEqual ( a1 , b1 , c1 , k1 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Arrange numbers to form a valid sequence | Java implementation of the above approach ; Function to organize the given numbers to form a valid sequence . ; Sorting the array ; Two pointer technique to organize the numbers ; Driver code
import java . util . * ; class GFG { static int [ ] orgazineInOrder ( int [ ] vec , int [ ] op , int n ) { int [ ] result = new int [ n ] ; Arrays . sort ( vec ) ; int i = 0 , j = n - 1 , k = 0 ; while ( i <= j && k <= n - 2 ) { if ( op [ k ] == ' < ' ) { result [ k ] = vec [ i ++ ] ; } else { result [ k ] = vec [ j -- ] ; } k ++ ; } result [ n - 1 ] = vec [ i ] ; return result ; } public static void main ( String [ ] args ) { int [ ] vec = { 8 , 2 , 7 , 1 , 5 , 9 } ; int [ ] op = { ' > ' , ' > ' , ' < ' , ' > ' , ' < ' } ; int [ ] result = orgazineInOrder ( vec , op , vec . length ) ; for ( int i = 0 ; i < result . length ; i ++ ) { System . out . print ( result [ i ] + " ▁ " ) ; } } }
Minimize the sum of differences of consecutive elements after removing exactly K elements | Java implementation of the above approach . ; function to find minimum sum ; variable to store final answer and initialising it with the values when 0 elements is removed from the left and K from the right . ; loop to simulate removal of elements ; removing i elements from the left and and K - i elements from the right and updating the answer correspondingly ; returning final answer ; Driver function ; input values ; callin the required function ;
class GFG { static int findSum ( int [ ] arr , int n , int k ) { int ans = arr [ n - k - 1 ] - arr [ 0 ] ; for ( int i = 1 ; i <= k ; i ++ ) { ans = Math . min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . length ; System . out . println ( findSum ( arr , n , k ) ) ; } }
Minimum elements to be removed from the ends to make the array sorted | Java implementation of the approach ; Function to return the minimum number of elements to be removed from the ends of the array to make it sorted ; To store the final answer ; Two pointer loop ; While the array is increasing increment j ; Updating the ans ; Updating the left pointer ; Returning the final answer ; Driver code
class GFG { static int findMin ( int arr [ ] , int n ) { int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; ans = Math . max ( ans , j - i ) ; i = j - 1 ; } return n - ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 } ; int n = arr . length ; System . out . println ( findMin ( arr , n ) ) ; } }
Number of subsequences of maximum length K containing no repeated elements | Java implementation of the approach ; Returns number of subsequences of maximum length k and contains no repeated element ; Sort the array a [ ] ; Store the frequencies of all the distinct element in the vector arr ; count is the the number of such subsequences ; Create a 2 - d array dp [ n + 1 ] [ m + 1 ] to store the intermediate result ; Initialize the first row to 1 ; Update the dp [ ] [ ] array based on the recurrence relation ; Return the number of subsequences ; Driver code
import java . util . * ; class GFG { static int countSubSeq ( int a [ ] , int n , int k ) { Arrays . sort ( a ) ; List < Integer > arr = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; ) { int count = 1 , x = a [ i ] ; i ++ ; while ( i < n && a [ i ] == x ) { count ++ ; i ++ ; } arr . add ( count ) ; } int m = arr . size ( ) ; n = Math . min ( m , k ) ; int count = 1 ; int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m ; j >= 0 ; j -- ) { if ( j > m - i ) dp [ i ] [ j ] = 0 ; else { dp [ i ] [ j ] = dp [ i ] [ j + 1 ] + arr . get ( j ) * dp [ i - 1 ] [ j + 1 ] ; } } count = count + dp [ i ] [ 0 ] ; } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 2 , 3 , 3 , 5 } ; int n = a . length ; int k = 3 ; System . out . println ( countSubSeq ( a , n , k ) ) ; } }
Find the count of unvisited indices in an infinite array | Java implementation of the approach ; Function to return the count of unvisited indices starting from the index 0 ; Largest index that cannot be visited ; Push the index to the queue ; To store the required count ; Current index that cannot be visited ; Increment the count for the current index ; ( curr - m ) and ( curr - n ) are also unreachable if they are valid indices ; Return the required count ; Driver code
import java . util . LinkedList ; import java . util . Queue ; class GFG { public static int countUnvisited ( int n , int m ) { int X = ( m * n ) - m - n ; Queue < Integer > queue = new LinkedList < > ( ) ; queue . add ( X ) ; int count = 0 ; while ( ! queue . isEmpty ( ) ) { int curr = queue . poll ( ) ; count ++ ; if ( curr - m > 0 ) queue . add ( curr - m ) ; if ( curr - n > 0 ) queue . add ( curr - n ) ; } return count ; } public static void main ( String args [ ] ) { int n = 2 , m = 5 ; System . out . print ( countUnvisited ( n , m ) ) ; } }
Check whether the string S1 can be made equal to S2 with the given operation | Java implementation of the approach ; Function to return the string formed by the odd indexed characters of s ; Function to return the string formed by the even indexed characters of s ; Function that returns true if s1 can be made equal to s2 with the given operation ; Get the string formed by the even indexed characters of s1 ; Get the string formed by the even indexed characters of s2 ; Get the string formed by the odd indexed characters of s1 ; Get the string formed by the odd indexed characters of s2 ; Sorting all the lists ; If the strings can be made equal ; Driver Code
import java . util . * ; class GFG { static String partOdd ( String s ) { String st = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 != 0 ) st += s . charAt ( i ) ; } return st ; } static String partEven ( String str ) { String s = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( i % 2 == 0 ) s += str . charAt ( i ) ; } return s ; } static boolean canBeMadeEqual ( String s1 , String s2 ) { char [ ] even1 = partEven ( s1 ) . toCharArray ( ) ; char [ ] even2 = partEven ( s2 ) . toCharArray ( ) ; char [ ] odd1 = partOdd ( s1 ) . toCharArray ( ) ; char [ ] odd2 = partOdd ( s2 ) . toCharArray ( ) ; Arrays . sort ( even1 ) ; Arrays . sort ( even2 ) ; Arrays . sort ( odd1 ) ; Arrays . sort ( odd2 ) ; String even_s1 = new String ( even1 ) ; String even_s2 = new String ( even2 ) ; String odd_s1 = new String ( odd1 ) ; String odd_s2 = new String ( odd2 ) ; if ( even_s1 . equals ( even_s2 ) && odd_s1 . equals ( odd_s2 ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s1 = " cdab " ; String s2 = " abcd " ; if ( canBeMadeEqual ( s1 , s2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Sort the given stack elements based on their modulo with K | Java implementation of the above approach ; Function to sort the stack using another stack based on the values of elements modulo k ; Pop out the first element ; While temporary stack is not empty ; The top of the stack modulo k is greater than ( temp & k ) or if they are equal then compare the values ; Pop from temporary stack and push it to the input stack ; Push temp in temporary of stack ; Push all the elements in the original stack to get the ascending order ; Print the sorted elements ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void sortStack ( Stack < Integer > input , int k ) { Stack < Integer > tmpStack = new Stack < Integer > ( ) ; while ( ! input . isEmpty ( ) ) { int tmp = input . peek ( ) ; input . pop ( ) ; while ( ! tmpStack . isEmpty ( ) ) { int tmpStackMod = tmpStack . peek ( ) % k ; int tmpMod = tmp % k ; if ( ( tmpStackMod > tmpMod ) || ( tmpStackMod == tmpMod && tmpStack . peek ( ) > tmp ) ) { input . push ( tmpStack . peek ( ) ) ; tmpStack . pop ( ) ; } else break ; } tmpStack . push ( tmp ) ; } while ( ! tmpStack . isEmpty ( ) ) { input . push ( tmpStack . peek ( ) ) ; tmpStack . pop ( ) ; } while ( ! input . empty ( ) ) { System . out . print ( input . peek ( ) + " ▁ " ) ; input . pop ( ) ; } } public static void main ( String args [ ] ) { Stack < Integer > input = new Stack < Integer > ( ) ; input . push ( 10 ) ; input . push ( 3 ) ; input . push ( 2 ) ; input . push ( 6 ) ; input . push ( 12 ) ; int k = 4 ; sortStack ( input , k ) ; } }
Longest sub | Java implementation of the approach ; Function to return the length of the largest subsequence with non - negative sum ; To store the current sum ; Sort the input array in non - increasing order ; Traverse through the array ; Add the current element to the sum ; Condition when c_sum falls below zero ; Complete array has a non - negative sum ; Driver code
import java . util . * ; class GFG { static int maxLen ( int [ ] arr , int n ) { int c_sum = 0 ; Arrays . sort ( arr ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { c_sum += arr [ i ] ; if ( c_sum < 0 ) return i ; } return n ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , - 6 } ; int n = arr . length ; System . out . println ( maxLen ( arr , n ) ) ; } }