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

Given two integers **N** and **K**, the task is to represent **N** as sum of **K** odd numbers. If it is not possible to create the sum then output **-1**.

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

**Examples:**

Input:N = 5, K = 3

Output:1, 1, 3

Explanation:

The given number N can be represented as 1 + 1 + 3 = 5

Input:N = 7, K = 5

Output:1, 1, 1, 1, 3

Explanation:

The given number N can be represented as 1 + 1 + 1 + 1 + 3 = 7

**Approach:**

To solve the problem mentioned above a simple solution is to **maximise the occurrence of 1** which is the possible smallest odd number. Necessary conditions for representing the number N as K odd numbers are:

**(K – 1)**must be less than N.**N – (K – 1)**must be a Odd number.

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` `sumOddNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `check = N - (K - 1); ` ` ` ` ` `// N must be greater than equal to 2*K ` ` ` `// and must be odd ` ` ` `if` `(check > 0 && check % 2 == 1) { ` ` ` `for` `(` `int` `i = 0; i < K - 1; i++) { ` ` ` `cout << ` `"1 "` `; ` ` ` `} ` ` ` `cout << check; ` ` ` `} ` ` ` `else` ` ` `cout << ` `"-1"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 5; ` ` ` `int` `K = 3; ` ` ` ` ` `sumOddNumbers(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` `sumOddNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `check = N - (K - ` `1` `); ` ` ` ` ` `// N must be greater than equal ` ` ` `// to 2*K and must be odd ` ` ` `if` `(check > ` `0` `&& check % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < K - ` `1` `; i++) ` ` ` `{ ` ` ` `System.out.print(` `"1 "` `); ` ` ` `} ` ` ` `System.out.print(+check); ` ` ` `} ` ` ` `else` ` ` `System.out.println(` `"-1 "` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `N = ` `5` `; ` ` ` `int` `K = ` `3` `; ` ` ` ` ` `sumOddNumbers(N, K); ` `} ` `} ` ` ` `// This code is contributed by AbhiThakur ` |

*chevron_right*

*filter_none*

## Python3

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

*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` `sumOddNumbers(` `int` `N, ` `int` `K) ` `{ ` ` ` `int` `check = N - (K - 1); ` ` ` ` ` `// N must be greater than equal ` ` ` `// to 2*K and must be odd ` ` ` `if` `(check > 0 && check % 2 == 1) ` ` ` `{ ` ` ` `for` `(` `int` `i = 0; i < K - 1; i++) ` ` ` `{ ` ` ` `Console.Write(` `"1 "` `); ` ` ` `} ` ` ` `Console.Write(+check); ` ` ` `} ` ` ` `else` ` ` `Console.WriteLine(` `"-1 "` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 5; ` ` ` `int` `K = 3; ` ` ` ` ` `sumOddNumbers(N, K); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

1 1 3

**Time Complexity:** O(K)

## Recommended Posts:

- Represent N as sum of K even or K odd numbers with repetitions allowed
- Represent K^N as the sum of exactly N numbers
- Represent N as sum of K even numbers
- 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
- Pick maximum sum M elements such that contiguous repetitions do not exceed K
- Represent n as the sum of exactly k powers of two | Set 2
- Represent a given set of points by the best possible straight line
- 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 integers up to N which represent a Binary number
- Count of sticks required to represent the given string
- Different types of Coding Schemes to represent data
- Count of ways to represent N as sum of a prime number and twice of a square
- Maximum number of distinct positive integers that can be used to represent N
- Largest number with one swap allowed
- Find n-th element in a series with only 2 digits (4 and 7) allowed

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.