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.
Input: N = 8
Output: 2 4 6 8 1 3 5 11
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
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:
2 4 6 8 1 3 5 11
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Generate an array of given size with equal count and sum of odd and even numbers
- Even numbers at even index and odd numbers at odd index
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count numbers from given range having odd digits at odd places and even digits at even places
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Construct array with sum of product of same indexed elements in the given array equal to zero
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Minimize adding odd and subtracting even numbers to make all array elements equal to K
- Construct a distinct elements array with given size, sum and element upper bound
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Number of permutations such that sum of elements at odd index and even index are equal
- Count subarrays having sum of elements at even and odd positions equal
- Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal
- Length of longest Subarray with equal number of odd and even elements
- Construct a matrix with sum equal to the sum of diagonal elements
- Count rotations of N which are Odd and Even
- Rearrange array by interchanging positions of even and odd elements in the given array
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.