Open In App

Array Parity Transformation

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 {
 
    // Method to check if the sum of elements in the array is odd and
    // divisible by half of the array length
    static void checkParity(int[] A, int N) {
        // Variable to store the sum of array elements
        int sum = 0;
 
        // Calculate the sum of array elements
        for (int i = 0; i < N; i++)
            sum += A[i];
 
        // Calculate half of the array length
        int n = N / 2;
 
        // Check if the sum is odd and divisible by half of the array length
        if (sum % n == 0 && ((sum / n) & 1) == 1) {
            System.out.println("YES"); // Print "YES" if the condition is true
        } else {
            System.out.println("NO");  // Print "NO" if the condition is false
        }
    }
 
    public static void main(String[] args) {
        // Sample array
        int[] A = {1, 2, 5, 6};
 
        // Get the length of the array
        int N = A.length;
 
        // Call the checkParity method to check and print the result
        checkParity(A, N);
    }
}


Python




def check_parity(A):
    # Calculate the sum of elements in the array
    total_sum = sum(A)
 
    # Calculate the length of the array
    n = len(A)
 
    # Check if the sum is divisible by half of the array length and the result is odd
    if total_sum % (n // 2) == 0 and ((total_sum // (n // 2)) & 1):
        print("YES")
    else:
        print("NO")
 
 
# Main function
if __name__ == "__main__":
    # Given array
    A = [1, 2, 5, 6]
 
    # Call the function to check parity
    check_parity(A)


C#




using System;
 
class Program {
    // Function to check the parity of the sum of array
    // elements
    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");
        }
    }
 
    // Main method
    static void Main()
    {
        int[] A = { 1, 2, 5, 6 };
        int N = A.Length;
 
        // Call the function to check parity
        CheckParity(A, N);
 
        Console
            .ReadLine(); // To keep the console window open
    }
}


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);


Output

YES

Time Complexity: O(N)
Auxiliary Space: O(1),where N is the size of the array.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads