Construct an Array of size N in which sum of odd elements is equal to sum of even elements | Function to construct the required array ; To construct first half , distinct even numbers ; To construct second half , distinct odd numbers ; Calculate the last number of second half so as to make both the halves equal ; Function to construct the required array ; check if size is multiple of 4 then array exist ; function call to construct array ; Driver code
function arrayConstruct ( N ) { for ( let i = 2 ; i <= N ; i = i + 2 ) document . write ( i + " " ) ; for ( let i = 1 ; i < N - 1 ; i = i + 2 ) document . write ( i + " " ) ; document . write ( N - 1 + ( N / 2 ) + " " ) ; } function createArray ( N ) { if ( N % 4 == 0 ) arrayConstruct ( N ) ; else document . write ( - 1 + " " ) ; } let N = 8 ; createArray ( N ) ;