Largest value of K that a set of all possible subset | Java program for the above approach ; Function to find maximum count of consecutive integers from 0 in set S of all possible subset - sum ; Stores the maximum possible integer ; Sort the given array in non - decrasing order ; Iterate the given array ; If arr [ i ] <= X + 1 , then update X otherwise break the loop ; Return Answer ; Driver Code
import java . util . Arrays ; class GFG { public static int maxConsecutiveCnt ( int [ ] arr ) { int X = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] <= ( X + 1 ) ) { X = X + arr [ i ] ; } else { break ; } } return X + 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 1 , 4 } ; System . out . println ( maxConsecutiveCnt ( arr ) ) ; } }