# 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 = 3Output:1, 1, 3Explanation:

The given number N can be represented as 1 + 1 + 3 = 5Input:N = 7, K = 5Output:1, 1, 1, 1, 3Explanation:

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;` `}` |

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

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

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

## JavaScript

`<script>` ` ` `// Javascript implementation to represent` `// N as sum of K even numbers` ` ` `// Function to print the representation` `function` `sumOddNumbers(N, K)` `{` ` ` `var` `check = N - (K - 1);` ` ` `var` `i;` ` ` ` ` `// N must be greater than equal to 2*K` ` ` `// and must be odd` ` ` `if` `(check > 0 && check % 2 == 1) {` ` ` `for` `(i = 0; i < K - 1; i++) {` ` ` `document.write(` `"1,"` `+` `" "` `);` ` ` `}` ` ` `document.write(check + ` `" "` `);` ` ` `}` ` ` `else` ` ` `document.write(` `"-1"` `);` `}` ` ` `// Driver Code` ` ` `var` `N = 5;` ` ` `var` `K = 3;` ` ` ` ` `sumOddNumbers(N, K);` ` ` `</script>` |

**Output:**

1 1 3

**Time Complexity:** O(K)

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****.**