Last Updated: 9 Dec, 2020

Check Even Partitioning

Easy
Asked in company
Nagarro Software

Problem statement

You are given a positive integer ‘N’. You have to check whether ‘N’ can be represented as a sum of two even numbers or not.

For example:
If the given number is 6, The number 6 can be represented as 2 + 4, and both 2 and 4 are even numbers. Hence the answer is “YES”.
Input Format:
The first line of the input contains an integer, 'T,’ denoting the number of test cases.

The first line of each test case contains a single integer ‘N’, representing the given integer.     
Output Format:
For each test case, print “YES” if ‘N’ can be represented as a sum of two even numbers. Otherwise, print “NO”.

Print the output of each test case in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 10^9

Time limit: 1 sec

Approaches

01 Approach

The intuition behind the approach is that the sum of two even numbers is always even.

  1. We will check if N is odd, then the answer will be “NO”.
  2. For all even numbers except 2, the answer will be “YES” because all even numbers greater than 2 can be represented as a sum of two even numbers.
     

Algorithm:

  • We will declare a variable answer, which will store the solution of the problem.
  • We will declare variable rem to store the remainder of N when divided by 2.
  • Set rem as N%2.
  • If rem is equal to 1
    • Set answer as False.
  • Otherwise. If rem is equal to 0.
    • If N is equal to 2, set the answer as False because 2 is the only even number that cannot be represented as a sum of two even numbers.
    • Otherwise, set the answer as True.
  • Return the variable answer.