Create an array of size N with sum S such that no subarray exists with sum S or S | Java for the above approach ; Function to create an array with N elements with sum as S such that the given conditions satisfy ; Check if the solution exists ; Print the array as print ( n - 1 ) elements of array as 2 ; Print the last element of the array ; Print the value of k ; If solution doesnot exists ; Driver Code ; Given N and sum S ; Function call
class GFG { static void createArray ( int n , int s ) { if ( 2 * n <= s ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { System . out . print ( 2 + " ▁ " ) ; s -= 2 ; } System . out . println ( s ) ; System . out . println ( 1 ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int N = 1 ; int S = 4 ; createArray ( N , S ) ; } }