![]() |
VOOZH | about |
Given an array A[] of length N (N is even && N>=2).Where half elements are odd and the other half are even. You can apply the below operation (possibly zero time):
Then your task is to output YES/NO by checking whether the following conditions can be satisfied or not by using the above operation any number of times:
Examples:
Input: A[] = {1, 2, 5 ,6}
Output: YES
Explanation: One of the possible sequence of operations is listed below:
Move 1: Select i=2, j=4.
A[2]= 3, A[4]= 5 to get {1, 3, 5, 5}Move 2: Select i=2, j=4.
A[2]= 4, A[4]= 4 to get {1, 4, 5, 4}Move 3: Select i=1, j=3.
A[1]= 2, A[3]= 4 to get {2, 4, 4, 4}Move 4: Select i=1, j=3.
A[1]= 3, A[3]= 3 to get {3, 4, 3, 4}Here, all the odd elements are equal and all the even elements are equal. Also, the parity at each index is preserved.
Input: A[] = {1, 1, 2, 4}
Output: NO
Explanation: It is not possible to satisfy all the given conditions using any number of operations.
Approach: To solve the problem follow the below steps.
The first observation of this problem is that sum of all elements will always remain same even after doing operations (because we are doing +1 and -1 at the same time.
Below is the implementation of the above approach:
YES
Time Complexity: O(N)
Auxiliary Space: O(1),where N is the size of the array.