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
def findSolution ( N ) : NEW_LINE INDENT if ( N % 4 != 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT temp = 0 NEW_LINE sum_odd = 0 NEW_LINE sum_even = 0 NEW_LINE result = [ 0 ] * N NEW_LINE for i in range ( 0 , N , 2 ) : NEW_LINE INDENT temp += 2 NEW_LINE result [ i + 1 ] = temp NEW_LINE sum_even += result [ i + 1 ] NEW_LINE result [ i ] = temp - 1 NEW_LINE sum_odd += result [ i ] NEW_LINE DEDENT diff = sum_even - sum_odd NEW_LINE result [ N - 2 ] += diff NEW_LINE for i in range ( N ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT N = 8 ; NEW_LINE findSolution ( N ) NEW_LINE