Check Even Partitioning

Easy
0/40
Average time to solve is 15m
9 upvotes
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”.
Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1:
2
16
11
Sample Output 1:
YES
NO
Explanation of sample input 1:
For the first test case, 
The number 16 can be represented as 10 + 6, and both 10 and 6 are even numbers. Hence the answer is “YES”.

For the second test case,
The number 11 cannot be represented as a sum of two even numbers. Hence the answer is “NO”.
Sample Input 2:
2
44
2
Sample Output 2:
YES
NO
Hint

The Sum of two even numbers is always even.

Approaches (1)
Simple Observation

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.
Time Complexity

O(1).

 

All operations in this approach are taking constant time. Hence the overall time complexity is O(1).

Space Complexity

O(1).

 

Constant space is being used. Hence, the overall space complexity is O(1).

Code Solution
(100% EXP penalty)
Check Even Partitioning
Full screen
Console