Skip to content
Related Articles

Related Articles

Divide given number into two even parts

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 29 Dec, 2021

Given a number N, the task is to check if this number can be divided into 2 even parts.

Examples:

Input: N = 8
Output: YES
Explanation: 8 can be divided into two even parts in two ways, 2, 6 or 4, 4 as both are even.

Input: N = 5
Output: NO

 

Naive approach: Check all number pairs upto N, such that they both are even and they both sum to N. If possible, print Yes, else No.

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient approach: Upon observation, it can be noticed that any even number can be expressed as sum of two even numbers except for 2 and no two even number can sum up to form an odd number.

Below is the implementation of the above approach.

C++




// C++ code to implement above approach
#include <bits/stdc++.h>
using namespace std;
  
// Divide number into 2 even parts
bool divNum(int n)
{
    return (n <= 2
                ? false
                : (n % 2 == 0
                       ? true
                       : false));
}
  
// Driven Program
int main()
{
    int n = 8;
    cout << (divNum(n) ? "YES" : "NO")
         << endl;
    return 0;
}

Java




// Java code to implement above approach
import java.util.*;
public class GFG {
  
  // Divide number into 2 even parts
  static boolean divNum(int n)
  {
    return (n <= 2 ? false
            : (n % 2 == 0 ? true : false));
  }
  
  // Driven Program
  public static void main(String args[])
  {
    int n = 8;
    System.out.println(divNum(n) ? "YES" : "NO");
  }
}
  
// This code is contributed by Samim Hossain Mondal.

Python




# Pyhton program for above approach
  
# Divide number into 2 even parts
def divNum(n):
    ans = False if n <= 2 else True if n % 2 == 0 else False                       
    return ans
  
# Driver Code
n = 8
  
if(divNum(n) == True):
    print("YES")
else:
    print("NO")
  
# This code is contributed by Samim Hossain Mondal.

C#




// C# code to implement above approach
using System;
class GFG {
  
    // Divide number into 2 even parts
    static bool divNum(int n)
    {
        return (n <= 2 ? false
                       : (n % 2 == 0 ? true : false));
    }
  
    // Driven Program
    public static void Main()
    {
        int n = 8;
        Console.WriteLine(divNum(n) ? "YES" : "NO");
    }
}
  
// This code is contributed by ukasp.

Javascript




<script>
// Javascript code to implement above approach
  
// Divide number into 2 even parts
function divNum(n)
{
    return (n <= 2
                ? false
                : (n % 2 == 0
                       ? true
                       : false));
}
  
// Driven Program
let n = 8;
document.write((divNum(n) ? "YES" : "NO") + "\n");
  
// This code is contributed by Samim Hossin Mondal.
</script>

 
 

Output

YES

Time Complexity: O(1)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!