# Represent N as sum of K even or K odd numbers with repetitions allowed

Given two integer N and K, the task is to find an array of size K containing only even or odd elements where the sum of all the elements of the array is N. If there is no such array print “No”.

Examples:

Input: N = 18, K = 3
Output: 6 6 6

Input: N = 19, K = 5
Output: 3 3 3 3 7

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to choose the smallest even or odd number K-1 times and Finally, compute the last number with the help of total sum. If the last number is also even for the even number and odd for the smallest odd number. Then it is possible to choose such an array. Otherwise, there is no such array possible.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find an ` `// array of size K with all the ` `// even or odd elements in the array ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the array with ` `// all the even / odd elements ` `void` `getArrayOfSizeK(``int` `n, ``int` `k) ` `{ ` `    ``vector<``int``> ans; ` ` `  `    ``// If array could be constructed ` `    ``// by adding odd elements ` `    ``// we need to find the kth ` `    ``// element is odd or even ` ` `  `    ``// if array of odd elements ` `    ``// would be the answer then ` `    ``// k-1 elements would be 1 ` ` `  `    ``// First let's check ` `    ``// kth is odd or even ` `    ``int` `odd = n - ((k - 1) * 1); ` ` `  `    ``// if last element is also ` `    ``// an odd number then ` `    ``// we can choose odd ` `    ``// elements for our answer ` `    ``if` `(odd > 0 ` `        ``&& odd % 2 != 0) { ` ` `  `        ``// Add 1 in the array (k-1) times ` `        ``for` `(``int` `i = 0; i < k - 1; i++) { ` `            ``ans.push_back(1); ` `        ``} ` ` `  `        ``// Add last odd element ` `        ``ans.push_back(odd); ` `    ``} ` ` `  `    ``// If array of even elements ` `    ``// would be the answer then ` `    ``// k-1 elements would be 2 ` `    ``int` `even = n - ((k - 1) * 2); ` ` `  `    ``// if last element is also ` `    ``// an even number then ` `    ``// we can choose even ` `    ``// elements for our answer ` `    ``if` `(even > 0 ` `        ``&& even % 2 == 0 ` `        ``&& ans.size() == 0) { ` ` `  `        ``// Add 2 in the array (k-1) times ` `        ``for` `(``int` `i = 0; i < k - 1; i++) { ` `            ``ans.push_back(2); ` `        ``} ` ` `  `        ``// Add last even element ` `        ``ans.push_back(even); ` `    ``} ` ` `  `    ``// Printing the array ` `    ``if` `(ans.size() > 0) { ` `        ``for` `(``int` `i = 0; i < k; i++) { ` `            ``cout << ans[i] << ``" "``; ` `        ``} ` `    ``} ` `    ``else` `{ ` `        ``cout << ``"NO"` `<< endl; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 10, k = 3; ` `    ``getArrayOfSizeK(n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to find an ` `// array of size K with all the ` `// even or odd elements in the array ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to find the array with ` `// all the even / odd elements ` `static` `void` `getArrayOfSizeK(``int` `n, ``int` `k) ` `{ ` `    ``Vector ans = ``new` `Vector(); ` ` `  `    ``// If array could be constructed ` `    ``// by adding odd elements ` `    ``// we need to find the kth ` `    ``// element is odd or even ` ` `  `    ``// If array of odd elements ` `    ``// would be the answer then ` `    ``// k-1 elements would be 1 ` ` `  `    ``// First let's check ` `    ``// kth is odd or even ` `    ``int` `odd = n - ((k - ``1``) * ``1``); ` ` `  `    ``// If last element is also ` `    ``// an odd number then ` `    ``// we can choose odd ` `    ``// elements for our answer ` `    ``if` `(odd > ``0` `&& odd % ``2` `!= ``0``) ` `    ``{ ` `         `  `        ``// Add 1 in the array (k-1) times ` `        ``for``(``int` `i = ``0``; i < k - ``1``; i++) ` `        ``{ ` `           ``ans.add(``1``); ` `        ``} ` ` `  `        ``// Add last odd element ` `        ``ans.add(odd); ` `    ``} ` ` `  `    ``// If array of even elements ` `    ``// would be the answer then ` `    ``// k-1 elements would be 2 ` `    ``int` `even = n - ((k - ``1``) * ``2``); ` ` `  `    ``// If last element is also ` `    ``// an even number then ` `    ``// we can choose even ` `    ``// elements for our answer ` `    ``if` `(even > ``0` `&& even % ``2` `== ``0` `&&  ` `                  ``ans.size() == ``0``) ` `    ``{ ` ` `  `        ``// Add 2 in the array (k-1) times ` `        ``for``(``int` `i = ``0``; i < k - ``1``; i++)  ` `        ``{ ` `           ``ans.add(``2``); ` `        ``} ` ` `  `        ``// Add last even element ` `        ``ans.add(even); ` `    ``} ` ` `  `    ``// Printing the array ` `    ``if` `(ans.size() > ``0``) ` `    ``{ ` `        ``for``(``int` `i = ``0``; i < k; i++) ` `        ``{ ` `           ``System.out.print(ans.get(i) + ``" "``); ` `        ``} ` `    ``} ` `    ``else`  `    ``{ ` `        ``System.out.println(``"NO"``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `n = ``10``, k = ``3``; ` `     `  `    ``getArrayOfSizeK(n, k); ` `} ` `} ` ` `  `// This code is contributed by Surendra_Gangwar `

