Divide a number into two unequal even parts
Given a positive integer N. The task is to decide whether the integer can be divided into two unequal positive even parts or not.
Input: N = 8
Explanation: 8 can be divided into two different even parts i.e. 2 and 6.
Input: N = 5
Explanation: 5 can not be divided into two even parts in any way.
Input: N = 4
Explanation: 4 can be divided into two even parts, 2 and 2. Since the numbers are equal, the output is NO.
Prerequisites: Knowledge of if-else conditional statements.
Approach: The core concept of the problem lies in the following observation:
The sum of any two even numbers is always even. Conversely any even number can be expressed as sum of two even numbers.
But here is two exceptions
- The number 2 is an exception here. It can only be expressed as the sum of two odd numbers (1 + 1).
- The number 4 can only be expressed as the sum of equal even numbers (2 + 2).
Hence, it is possible to express N as the sum of two even numbers only if N is even and not equal to 2 or 4. If N is odd, it is impossible to divide it into two even parts. Follow the steps mentioned below:
- Check if N = 2 or N = 4.
- If yes, then print NO.
- Else check if N is even (i.e. a multiple of 2)
- If yes, then print YES.
- Else, print NO.
Below is the implementation of the above approach.
Time Complexity: O(1)
Auxiliary Space: O(1)