Generate an array of given size with equal count and sum of odd and even numbers | Function to find the array such that the array contains the same count of even and odd elements with equal sum of even and odd elements ; Length of array which is not divisible by 4 is unable to form such array ; Loop to find the resulted array containing the same count of even and odd elements ; Find the total sum of even elements ; Find the total sum of odd elements ; Find the difference between the total sum of even and odd elements ; The difference will be added in the last odd element ; Driver Code
function findSolution ( N ) { if ( N % 4 != 0 ) document . write ( - 1 + " " ) ; else { let temp = 0 , sum_odd = 0 , sum_even = 0 ; let result = new Uint8Array ( N ) ; for ( let i = 0 ; i < N ; i += 2 ) { temp += 2 ; result [ i + 1 ] = temp ; sum_even += result [ i + 1 ] ; result [ i ] = temp - 1 ; sum_odd += result [ i ] ; } let diff = sum_even - sum_odd ; result [ N - 2 ] += diff ; for ( let i = 0 ; i < N ; i ++ ) document . write ( result [ i ] + " " ) ; document . write ( " " ) ; } } let N = 8 ; findSolution ( N ) ;