# Find any K distinct odd integers such that there sum is equal to N

Given two integers **N** and **K**, the task is to find any **K** distinct odd integers such that their sum is equal to **N**. If no such integers exists, print -1.

**Examples:**

Input:N = 10, K = 2

Output:1, 9

Explanation:

There are two possible distinct odd integers, such that their sum is equal to N.

Possible K integers can be – {(1, 9), (3, 7)}

Input:N = 5, K = 4

Output:-1

Explanation:

There are no such 4 distinct odd integers such that their sum is 5.

**Approach:**

- The key observation in this problem is if N and K have different parity then it is not possible to find K such distinct integers such that their sum is equal to N,
- Otherwise such
**K – 1**integers will consist of**first K-1 odd positive integers** - The
**K**will be equal to^{th}odd number**(N – the sum of first (K-1) odd integers)**K

^{th}Odd number = N - sum of first K-1 integer

Below is the implementation of the above approach:

## C++

`// C++ implementation to find k ` `// odd integers such that their sum is N ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find K odd integers ` `// such that their sum is N ` `void` `oddIntegers(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Condition to check if there ` ` ` `// exist such K integers ` ` ` `if` `(n % 2 != k % 2) { ` ` ` `cout << ` `"-1"` ` ` `<< ` `"\n"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `int` `sum = 0; ` ` ` `int` `i = 1; ` ` ` `int` `j = 1; ` ` ` ` ` `// Loop to find first K-1 ` ` ` `// distinct odd integers ` ` ` `while` `(j < k) { ` ` ` `sum = sum + i; ` ` ` `cout << i << ` `" "` `; ` ` ` `i = i + 2; ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// Final Kth odd number ` ` ` `int` `finalOdd = n - sum; ` ` ` ` ` `cout << finalOdd << ` `"\n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `int` `k = 2; ` ` ` ` ` `oddIntegers(n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find k ` `// odd integers such that their sum is N ` `class` `GFG ` `{ ` ` ` `// Function to find K odd integers ` `// such that their sum is N ` `static` `void` `oddIntegers(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Condition to check if there ` ` ` `// exist such K integers ` ` ` `if` `(n % ` `2` `!= k % ` `2` `) { ` ` ` `System.out.println(` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `int` `sum = ` `0` `; ` ` ` `int` `i = ` `1` `; ` ` ` `int` `j = ` `1` `; ` ` ` ` ` `// Loop to find first K-1 ` ` ` `// distinct odd integers ` ` ` `while` `(j < k) { ` ` ` `sum = sum + i; ` ` ` `System.out.print(i+` `" "` `); ` ` ` `i = i + ` `2` `; ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// Final Kth odd number ` ` ` `int` `finalOdd = n - sum; ` ` ` ` ` `System.out.println(finalOdd); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` `int` `k = ` `2` `; ` ` ` ` ` `oddIntegers(n, k); ` `} ` `} ` ` ` `// This code is contributed by shubhamsingh ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find k ` `# odd integers such that their sum is N ` ` ` `# Function to find K odd integers ` `# such that their sum is N ` `def` `oddIntegers(n, k) : ` ` ` ` ` `# Condition to check if there ` ` ` `# exist such K integers ` ` ` `if` `(n ` `%` `2` `!` `=` `k ` `%` `2` `) : ` ` ` `print` `(` `"-1"` `); ` ` ` ` ` `return` `; ` ` ` ` ` `sum` `=` `0` `; ` ` ` `i ` `=` `1` `; ` ` ` `j ` `=` `1` `; ` ` ` ` ` `# Loop to find first K-1 ` ` ` `# distinct odd integers ` ` ` `while` `(j < k) : ` ` ` `sum` `+` `=` `i; ` ` ` `print` `(i,end` `=` `" "` `); ` ` ` `i ` `+` `=` `2` `; ` ` ` `j ` `+` `=` `1` `; ` ` ` ` ` `# Final Kth odd number ` ` ` `finalOdd ` `=` `n ` `-` `sum` `; ` ` ` ` ` `print` `(finalOdd); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `10` `; ` ` ` `k ` `=` `2` `; ` ` ` ` ` `oddIntegers(n, k); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find k ` `// odd integers such that their sum is N ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find K odd integers ` `// such that their sum is N ` `static` `void` `oddints(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Condition to check if there ` ` ` `// exist such K integers ` ` ` `if` `(n % 2 != k % 2) { ` ` ` `Console.WriteLine(` `"-1"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `int` `sum = 0; ` ` ` `int` `i = 1; ` ` ` `int` `j = 1; ` ` ` ` ` `// Loop to find first K-1 ` ` ` `// distinct odd integers ` ` ` `while` `(j < k) { ` ` ` `sum = sum + i; ` ` ` `Console.Write(i+` `" "` `); ` ` ` `i = i + 2; ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// Final Kth odd number ` ` ` `int` `finalOdd = n - sum; ` ` ` ` ` `Console.WriteLine(finalOdd); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 10; ` ` ` `int` `k = 2; ` ` ` ` ` `oddints(n, k); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

1 9

**Performance Analysis:**

**Time Complexity:**As in the above approach, There is a loop to find such K odd integers which takes O(K) time in worst case. Hence the Time Complexity will be**O(K)**.**Auxiliary Space Complexity:**As in the above approach, There is no extra space used. Hence the auxiliary space complexity will be**O(1)**.

## Recommended Posts:

- Check if the sum of distinct digits of two integers are equal
- Find K distinct positive odd integers with sum N
- Find distinct integers for a triplet with given product
- Find the first N integers such that the sum of their digits is equal to 10
- Find three integers less than or equal to N such that their LCM is maximum
- Find N distinct numbers whose bitwise Or is equal to K
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Number of distinct integers obtained by lcm(X, N)/X
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Check whether a number can be represented as sum of K distinct positive integers
- Integers from the range that are composed of a single distinct digit
- Maximum number of distinct positive integers that can be used to represent N
- Count number of integers less than or equal to N which has exactly 9 divisors
- Remove two consecutive integers from 1 to N to make sum equal to S
- Count all distinct pairs with product equal to K

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.