Generate an N-length array having equal count and sum of elements of both parities
Given an integer N (3 ≤ N ≤ 105), the task is to generate an array of N distinct positive elements such that the count and sum of elements of both parties i.e even and odd, are the same. If it is not possible to construct such an array, print -1.
Examples:
Input: N = 8
Output: 2, 4, 6, 8, 1, 3, 5, 11
Input: 6
Output: -1
Explanation: For the count of odd and even array elements to be equal, 3 even and odd elements must be present in the array. But, sum of 3 odd elements will always be odd. Therefore, it is not possible to generate such an array.
Approach: Follow the steps below to solve the problem:
- If N is odd or (N / 2) is odd, then print -1.
- Otherwise:
- Print N/2 even values starting from 2 and store the count in some variable, say SumEven.
- Print N / 2 – 1 odd values starting from 1 and store the sum in another variable, say SumOdd.
- At last print SumEven – SumOdd which is also odd.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Print( int N)
{
if ((N / 2) % 2 == 1
|| (N % 2 == 1)) {
cout << -1 << endl;
return ;
}
int CurEven = 2, CurOdd = 1;
int SumOdd = 0, SumEven = 0;
for ( int i = 0; i < (N / 2); i++) {
cout << CurEven << " " ;
SumEven += CurEven;
CurEven += 2;
}
for ( int i = 0; i < N / 2 - 1; i++) {
cout << CurOdd << " " ;
SumOdd += CurOdd;
CurOdd += 2;
}
CurOdd = SumEven - SumOdd;
cout << CurOdd << '\n' ;
}
int main()
{
int N = 12;
Print(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void Print( int N)
{
if ((N / 2 ) % 2 == 1 || (N % 2 == 1 ))
{
System.out.print(- 1 );
return ;
}
int CurEven = 2 , CurOdd = 1 ;
int SumOdd = 0 , SumEven = 0 ;
for ( int i = 0 ; i < (N / 2 ); i++)
{
System.out.print(CurEven + " " );
SumEven += CurEven;
CurEven += 2 ;
}
for ( int i = 0 ; i < N / 2 - 1 ; i++)
{
System.out.print(CurOdd + " " );
SumOdd += CurOdd;
CurOdd += 2 ;
}
CurOdd = SumEven - SumOdd;
System.out.println(CurOdd);
}
public static void main (String[] args)
{
int N = 12 ;
Print(N);
}
}
|
Python3
def Print (N):
if ((N / 2 ) % 2 or (N % 2 )):
print ( - 1 )
return
CurEven = 2
CurOdd = 1
SumOdd = 0
SumEven = 0
for i in range (N / / 2 ):
print (CurEven,end = " " )
SumEven + = CurEven
CurEven + = 2
for i in range ( (N / / 2 ) - 1 ):
print (CurOdd, end = " " )
SumOdd + = CurOdd
CurOdd + = 2
CurOdd = SumEven - SumOdd
print (CurOdd)
N = 12
Print (N)
|
C#
using System;
class GFG
{
static void Print( int N)
{
if ((N / 2) % 2 == 1 || (N % 2 == 1))
{
Console.WriteLine(-1);
return ;
}
int CurEven = 2, CurOdd = 1;
int SumOdd = 0, SumEven = 0;
for ( int i = 0; i < (N / 2); i++)
{
Console.Write(CurEven + " " );
SumEven += CurEven;
CurEven += 2;
}
for ( int i = 0; i < N / 2 - 1; i++)
{
Console.Write(CurOdd + " " );
SumOdd += CurOdd;
CurOdd += 2;
}
CurOdd = SumEven - SumOdd;
Console.WriteLine(CurOdd);
}
public static void Main()
{
int N = 12;
Print(N);
}
}
|
Javascript
<script>
function Print(N)
{
if ((N / 2) % 2 == 1 || (N % 2 == 1))
{
document.write(-1);
return ;
}
var CurEven = 2, CurOdd = 1;
var SumOdd = 0, SumEven = 0;
for ( var i = 0; i < (N / 2); i++)
{
document.write(CurEven + " " );
SumEven += CurEven;
CurEven += 2;
}
for ( var i = 0; i < N / 2 - 1; i++)
{
document.write(CurOdd + " " );
SumOdd += CurOdd;
CurOdd += 2;
}
CurOdd = SumEven - SumOdd;
document.write(CurOdd);
}
var N = 12;
Print(N);
</script>
|
Output:
2 4 6 8 10 12 1 3 5 7 9 17
Time Complexity: O(N)
Auxiliary Space O(1)
Last Updated :
28 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...