Open In App

Divide given number into two even parts

Last Updated : 14 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Code-

C++




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


Java




import java.util.*;
 
class Main {
 
// Divide number into 2 even parts
static boolean divNum(int n) {
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < n; j++) {
            if ((i % 2 == 0) && (j % 2 == 0) && (i + j == n)) {
                return true;
            }
        }
    }
    return false;
}
 
// Driven Program
public static void main(String[] args) {
    int n = 8;
    System.out.println(divNum(n) ? "YES" : "NO");
}
}


Python3




# Python code to implement above approach
 
# Divide number into 2 even parts
def divNum(n):
    for i in range(1, n):
        for j in range(1, n):
            if i % 2 == 0 and j % 2 == 0 and i + j == n:
                return True
    return False
 
# Driven Program
if __name__ == '__main__':
    n = 8
    print("YES" if divNum(n) else "NO")


C#




using System;
 
class Program
{
    // Divide number into 2 even parts
    static bool DivNum(int n)
    {
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < n; j++)
            {
                if ((i % 2 == 0) && (j % 2 == 0) && (i + j == n))
                {
                    return true;
                }
            }
        }
        return false;
    }
 
    // Driven Program
    static void Main(string[] args)
    {
        int n = 8;
        Console.WriteLine(DivNum(n) ? "YES" : "NO");
    }
}


Javascript




// Javascript code to implement above approach
 
// Divide number into 2 even parts
function divNum(n) {
  for (let i = 1; i < n; i++) {
for (let j = 1; j < n; j++) {
  if (i % 2 === 0 && j % 2 === 0 && i + j === n) {
    return true;
  }
}
  }
  return false;
}
 
// Driven Program
 
  const n = 8;
  console.log(divNum(n) ? "YES" : "NO");
   
// This code is contributed by Utkarsh Kumar


Output

YES

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




# Python 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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads