Generate array with minimum sum which can be deleted in P steps | Function to find the required array ; calculating minimum possible sum ; Array ; place first P natural elements ; Fill rest of the elements with 1 ; Driver Code
function findArray ( N , P ) { let ans = parseInt ( ( P * ( P + 1 ) ) / 2 , 10 ) + ( N - P ) ; let arr = new Array ( N + 1 ) ; for ( let i = 1 ; i <= P ; i ++ ) { arr [ i ] = i ; } for ( let i = P + 1 ; i <= N ; i ++ ) { arr [ i ] = 1 ; } document . write ( " " + ans + " " ) ; document . write ( " " + " " ) ; for ( let i = 1 ; i <= N ; i ++ ) { document . write ( arr [ i ] + " " ) ; } } let N = 5 , P = 3 ; findArray ( N , P ) ;