Construct an Array of size N in which sum of odd elements is equal to sum of even elements
Last Updated :
17 Nov, 2021
Given an integer N which is always even, the task is to create an array of size N which contains N/2 even numbers and N/2 odd numbers. All the elements of array should be distinct and the sum of even numbers is equal to the sum of odd numbers. If no such array exists then print -1.
Examples:
Input: N = 8
Output: 2 4 6 8 1 3 5 11
Explanation:
The array has 8 distinct elements which have equal sum of odd and even numbers, i.e., (2 + 4 + 6 + 8 = 1 + 3 + 5 + 11).
Input: N = 10
Output: -1
Explanation:
It is not possible to construct array of size 10.
Approach: To solve the problem mentioned above the very first observation is that it is not possible to create an array that has size N which is a multiple of 2 but not multiple of 4. Because, if that happens then the sum of one half which contains odd numbers will always be odd and the sum of another half which contains even numbers will always be even, hence the sum of both halves can’t be the same.
Therefore, the array which satisfies the problem statement should always have a size N which is a multiple of 4. The approach is to first construct N/2 even numbers starting from 2, which is the first half of the array. Then create another part of the array starting from 1 and finally calculate the last odd element such that it makes both the halves equal. In order to do so the last element of odd numbers should be (N/2) – 1 + N.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
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;
}
|
Java
class GFG{
static void arrayConstruct( int N)
{
for ( int i = 2 ; i <= N; i = i + 2 )
System.out.print(i+ " " );
for ( int i = 1 ; i < N - 1 ; i = i + 2 )
System.out.print(i+ " " );
System.out.print(N - 1 + (N / 2 ) + "\n" );
}
static void createArray( int N)
{
if (N % 4 == 0 )
arrayConstruct(N);
else
System.out.print(- 1 + "\n" );
}
public static void main(String[] args)
{
int N = 8 ;
createArray(N);
}
}
|
Python3
def arrayConstruct(N):
for i in range ( 2 , N + 1 , 2 ):
print (i,end = " " )
for i in range ( 1 , N - 1 , 2 ):
print (i, end = " " )
print (N - 1 + (N / / 2 ))
def createArray(N):
if (N % 4 = = 0 ):
arrayConstruct(N)
else :
cout << - 1 << endl
if __name__ = = '__main__' :
N = 8
createArray(N)
|
C#
using System;
class GFG{
static void arrayConstruct( int N)
{
for ( int i = 2; i <= N; i = i + 2)
Console.Write(i + " " );
for ( int i = 1; i < N - 1; i = i + 2)
Console.Write(i + " " );
Console.Write(N - 1 + (N / 2) + "\n" );
}
static void createArray( int N)
{
if (N % 4 == 0)
arrayConstruct(N);
else
Console.Write(-1 + "\n" );
}
public static void Main(String[] args)
{
int N = 8;
createArray(N);
}
}
|
Javascript
<script>
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) + "<br>" );
}
function createArray(N)
{
if (N % 4 == 0)
arrayConstruct(N);
else
document.write(-1 + "<br>" );
}
let N = 8;
createArray(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...