## Python3

 `# Python 3 implementation to find an ` `# array of size K with all the ` `# even or odd elements in the array ` ` `  `# Function to find the array with ` `# all the even / odd elements ` `def` `getArrayOfSizeK(n, k): ` ` `  `    ``ans ``=` `[] ` ` `  `    ``# If array could be constructed ` `    ``# by adding odd elements ` `    ``# we need to find the kth ` `    ``# element is odd or even ` ` `  `    ``# if array of odd elements ` `    ``# would be the answer then ` `    ``# k-1 elements would be 1 ` ` `  `    ``# First let's check ` `    ``# kth is odd or even ` `    ``odd ``=` `n ``-` `((k ``-` `1``) ``*` `1``) ` ` `  `    ``# if last element is also ` `    ``# an odd number then ` `    ``# we can choose odd ` `    ``# elements for our answer ` `    ``if` `(odd > ``0` `        ``and` `odd ``%` `2` `!``=` `0``): ` ` `  `        ``# Add 1 in the array (k-1) times ` `        ``for` `i ``in` `range``(k ``-` `1``): ` `            ``ans.append(``1``) ` ` `  `        ``# Add last odd element ` `        ``ans.append(odd) ` ` `  `    ``# If array of even elements ` `    ``# would be the answer then ` `    ``# k-1 elements would be 2 ` `    ``even ``=` `n ``-` `((k ``-` `1``) ``*` `2``) ` ` `  `    ``# if last element is also ` `    ``# an even number then ` `    ``# we can choose even ` `    ``# elements for our answer ` `    ``if` `(even > ``0` `        ``and` `even ``%` `2` `=``=` `0` `        ``and` `len``(ans) ``=``=` `0``): ` ` `  `        ``# Add 2 in the array (k-1) times ` `        ``for` `i ``in` `range``(k ``-` `1``): ` `            ``ans.append(``2``) ` ` `  `        ``# Add last even element ` `        ``ans.append(even) ` ` `  `    ``# Printing the array ` `    ``if` `(``len``(ans) > ``0``): ` `        ``for` `i ``in` `range``( k): ` `            ``print` `(ans[i], end ``=` `" "``) ` `         `  `    ``else` `: ` `        ``print` `(``"NO"``) ` ` `  `# Driver Code ` `if` `__name__``=``=``"__main__"``: ` `    ``n, k ``=` `10``, ``3` `    ``getArrayOfSizeK(n, k) ` ` `  `# This code is contributed by chitranayal `

## C#

 `// C# implementation to find an ` `// array of size K with all the ` `// even or odd elements in the array ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` ` `  `// Function to find the array with ` `// all the even / odd elements ` `static` `void` `getArrayOfSizeK(``int` `n, ``int` `k) ` `{ ` `    ``List<``int``> ans = ``new` `List<``int``>(); ` ` `  `    ``// If array could be constructed ` `    ``// by adding odd elements ` `    ``// we need to find the kth ` `    ``// element is odd or even ` ` `  `    ``// If array of odd elements ` `    ``// would be the answer then ` `    ``// k-1 elements would be 1 ` ` `  `    ``// First let's check ` `    ``// kth is odd or even ` `    ``int` `odd = n - ((k - 1) * 1); ` ` `  `    ``// If last element is also ` `    ``// an odd number then ` `    ``// we can choose odd ` `    ``// elements for our answer ` `    ``if` `(odd > 0 && odd % 2 != 0) ` `    ``{ ` `         `  `        ``// Add 1 in the array (k-1) times ` `        ``for``(``int` `i = 0; i < k - 1; i++) ` `        ``{ ` `           ``ans.Add(1); ` `        ``} ` ` `  `        ``// Add last odd element ` `        ``ans.Add(odd); ` `    ``} ` ` `  `    ``// If array of even elements ` `    ``// would be the answer then ` `    ``// k-1 elements would be 2 ` `    ``int` `even = n - ((k - 1) * 2); ` ` `  `    ``// If last element is also ` `    ``// an even number then ` `    ``// we can choose even ` `    ``// elements for our answer ` `    ``if` `(even > 0 && even % 2 == 0 &&  ` `                   ``ans.Count == 0) ` `    ``{ ` ` `  `        ``// Add 2 in the array (k-1) times ` `        ``for``(``int` `i = 0; i < k - 1; i++)  ` `        ``{ ` `           ``ans.Add(2); ` `        ``} ` ` `  `        ``// Add last even element ` `        ``ans.Add(even); ` `    ``} ` ` `  `    ``// Printing the array ` `    ``if` `(ans.Count > 0) ` `    ``{ ` `        ``for``(``int` `i = 0; i < k; i++) ` `        ``{ ` `           ``Console.Write(ans[i] + ``" "``); ` `        ``} ` `    ``} ` `    ``else` `    ``{ ` `        ``Console.WriteLine(``"NO"``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``int` `n = 10, k = 3; ` `     `  `    ``getArrayOfSizeK(n, k); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

Output:

```2 2 6
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.