Order of indices which is lexicographically smallest and sum of elements is <= X | Java implementation of the approach ; Function to return the chosen indices ; Maximum indices chosen ; Try to replace the first element in ans by i , making the order lexicographically smaller ; If no further replacement possible return ; If found an index smaller than ind and sum not exceeding X then remove index of smallest value from ans and then add index i to ans ; Driver code ; Print the chosen indices
import java . util . * ; class GFG { static Vector < Integer > solve ( int X , Vector < Integer > A ) { int min = Integer . MAX_VALUE ; int ind = - 1 ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { if ( A . get ( i ) < min ) { min = A . get ( i ) ; ind = i ; } } int maxIndChosen = X / min ; Vector < Integer > ans = new Vector < > ( ) ; if ( maxIndChosen == 0 ) { return ans ; } for ( int i = 0 ; i < maxIndChosen ; i ++ ) { ans . add ( ind ) ; } int temp = maxIndChosen ; int sum = maxIndChosen * A . get ( ind ) ; for ( int i = 0 ; i < ind ; i ++ ) { if ( sum - X == 0 temp == 0 ) break ; while ( ( sum - A . get ( ind ) + A . get ( i ) ) <= X && temp != 0 ) { ans . remove ( 0 ) ; ans . add ( i ) ; temp -- ; sum += ( A . get ( i ) - A . get ( ind ) ) ; } } Collections . sort ( ans ) ; return ans ; } public static void main ( String args [ ] ) { Integer arr [ ] = { 5 , 6 , 4 , 8 } ; Vector < Integer > A = new Vector < Integer > ( Arrays . asList ( arr ) ) ; int X = 18 ; Vector < Integer > ans = solve ( X , A ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } }