Fill rest of the elements with 1
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 ] + " " ) ; } }