Given **N** positive integers **A _{1}, A_{2}, …, A_{N}**, the task is to determine the parity of the expression S.

For the given

**N**numbers, the expression S is given as:

**Examples:**

Input:N = 3, A1 = 2, A2 = 3, A3 = 1

Output:Even

Explanation:

S = 1 + (2 + 3 + 1) + (2*3 + 3*1 + 1*2) + (2*3*1) = 24, which is even

Input:N = 2, A1 = 2, A2 = 4

Output:Odd

Explanation:

S = 1 + (2 + 4) + (2 * 4) = 15, which is odd

**Naive Approach:** The naive approach for this problem is to plug in all the values of A_{i} in the given expression and find the parity of the given expression. This method doesn’t work for higher values of N as the multiplication is not a constant operation for higher-ordered numbers. And also, the value might become so large that it might cause integer overflow.

**Efficient Approach:** The idea is to perform some processing on the expression and reduce the expression into simpler terms so that the parity can be checked without computing the value. Let N = 3. Then:

- The expression S is:

1 + (A

_{1}+ A_{2}+ A_{3}) + ((A_{1}* A_{2}) + (A_{2}* A_{3}) + (A_{3}* A_{1}) + (A_{1}* A_{2}* A_{3}) - Now, the same expression is restructured as follows:

(1 + A

_{1}) + (A_{2}+ A_{1}* A_{2}) + (A_{3}+ A_{3}* A_{1}) + (A_{2}* A_{3}+ A_{1}* A_{2}* A_{3})=> (1 + A

_{1}) + A_{2}* (1 + A_{1}) + A_{3}* (1 + A_{1}) + A_{2}* A_{3}* (1 + A_{1}) - On taking (1 + A
_{1}) common from the above equation,

(1 + A

_{1}) * (1 + A_{2}+ A_{2}+ (A_{2}* A_{3}))=> (1 + A

_{1}) * (1 + A_{2}+ A_{3}* (1 + A_{2}) - Finally, on taking (1 + A
_{2}) common, the final expression becomes:

(1 + A

_{1}) * (1 + A_{2}) * (1 + A_{3}) - By symmetry, for N elements, the expression S becomes:

(1 + A

_{1}) * (1 + A_{2}) * (1 + A_{3}) … * (1 + A_{N}) - Clearly, for a number to become even parity, the answer must be even. It is known that the answer is even if any of the numbers are even.
- Therefore, the idea is to check if any of the numbers in the given input is odd. If it is, then on adding one, it becomes even and the value is even parity.

Below is the implementation of the above approach:

## C++

`// C++ program to determine the ` `// parity of the given mathematical ` `// expression ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `getParity( ` ` ` `int` `n, ` ` ` `const` `vector<` `int` `>& A) ` `{ ` ` ` ` ` `// Iterating through the ` ` ` `// given integers ` ` ` `for` `(` `auto` `x : A) { ` ` ` `if` `(x & 1) { ` ` ` ` ` `// If any odd number ` ` ` `// is present, then S ` ` ` `// is even parity ` ` ` `cout << ` `"Even"` `<< endl; ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Else, S is odd parity ` ` ` `cout << ` `"Odd"` `<< endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 3; ` ` ` `vector<` `int` `> A = { 2, 3, 1 }; ` ` ` `getParity(N, A); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to determine the ` `// parity of the given mathematical ` `// expression ` `class` `GFG{ ` ` ` `static` `void` `getParity(` `int` `n, ` `int` `[]A) ` `{ ` ` ` ` ` `// Iterating through the ` ` ` `// given integers ` ` ` `for` `(` `int` `x : A) ` ` ` `{ ` ` ` `if` `((x & ` `1` `) == ` `1` `) ` ` ` `{ ` ` ` ` ` `// If any odd number ` ` ` `// is present, then S ` ` ` `// is even parity ` ` ` `System.out.println(` `"Even"` `); ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Else, S is odd parity ` ` ` `System.out.println(` `"Odd"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `3` `; ` ` ` `int` `[] A = { ` `2` `, ` `3` `, ` `1` `}; ` ` ` `getParity(N, A); ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to determine the ` `# parity of the given mathematical ` `# expression ` `def` `getParity(n, A): ` ` ` ` ` `# Iterating through ` ` ` `# the given integers ` ` ` `for` `x ` `in` `A: ` ` ` `if` `(x & ` `1` `): ` ` ` ` ` `# If any odd number ` ` ` `# is present, then S ` ` ` `# is even parity ` ` ` `print` `(` `"Even"` `) ` ` ` `return` ` ` ` ` `# Else, S is odd parity ` ` ` `print` `(` `"Odd"` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `N ` `=` `3` ` ` `A ` `=` `[ ` `2` `, ` `3` `, ` `1` `] ` ` ` ` ` `getParity(N, A) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to determine the ` `// parity of the given mathematical ` `// expression ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` ` ` `static` `void` `getParity(` `int` `n, ` `int` `[]A) ` ` ` `{ ` ` ` ` ` `// Iterating through the ` ` ` `// given integers ` ` ` `foreach` `(` `int` `x ` `in` `A) ` ` ` `{ ` ` ` `if` `((x & 1) == 1) ` ` ` `{ ` ` ` ` ` `// If any odd number ` ` ` `// is present, then S ` ` ` `// is even parity ` ` ` `Console.WriteLine(` `"Even"` `); ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Else, S is odd parity ` ` ` `Console.WriteLine(` `"Odd"` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(` `string` `[] args) ` ` ` `{ ` ` ` `int` `N = 3; ` ` ` `int` `[] A = { 2, 3, 1 }; ` ` ` `getParity(N, A); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

Even

**Time Complexity:** *O(N)*, where N is the number of given numbers.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count numbers which can be represented as sum of same parity primes
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Deriving the expression of Fibonacci Numbers in terms of golden ratio
- Tau - A Mathematical Constant
- Check if two arrays are permutations of each other using Mathematical Operation
- Program to find parity
- Longest alternative parity subsequence
- Compute the parity of a number using XOR and table look-up
- Count of all subsequences having adjacent elements with different parity
- Count of all possible pairs of array elements with same parity
- Find the maximum sum pair in an Array with even parity
- Range Queries to count the number of even parity values with updates
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Maximize the value of the given expression
- Expression Evaluation
- Find Range Value of the Expression
- Maximize the expression (A AND X) * (B AND X) | Bit Manipulation
- Find the minimum value of X for an expression

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.