# Represent N as sum of K even numbers

Given two integers **N** and **K**, the task is to represent N as the 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 = 3Output:2 2 2Explanation:

The given number 6 can be represented as 2 + 2 + 2 = 6Input:N = 8, K = 2Output:2 6Explanation:

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

**Approach:** To solve the problem mentioned above a simple solution is to maximize the occurrence of 2 which is the smallest even number. The necessary condition to represent N as the 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;` `}` |

## 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` |

## 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` |

## 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` |

## Javascript

`<script>` `// Javascript implementation to represent` `// N as sum of K even numbers` ` ` `// Function to prlet the representation` `function` `sumEvenNumbers(N, K)` `{` ` ` `let 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` `(let i = 0; i < K - 1; i++)` ` ` `{` ` ` `document.write(` `"2 "` `);` ` ` `}` ` ` `document.write(check);` ` ` `}` ` ` `else` ` ` `{` ` ` `document.write(` `"-1"` `);` ` ` `}` `}` `// Driver Code` ` ` ` ` `let N = 8;` ` ` `let K = 2;` ` ` ` ` `sumEvenNumbers(N, K);` ` ` `</script>` |

**Output:**

2 6

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