Generate an N | Fun dtion to print the required sequence ; Stores count of even and odd elements ; Stores sum of even and odd elements ; Print N / 2 even elements ; Print N / 2 - 1 odd elements ; Print final odd element ; Driver Code
function Print ( N ) { if ( ( N / 2 ) % 2 == 1 || ( N % 2 == 1 ) ) { document . write ( - 1 ) ; return ; } var CurEven = 2 , CurOdd = 1 ; var SumOdd = 0 , SumEven = 0 ; for ( var i = 0 ; i < ( N / 2 ) ; i ++ ) { document . write ( CurEven + " " ) ; SumEven += CurEven ; CurEven += 2 ; } for ( var i = 0 ; i < N / 2 - 1 ; i ++ ) { document . write ( CurOdd + " " ) ; SumOdd += CurOdd ; CurOdd += 2 ; } CurOdd = SumEven - SumOdd ; document . write ( CurOdd ) ; } var N = 12 ; Print ( N ) ;