Construct an Array of size N in which sum of odd elements is equal to sum of even elements | C ++ program to Create an array of size N consisting of distinct elements where 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void arrayConstruct ( int N ) { for ( int i = 2 ; i <= N ; i = i + 2 ) cout << i << " ▁ " ; for ( int i = 1 ; i < N - 1 ; i = i + 2 ) cout << i << " ▁ " ; cout << N - 1 + ( N / 2 ) << endl ; } void createArray ( int N ) { if ( N % 4 == 0 ) arrayConstruct ( N ) ; else cout << -1 << endl ; } int main ( ) { int N = 8 ; createArray ( N ) ; return 0 ; }