Given two integers **N** and **K**, the task is to represent N as sum of K even number. If it is not possible to represent the number, print -1.

**Note:** The representation may contain duplicate even numbers.

**Examples:**

Input:N = 6, K = 3

Output:2 2 2

Explanation:

The given number 6 can be represented as 2 + 2 + 2 = 6

Input:N = 8, K = 2

Output:2 6

Explanation:

The given number 3 can be represented as 2 + 6 = 8

**Approach:** To solve the problem mentioned above a simple solution is to maximise the occurrence of 2 which is the smallest even number. Necessary condition to represent N as sum of K numbers are:

**(K – 1) * 2**must be less than N.**N – (K – 1) * 2**must be Even.

Below is the implementation of the above approach:

## C++

`// C++ implementation to represent ` `// N as sum of K even numbers ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the representation ` `void` `sumEvenNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `check = N - 2 * (K - 1); ` ` ` ` ` `// N must be greater than equal to 2*K ` ` ` `// and must be even ` ` ` `if` `(check > 0 && check % 2 == 0) { ` ` ` `for` `(` `int` `i = 0; i < K - 1; i++) { ` ` ` `cout << ` `"2 "` `; ` ` ` `} ` ` ` `cout << check; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"-1"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 8; ` ` ` `int` `K = 2; ` ` ` ` ` `sumEvenNumbers(N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to represent ` `// N as sum of K even numbers ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to print the representation ` `static` `void` `sumEvenNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `check = N - ` `2` `* (K - ` `1` `); ` ` ` ` ` `// N must be greater than equal to 2 * K ` ` ` `// and must be even ` ` ` `if` `(check > ` `0` `&& check % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < K - ` `1` `; i++) ` ` ` `{ ` ` ` `System.out.print(` `"2 "` `); ` ` ` `} ` ` ` `System.out.println(check); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `System.out.println(` `"-1"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `N = ` `8` `; ` ` ` `int` `K = ` `2` `; ` ` ` ` ` `sumEvenNumbers(N, K); ` `} ` `} ` ` ` `// This code is contributed by ANKITKUMAR34 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to represent ` `# N as sum of K even numbers ` ` ` `# Function to print the representation ` `def` `sumEvenNumbers(N, K): ` ` ` ` ` `check ` `=` `N ` `-` `2` `*` `(K ` `-` `1` `) ` ` ` ` ` `# N must be greater than equal to 2 * K ` ` ` `# and must be even ` ` ` `if` `(check > ` `0` `and` `check ` `%` `2` `=` `=` `0` `): ` ` ` `for` `i ` `in` `range` `(K ` `-` `1` `): ` ` ` `print` `(` `"2 "` `, end ` `=` `"") ` ` ` ` ` `print` `(check) ` ` ` `else` `: ` ` ` `print` `(` `"-1"` `) ` ` ` `# Driver Code ` `N ` `=` `8` `K ` `=` `2` `sumEvenNumbers(N, K) ` ` ` `# This code is contributed by ANKITKUMAR34 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to represent ` `// N as sum of K even numbers ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to print the representation ` `static` `void` `sumEvenNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `check = N - 2 * (K - 1); ` ` ` ` ` `// N must be greater than equal to ` ` ` `// 2 * K and must be even ` ` ` `if` `(check > 0 && check % 2 == 0) ` ` ` `{ ` ` ` `for` `(` `int` `i = 0; i < K - 1; i++) ` ` ` `{ ` ` ` `Console.Write(` `"2 "` `); ` ` ` `} ` ` ` `Console.WriteLine(check); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `Console.WriteLine(` `"-1"` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main(String []args) ` `{ ` ` ` `int` `N = 8; ` ` ` `int` `K = 2; ` ` ` ` ` `sumEvenNumbers(N, K); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

2 6

**Time Complexity:** O(K)

## Recommended Posts:

- Represent K^N as the sum of exactly N numbers
- Represent K as sum of N-bonacci numbers
- Represent N as sum of K odd numbers with repetitions allowed
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Represent the given number as the sum of two composite numbers
- Represent the fraction of two numbers in the string format
- Represent a number as sum of minimum possible psuedobinary numbers
- Minimum count of numbers required from given array to represent S
- Sort numbers based on count of letters required to represent them in words
- Represent a number as a sum of maximum possible number of Prime Numbers
- Represent n as the sum of exactly k powers of two | Set 2
- Different ways to represent N as sum of K non-zero integers
- Represent a given set of points by the best possible straight line
- Count of integers up to N which represent a Binary number
- Count of matchsticks required to represent the given number
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Count of sticks required to represent the given string
- Maximum number of distinct positive integers that can be used to represent N
- Count of ways to represent N as sum of a prime number and twice of a square
- Number of distinct ways to represent a number as sum of K unique primes

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.