Given an integer **N**, the task is to find an array of length **N** that contains same count of odd and even elements with an equal sum of even and odd elements in the array.

**Note:** Print -1 if no such array is possible.

**Examples:**

Input:N = 4

Output:1 2 5 4

Explanation:

Even elements of the array – {2, 4}, S(even) = 6

Odd elements of the array – {1, 5}, S(odd) = 6

Input:N = 6

Output:-1

Explanation:

There are no such array which contains 3 even elements and 3 odd elements with equal sum.

**Approach:** The key observation in the problem is that only the length of array which is multiple of **4** can form array with equal number of even and odd elements with equal sum. Below is the illustration of the steps:

- Even elements of the array is the first
**N/2**even elements of the natural numbers starting from 2. - Similarly,
**(N/2 – 1)**odd elements of the array is the first**(N/2 – 1)**odd elements of the natural numbers starting from 1. - The Last odd element of the array is the required value to make the sum of the even and odd elements of the array equal.
Last Odd Element = (sum of even elements) - (sum of N/2 - 1 odd elements)

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// array containing same count of ` `// even and odd elements with equal ` `// sum of even and odd elements ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// 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 ` `void` `findSolution(` `int` `N) ` `{ ` ` ` ` ` `// Length of array which is not ` ` ` `// divisible by 4 is unable to ` ` ` `// form such array ` ` ` `if` `(N % 4 != 0) ` ` ` `cout << -1 << ` `"\n"` `; ` ` ` `else` `{ ` ` ` `int` `temp = 0, sum_odd = 0, ` ` ` `sum_even = 0; ` ` ` `int` `result[N] = { 0 }; ` ` ` ` ` `// Loop to find the resulted ` ` ` `// array containing the same ` ` ` `// count of even and odd elements ` ` ` `for` `(` `int` `i = 0; i < N; i += 2) { ` ` ` `temp += 2; ` ` ` ` ` `result[i + 1] = temp; ` ` ` `// Find the total sum ` ` ` `// of even elements ` ` ` `sum_even += result[i + 1]; ` ` ` ` ` `result[i] = temp - 1; ` ` ` `// Find the total sum ` ` ` `// of odd elements ` ` ` `sum_odd += result[i]; ` ` ` `} ` ` ` ` ` `// Find the difference between the ` ` ` `// total sum of even and odd elements ` ` ` `int` `diff = sum_even - sum_odd; ` ` ` ` ` `// The difference will be added ` ` ` `// in the last odd element ` ` ` `result[N - 2] += diff; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `cout << result[i] << ` `" "` `; ` ` ` `cout << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 8; ` ` ` `findSolution(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// array containing same count of ` `// even and odd elements with equal ` `// sum of even and odd elements ` ` ` `class` `GFG{ ` ` ` `// 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 ` `static` `void` `findSolution(` `int` `N) ` `{ ` ` ` ` ` `// Length of array which is not ` ` ` `// divisible by 4 is unable to ` ` ` `// form such array ` ` ` `if` `(N % ` `4` `!= ` `0` `) ` ` ` `System.out.print(-` `1` `+ ` `"\n"` `); ` ` ` ` ` `else` ` ` `{ ` ` ` `int` `temp = ` `0` `, sum_odd = ` `0` `; ` ` ` `int` `sum_even = ` `0` `; ` ` ` `int` `result[] = ` `new` `int` `[N]; ` ` ` ` ` `// Loop to find the resulted ` ` ` `// array containing the same ` ` ` `// count of even and odd elements ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i += ` `2` `) ` ` ` `{ ` ` ` `temp += ` `2` `; ` ` ` `result[i + ` `1` `] = temp; ` ` ` ` ` `// Find the total sum ` ` ` `// of even elements ` ` ` `sum_even += result[i + ` `1` `]; ` ` ` `result[i] = temp - ` `1` `; ` ` ` ` ` `// Find the total sum ` ` ` `// of odd elements ` ` ` `sum_odd += result[i]; ` ` ` `} ` ` ` ` ` `// Find the difference between the ` ` ` `// total sum of even and odd elements ` ` ` `int` `diff = sum_even - sum_odd; ` ` ` ` ` `// The difference will be added ` ` ` `// in the last odd element ` ` ` `result[N - ` `2` `] += diff; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `System.out.print(result[i] + ` `" "` `); ` ` ` `System.out.print(` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `8` `; ` ` ` `findSolution(N); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# array containing same count of ` `# even and odd elements with equal ` `# sum of even and odd elements ` ` ` `# 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 ` `def` `findSolution(N): ` ` ` ` ` `# Length of array which is not ` ` ` `# divisible by 4 is unable to ` ` ` `# form such array ` ` ` `if` `(N ` `%` `4` `!` `=` `0` `): ` ` ` `print` `(` `-` `1` `) ` ` ` `else` `: ` ` ` `temp ` `=` `0` ` ` `sum_odd ` `=` `0` ` ` `sum_even ` `=` `0` ` ` `result ` `=` `[` `0` `] ` `*` `N ` ` ` ` ` `# Loop to find the resulted ` ` ` `# array containing the same ` ` ` `# count of even and odd elements ` ` ` `for` `i ` `in` `range` `(` `0` `, N, ` `2` `): ` ` ` `temp ` `+` `=` `2` ` ` `result[i ` `+` `1` `] ` `=` `temp ` ` ` ` ` `# Find the total sum ` ` ` `# of even elements ` ` ` `sum_even ` `+` `=` `result[i ` `+` `1` `] ` ` ` `result[i] ` `=` `temp ` `-` `1` ` ` ` ` `# Find the total sum ` ` ` `# of odd elements ` ` ` `sum_odd ` `+` `=` `result[i] ` ` ` ` ` `# Find the difference between the ` ` ` `# total sum of even and odd elements ` ` ` `diff ` `=` `sum_even ` `-` `sum_odd ` ` ` ` ` `# The difference will be added ` ` ` `# in the last odd element ` ` ` `result[N ` `-` `2` `] ` `+` `=` `diff ` ` ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `print` `(result[i], end ` `=` `" "` `) ` ` ` `print` `() ` ` ` `# Driver Code ` `N ` `=` `8` `; ` `findSolution(N) ` ` ` `# This code is contributed by divyamohan123 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// array containing same count of ` `// even and odd elements with equal ` `// sum of even and odd elements ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// 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 ` `static` `void` `findSolution(` `int` `N) ` `{ ` ` ` ` ` `// Length of array which is not ` ` ` `// divisible by 4 is unable to ` ` ` `// form such array ` ` ` `if` `(N % 4 != 0) ` ` ` `Console.Write(-1 + ` `"\n"` `); ` ` ` ` ` `else` ` ` `{ ` ` ` `int` `temp = 0, sum_odd = 0; ` ` ` `int` `sum_even = 0; ` ` ` `int` `[]result = ` `new` `int` `[N]; ` ` ` ` ` `// Loop to find the resulted ` ` ` `// array containing the same ` ` ` `// count of even and odd elements ` ` ` `for` `(` `int` `i = 0; i < N; i += 2) ` ` ` `{ ` ` ` `temp += 2; ` ` ` `result[i + 1] = temp; ` ` ` ` ` `// Find the total sum ` ` ` `// of even elements ` ` ` `sum_even += result[i + 1]; ` ` ` `result[i] = temp - 1; ` ` ` ` ` `// Find the total sum ` ` ` `// of odd elements ` ` ` `sum_odd += result[i]; ` ` ` `} ` ` ` ` ` `// Find the difference between the ` ` ` `// total sum of even and odd elements ` ` ` `int` `diff = sum_even - sum_odd; ` ` ` ` ` `// The difference will be added ` ` ` `// in the last odd element ` ` ` `result[N - 2] += diff; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `Console.Write(result[i] + ` `" "` `); ` ` ` `Console.Write(` `"\n"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 8; ` ` ` `findSolution(N); ` `} ` `} ` ` ` `// This code is contributed by Rohit_ranjan ` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 5 6 11 8

**Performance Analysis:**

**Time Complexity:***O(N)***Auxiliary Space:***O(1)*

## Recommended Posts:

- Count numbers from given range having odd digits at odd places and even digits at even places
- Check if a number has an odd count of odd divisors and even count of even divisors
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Check if count of even divisors of N is equal to count of odd divisors
- Count subarrays having sum of elements at even and odd positions equal
- Minimize adding odd and subtracting even numbers to make all array elements equal to K
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Check whether count of odd and even factors of a number are equal
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Count of odd and even sum pairs in an array
- Generate Array whose sum of all K-size subarrays divided by N leaves remainder X
- Generate an array of size K which satisfies the given conditions

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.