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 = 8
Output: YES
Explanation: 8 can be divided into two different even parts i.e. 2 and 6.Input: N = 5
Output: NO
Explanation: 5 can not be divided into two even parts in any way.Input: Â N = 4
Output: NO
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.
Brute Force Approach:
We iterate over all possible values of i from 1 to N-1, and check if i and (N-i) are both even and unequal. If we find such a pair of values, we return true, indicating that N can be divided into two unequal even parts. If we don’t find such a pair of values, we return false, indicating that N cannot be divided into two unequal even parts.
Implementation of the above approach:
C++
#include<iostream> using namespace std; Â
// Function to check if N can be divided // into two unequal even parts bool evenParts( int N) { Â Â Â Â for ( int i = 1; i < N; i++) { Â Â Â Â Â Â Â Â if (i % 2 == 0 && (N-i) % 2 == 0 && i != (N-i)) { Â Â Â Â Â Â Â Â Â Â Â Â return true ; Â Â Â Â Â Â Â Â } Â Â Â Â } Â Â Â Â return false ; } Â
// Driver code int main(){     int N = 8;          // Function call     bool ans = evenParts(N);          if (ans)         cout << "YES" << '\n' ;     else         cout << "NO" << '\n' ;          return 0; } |
Java
public class EvenParts { // Function to check if N can be divided // into two unequal even parts     public static boolean evenParts( int N) {         for ( int i = 1 ; i < N; i++) {             if (i % 2 == 0 && (N - i) % 2 == 0 && i != (N - i)) {                 return true ;             }         }         return false ;     } Â
    public static void main(String[] args) {         int N = 8 ; // Sample input         boolean ans = evenParts(N);         if (ans) {             System.out.println( "YES" );         } else {             System.out.println( "NO" );         }     } } |
Python3
# Function to check if N can be divided # into two unequal even parts def evenParts(N):     for i in range ( 1 , N):         if i % 2 = = 0 and (N - i) % 2 = = 0 and i ! = (N - i):             return True     return False Â
# Driver code if __name__ = = '__main__' : Â Â Â Â N = 8 Â
    # Function call     ans = evenParts(N) Â
    if ans:         print ( "YES" )     else :         print ( "NO" ) |
C#
// C# Implementation using System; Â
class GFG {     // Function to check if N can be divided     // into two unequal even parts     static bool evenParts( int N) {         for ( int i = 1; i < N; i++) {             if (i % 2 == 0 && (N - i) % 2 == 0 && i != (N - i)) {                 return true ;             }         }         return false ;     } Â
    // Driver code     static void Main( string [] args) {         int N = 8; Â
        // Function call         bool ans = evenParts(N); Â
        if (ans)             Console.WriteLine( "YES" );         else             Console.WriteLine( "NO" );     } } Â
// This code is contributed by Utkarsh Kumar |
Javascript
// Function to check if N can be divided into two unequal even parts function evenParts(N) { Â Â Â Â for (let i = 1; i < N; i++) { Â Â Â Â Â Â Â Â if (i % 2 == 0 && (N - i) % 2 == 0 && i != (N - i)) { Â Â Â Â Â Â Â Â Â Â Â Â return true ; Â Â Â Â Â Â Â Â } Â Â Â Â } Â Â Â Â return false ; } Â
let N = 8; Â
// Function call let ans = evenParts(N); Â
if (ans)     console.log( "YES" ); else     console.log( "NO" ); |
YES
Time Complexity: O(n)
Space Complexity: O(1)
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.
C++
// C++ code to implement above approach #include<iostream> using namespace std; Â
// Function to check if N can be divided // into two unequal even parts 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 int main(){    int N = 8;         // Function call    bool ans = evenParts(N); Â
   if (ans)        std::cout << "YES" << '\n' ;    else        std::cout << "NO" << '\n' ;      return 0; } |
Java
// Java code to implement above approach import 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. |
Python3
# Python code for the above approach Â
# Function to check if N can be divided # into two unequal even parts def 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 code N = 8 Â
# Function call ans = evenParts(N) if (ans): Â Â Â Â print ( "YES" ) else : Â Â Â Â print ( "NO" ) Â
# This code is contributed by Saurabh Jaiswal. |
C#
// C# code to implement above approach using 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. |
Javascript
<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)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!