# Check if K distinct positive even integers with given sum exists or not

Given two integers **N** and **K**, the task is to find if there exist** K** distinct positive even integers such that their sum is equal to the given number N.

**Examples:**

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

Input:N = 16, K = 3Output:YesExplanation:Three distinct positive even integers with sum 16 are 8, 6 and 2.

Since, three such numbers exist, print “YES”.

Input:N = 18 K = 4Output:NoExplanation:Sum of four distinct positive even integers cannot be equal to 18. Hence, print “NO”.

**Approach: **The idea to solve this problem is to observe that if **N** **is** **odd**, then it will be impossible to get required **N** by **K** even numbers. If **N** is even, then find the sum of the first **K** even numbers and if their sum is less than or equal to **N**, then print “YES”. Otherwise, there does not exist **K** distinct even integers with sum equal to **N**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find if the sum` `// of K even integers equals N` `void` `isPossibleN(` `int` `N, ` `int` `K)` `{` ` ` `// If N is odd, then its impossible` ` ` `// to obtain N from K even numbers` ` ` `if` `(N % 2 == 1) {` ` ` `cout << ` `"NO"` `<< endl;` ` ` `return` `;` ` ` `}` ` ` `// Sum first k even numbers` ` ` `int` `sum = K * (K + 1);` ` ` `// If sum is less then equal to N` ` ` `if` `(sum <= N) {` ` ` `cout << ` `"YES"` `<< endl;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `cout << ` `"No"` `<< endl;` ` ` `}` ` ` `return` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 16;` ` ` `int` `K = 3;` ` ` `isPossibleN(N, K);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG` `{` ` ` `static` `void` `isPossibleN(` `int` `N, ` `int` `K)` `{` ` ` `// If N is odd, then its impossible` ` ` `// to obtain N from K even numbers` ` ` `if` `(N % ` `2` `== ` `1` `) {` ` ` `System.out.println(` `"NO"` `);` ` ` `return` `;` ` ` `}` ` ` `// Sum first k even numbers` ` ` `int` `sum = K * (K + ` `1` `);` ` ` `// If sum is less then equal to N` ` ` `if` `(sum <= N) {` ` ` `System.out.println(` `"YES"` `);` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `System.out.println(` `"No"` `);` ` ` `}` `}` `// Driver Code` `public` `static` `void` `main(String args[])` `{` ` ` `int` `N = ` `16` `;` ` ` `int` `K = ` `3` `;` ` ` `isPossibleN(N, K);` `}` `}` `// This code is contributed by jana_sayantan.` |

## Python3

`# Python3 program for the above approach` `# Function to find if the sum` `# of K even integers equals N` `def` `isPossibleN(N, K) :` ` ` ` ` `# If N is odd, then its impossible` ` ` `# to obtain N from K even numbers` ` ` `if` `(N ` `%` `2` `=` `=` `1` `) :` ` ` `print` `(` `"NO"` `)` ` ` `return` ` ` ` ` ` ` `# Sum first k even numbers` ` ` `sum` `=` `K ` `*` `(K ` `+` `1` `)` ` ` ` ` `# If sum is less then equal to N` ` ` `if` `(` `sum` `<` `=` `N) :` ` ` `print` `(` `"YES"` `)` ` ` ` ` ` ` `# Otherwise` ` ` `else` `:` ` ` `print` `(` `"NO"` `)` ` ` ` ` `return` ` ` `# Driver Code` `N ` `=` `16` `K ` `=` `3` ` ` `isPossibleN(N, K)` |

## C#

`// C# implementation of the` `// above approach` `using` `System;` `class` `GFG` `{` ` ` `static` `void` `isPossibleN(` `int` `N, ` `int` `K)` ` ` `{` ` ` `// If N is odd, then its impossible` ` ` `// to obtain N from K even numbers` ` ` `if` `(N % 2 == 1) {` ` ` `Console.WriteLine(` `"NO"` `);` ` ` `return` `;` ` ` `}` ` ` `// Sum first k even numbers` ` ` `int` `sum = K * (K + 1);` ` ` `// If sum is less then equal to N` ` ` `if` `(sum <= N) {` ` ` `Console.WriteLine(` `"YES"` `);` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `Console.WriteLine(` `"No"` `);` ` ` `}` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `N = 16;` ` ` `int` `K = 3;` ` ` `isPossibleN(N, K);` ` ` `}` `}` `// This code is contributed by jana_sayantan.` |

## Javascript

`<script>` `function` `isPossibleN( N, K)` `{` ` ` `// If N is odd, then its impossible` ` ` `// to obtain N from K even numbers` ` ` `if` `(N % 2 == 1) {` ` ` `document.write(` `"NO"` `);` ` ` `return` `;` ` ` `}` ` ` `// Sum first k even numbers` ` ` `let sum = K * (K + 1);` ` ` `// If sum is less then equal to N` ` ` `if` `(sum <= N) {` ` ` `document.write(` `"YES"` `);` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `document.write(` `"No"` `);` ` ` `}` `}` `// Driver Code` ` ` `let N = 16;` ` ` `let K = 3;` ` ` `isPossibleN(N, K);` `//this code is contributed by sravan kumar` `</script>` |

**Output:**

YES

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