Given an **integer N** which is always an even number, our task is to print **N** distinct numbers following the given conditions:

- The first half numbers are even whereas the other half numbers are odd
- Sum of the elements of first half numbers and sum of elements of second half numbers should be equal

Print the array if the above conditions satisfy otherwise output “-1”.

**Examples:**

Input:N = 4

Output:2 4 1 5

Explanation:

Given number 4 we are required to print 4 numbers. First Half = 2, 4 and their sum is 6, other half = 1, 5 and their sum is also 6.

Input:N = 22

Output:-1

Explanation:

It is not possible to print the required array.

**Approach:**

To solve the problem mentioned above we have to observe that the **integer N has to be a multiple of 4**.

- We know that the
**sum of the first N/2 even numbers will be even**, so if the sum of the other N/2 integers is also even then N/2 must be even, because the**sum of an odd number of odd integers is always odd.** - If N/2 is even then N is a multiple of 4, so if n is not divisible by 4 then the answer is “-1”, otherwise, there will be a possible array.
- For printing the array we will consider two parts such that the first half that is N/2 elements will be simply multiples of 2 and the other half will be multiple of 2 – 1. For the last element in the array, we will calculate the integer by applying the direct formula N + N / 2 – 1 because we are supposed to make the sum for two halves equal.

Below is the implementation of the above approach:

## C++

`// C++ implementation to Print N distinct numbers ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the required array ` `bool` `printArr(` `int` `n) ` `{ ` ` ` ` ` `// Check if number is a multiple of 4 ` ` ` `if` `(n % 4 == 0) { ` ` ` `// Printing Left Half of the array ` ` ` `for` `(` `int` `i = 1; i <= n / 2; i++) ` ` ` `cout << i * 2 << ` `' '` `; ` ` ` ` ` `// Printing Right Half of the array ` ` ` `for` `(` `int` `i = 1; i < n / 2; i++) ` ` ` `cout << i * 2 - 1 << ` `' '` `; ` ` ` `cout << n + n / 2 - 1 << ` `'\n'` `; ` ` ` `} ` ` ` ` ` `else` ` ` `cout << ` `"-1"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 22; ` ` ` ` ` `printArr(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to print N distinct numbers ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to print the required array ` `static` `void` `printArr(` `int` `n) ` `{ ` ` ` ` ` `// Check if number is a multiple of 4 ` ` ` `if` `(n % ` `4` `== ` `0` `) ` ` ` `{ ` ` ` `// Printing left half of the array ` ` ` `for` `(` `int` `i = ` `1` `; i <= n / ` `2` `; i++) ` ` ` `System.out.print(i * ` `2` `+ ` `" "` `); ` ` ` ` ` `// Printing Right Half of the array ` ` ` `for` `(` `int` `i = ` `1` `; i < n / ` `2` `; i++) ` ` ` `System.out.print(i * ` `2` `- ` `1` `+ ` `" "` `); ` ` ` ` ` `System.out.println(n + n / ` `2` `- ` `1` `); ` ` ` `} ` ` ` `else` ` ` `System.out.print(` `"-1"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `22` `; ` ` ` `printArr(n); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to print ` `# N distinct numbers ` ` ` `# Function to print the required array ` `def` `printArr(n): ` ` ` ` ` `# Check if number is a multiple of 4 ` ` ` `if` `(n ` `%` `4` `=` `=` `0` `): ` ` ` ` ` `# Printing Left Half of the array ` ` ` `for` `i ` `in` `range` `(` `1` `, (n ` `/` `2` `) ` `+` `1` `): ` ` ` `print` `(i ` `*` `2` `, end ` `=` `" "` `) ` ` ` ` ` `# Printing Right Half of the array ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `/` `2` `): ` ` ` `print` `(i ` `*` `2` `-` `1` `, end ` `=` `" "` `) ` ` ` ` ` `print` `(n ` `+` `n ` `/` `2` `-` `1` `, end ` `=` `"\n"` `) ` ` ` ` ` `else` `: ` ` ` `print` `(` `"-1"` `) ` ` ` `# Driver code ` `n ` `=` `22` `printArr(n) ` ` ` `# This code is contributed by PratikBasu ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to print N distinct numbers ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `// Function to print the required array ` `static` `void` `printArr(` `int` `n) ` `{ ` ` ` ` ` `// Check if number is a multiple of 4 ` ` ` `if` `(n % 4 == 0) ` ` ` `{ ` ` ` ` ` `// Printing left half of the array ` ` ` `for` `(` `int` `i = 1; i <= n / 2; i++) ` ` ` `Console.Write(i * 2 + ` `" "` `); ` ` ` ` ` `// Printing Right Half of the array ` ` ` `for` `(` `int` `i = 1; i < n / 2; i++) ` ` ` `Console.Write(i * 2 - 1 + ` `" "` `); ` ` ` ` ` `Console.WriteLine(n + n / 2 - 1); ` ` ` `} ` ` ` ` ` `else` ` ` `Console.Write(` `"-1"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 22; ` ` ` `printArr(n); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

-1

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Generate elements of the array following given conditions
- Permute the elements of an array following given order
- Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
- Print combinations of distinct numbers which add up to give sum N
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Minimum operations required to make all the elements distinct in an array
- Print all distinct permutations of a given string with duplicates
- Print All Distinct Elements of a given integer array
- Print a number strictly less than a given number such that all its digits are distinct.
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Numbers less than N which are product of exactly two distinct prime numbers
- Find two distinct prime numbers with given product
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Find two distinct numbers such that their LCM lies in given range
- Maximum count of equal numbers in an array after performing given operations
- Making three numbers equal with the given operations
- Print sorted distinct elements of array
- Print all distinct Coprime sets possible from 1 to N
- Print modified array after multiple array range increment operations

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.