Divide a number into two unequal even parts - GeeksforGeeks

29 Dec, 2021
Given a positive integer N. The task is to decide whether the integer can be divided into two unequal positive even parts or not.
Examples:
Input: N = 8Output: YESExplanation: 8 can be divided into two different even parts i.e. 2 and 6.
Input: N = 5Output: NOExplanation: 5 can not be divided into two even parts in any way.
Input:  N = 4Output: NOExplanation: 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.
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.
C++
Java
Python3
C#
Javascript
// C++ code to implement above approach#include<iostream>using namespace std;  // Function to check if N can be divided // into two unequal even partsbool evenParts(int N){       // Check if N is equal to 2 or 4      if(N == 2 || N == 4)        return false;      // Check if N is even    if(N % 2 == 0)        return true;    else        return false;}   //Driver codeint main(){   int N = 8;         // Function call   bool ans = evenParts(N);     if(ans)       std::cout << "YES" << '\n';   else       std::cout << "NO" << '\n';      return 0;}
// Java code to implement above approachimport java.util.*;public class GFG {    // Function to check if N can be divided   // into two unequal even parts  static boolean evenParts(int N)  {         // Check if N is equal to 2 or 4      if(N == 2 || N == 4)      return false;      // Check if N is even    if(N % 2 == 0)      return true;    else      return false;  }    // Driver code  public static void main(String args[])  {    int N = 8;      // Function call    boolean ans = evenParts(N);      if(ans)      System.out.println("YES");    else      System.out.println("NO");    }}  // This code is contributed by Samim Hossain Mondal.
# Python code for the above approach  # Function to check if N can be divided# into two unequal even partsdef evenParts(N):      # Check if N is equal to 2 or 4    if (N == 2 or N == 4):        return False      # Check if N is even    if (N % 2 == 0):        return True    else:        return False  # Driver codeN = 8  # Function callans = evenParts(N)if (ans):    print("YES")else:    print("NO")  # This code is contributed by Saurabh Jaiswal.
// C# code to implement above approachusing System;class GFG {    // Function to check if N can be divided   // into two unequal even parts  static bool evenParts(int N)  {             // Check if N is equal to 2 or 4      if(N == 2 || N == 4)      return false;      // Check if N is even    if(N % 2 == 0)      return true;    else      return false;  }    // Driver code  public static void Main()  {    int N = 8;      // Function call    bool ans = evenParts(N);      if(ans)      Console.Write("YES" + '\n');    else      Console.Write("NO" + '\n');    }}  // This code is contributed by Samim Hossain Mondal.
<script>       // JavaScript code for the above approach        // Function to check if N can be divided        // into two unequal even parts       function evenParts(N)       {                    // Check if N is equal to 2 or 4             if (N == 2 || N == 4)               return false;            // Check if N is even           if (N % 2 == 0)               return true;           else               return false;       }        // Driver code       let N = 8;        // Function call       let ans = evenParts(N);       if (ans)           document.write("YES" + '<br>')       else           document.write("NO" + '<br>')  // This code is contributed by Potta Lokesh   </script>
  
YES
 
Time Complexity: O(1)Auxiliary Space: O(1)
 
lokeshpotta20
samim2000
_saurabh_jaiswal
Algo-Geek 2021
Algo Geek
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Find sum of factorials till N factorial (1! + 2! + 3! + ... + N!)
Maximize partitions that if sorted individually makes the whole Array sorted
Generate string after adding spaces at specific positions in a given String
Maximum count of adjacent pairs with even sum in given Circular Array
Day-Stout-Warren algorithm to balance given Binary Search Tree
Program for Fibonacci numbers
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
C++ Data Types
Coin Change | DP-7