Open In App

Array Parity Transformation

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:




#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;
}




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




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)




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
    }
}




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.


Article Tags :