Array Parity Transformation
Last Updated :
31 Jan, 2024
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):
- Select two distinct indices let’s say X and Y and increment A[X] and decrement A[Y].
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:
- The remainder obtained by dividing an element by 2 at ith index let’s say (A[i]%2), must be the same before the operation and after applying all the operations.
- All the N/2 odd elements in the final array should be equal and all the N/2 even elements in the final array should be equal.
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.
- Let n be the number of odd or even elements i.e N/2 .
- Considering odd number be O and even number be E , we can say that n*O + n*E = Sum i.e. n*(O+E) = Sum.Hence, O+E = Sum/n.
- By looking at this formula, we can deduce two things:
- O+E should be an integer.
- Sum/n should be odd as sum of O and E must be odd.
- If both conditions are satisfying, then answer is YES , else NO .
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void checkParity( int A[], int N)
{
int sum = 0;
for ( int i = 0; i < N; i++)
sum += A[i];
int n = N / 2;
if (sum % n == 0 && ((sum / n) & 1)) {
cout << "YES\n" ;
}
else
cout << "NO\n" ;
}
int main()
{
int A[] = { 1, 2, 5, 6 };
int N = sizeof (A) / sizeof (A[0]);
checkParity(A, N);
return 0;
}
|
Java
public class ParityChecker {
static void checkParity( int [] A, int N) {
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
sum += A[i];
int n = N / 2 ;
if (sum % n == 0 && ((sum / n) & 1 ) == 1 ) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
public static void main(String[] args) {
int [] A = { 1 , 2 , 5 , 6 };
int N = A.length;
checkParity(A, N);
}
}
|
Python
def check_parity(A):
total_sum = sum (A)
n = len (A)
if total_sum % (n / / 2 ) = = 0 and ((total_sum / / (n / / 2 )) & 1 ):
print ( "YES" )
else :
print ( "NO" )
if __name__ = = "__main__" :
A = [ 1 , 2 , 5 , 6 ]
check_parity(A)
|
C#
using System;
class Program {
static void CheckParity( int [] A, int N)
{
int sum = 0;
for ( int i = 0; i < N; i++)
sum += A[i];
int n = N / 2;
if (sum % n == 0 && ((sum / n) & 1) == 1) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
static void Main()
{
int [] A = { 1, 2, 5, 6 };
int N = A.Length;
CheckParity(A, N);
Console
.ReadLine();
}
}
|
Javascript
function checkParity(A, N) {
let sum = 0;
for (let i = 0; i < N; i++) {
sum += A[i];
}
let n = Math.floor(N / 2);
if (sum % n === 0 && ((sum / n) & 1)) {
console.log( "YES" );
}
else {
console.log( "NO" );
}
}
let A = [1, 2, 5, 6];
let N = A.length;
checkParity(A, N);
|
Time Complexity: O(N)
Auxiliary Space: O(1),where N is the size of the array.
Share your thoughts in the comments
Please Login to comment...