Construct longest possible sequence of unique elements with given LCM | Function to construct an array of unique elements whose LCM is N ; Stores array elements whose LCM is N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If N is divisible by i ; Insert i into newArr [ ] ; If N is not perfect square ; Sort the array newArr [ ] ; Print array elements ; Given N ; Function Call
function constructArrayWithGivenLCM ( N ) { let newArr = new Array ( N ) ; newArr . fill ( 0 ) ; let j = 0 ; for ( let i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { newArr [ j ] = i ; j ++ ; if ( parseInt ( N / i , 10 ) != i ) { newArr [ j ] = parseInt ( N / i , 10 ) ; j ++ ; } } } newArr . sort ( function ( a , b ) { return a - b } ) ; for ( let i = j ; i < N ; i ++ ) { document . write ( newArr [ i ] + " " ) ; } } let N = 12 ; constructArrayWithGivenLCM ( N ) ;