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
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Construct a distinct elements array with given size, sum and element upper bound
- Find array elements equal to sum of any subarray of at least size 2
- Count of Array elements to be divided by 2 to make at least K elements equal
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Construct sum-array with sum of elements in given range
- Construct an array from GCDs of consecutive elements in given array
- Construct an array from XOR of all elements of array except element at same index
- Print all possible combinations of r elements in a given array of size n
- Given an array of size n and a number k, find all elements that appear more than n/k times
- Find array using different XORs of elements in groups of size 4
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- XOR of all elements of array with set bits equal to K
- Check if Sum and XOR of all elements of array is equal
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Count elements such that there are exactly X elements with values greater than or equal to X
- Minimum cost of choosing 3 increasing elements in an array of size N
- Minimum distance between any two equal elements in an Array
- Make all elements of an array equal with the given operation
- For each element in 1st array count elements less than or equal to it in 2nd 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 firstname.lastname@example.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.