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++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
int main()
{
int n = 8;
cout << (divNum(n) ? "YES" : "NO" )
<< endl;
return 0;
}
|
Java
import java.util.*;
class Main {
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 ;
}
public static void main(String[] args) {
int n = 8 ;
System.out.println(divNum(n) ? "YES" : "NO" );
}
}
|
Python3
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
if __name__ = = '__main__' :
n = 8
print ( "YES" if divNum(n) else "NO" )
|
C#
using System;
class Program
{
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 ;
}
static void Main( string [] args)
{
int n = 8;
Console.WriteLine(DivNum(n) ? "YES" : "NO" );
}
}
|
Javascript
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 ;
}
const n = 8;
console.log(divNum(n) ? "YES" : "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++
#include <bits/stdc++.h>
using namespace std;
bool divNum( int n)
{
return (n <= 2
? false
: (n % 2 == 0
? true
: false ));
}
int main()
{
int n = 8;
cout << (divNum(n) ? "YES" : "NO" )
<< endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static boolean divNum( int n)
{
return (n <= 2 ? false
: (n % 2 == 0 ? true : false ));
}
public static void main(String args[])
{
int n = 8 ;
System.out.println(divNum(n) ? "YES" : "NO" );
}
}
|
Python
def divNum(n):
ans = False if n < = 2 else True if n % 2 = = 0 else False
return ans
n = 8
if (divNum(n) = = True ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG {
static bool divNum( int n)
{
return (n <= 2 ? false
: (n % 2 == 0 ? true : false ));
}
public static void Main()
{
int n = 8;
Console.WriteLine(divNum(n) ? "YES" : "NO" );
}
}
|
Javascript
<script>
function divNum(n)
{
return (n <= 2
? false
: (n % 2 == 0
? true
: false ));
}
let n = 8;
document.write((divNum(n) ? "YES" : "NO" ) + "\n" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Jul, 2023
Like Article
Save Article