Given two integers **N** and **K**, the task is to find K distinct positive odd integers such that their sum is equal to the given number N.

**Examples:**

Input:N = 10, K = 2

Output:1 9

Explanation:

Two odd positive integers such that their sum is 10 can be (1, 9) or (3, 7).

Input:N = 10, K = 4

Output:NO

Explanation:

There does not exists four odd positive integers with sum 10.

**Approach:**

The number **N **can be represented as the sum of **K **positive odd integers only is the following two conditions satisfies:

- If the square of K is less than or equal to N and,
- If the sum of N and K is an even number.

If these conditions are satisfied then there exist K positive odd integers whose sum is N.

To generate K such odd numbers:

- Print first K-1 odd numbers starting from 1, i.e. 1, 3, 5, 7, 9…….
- The last odd number will be : N – (Sum of first K-1 odd positive integers)

Below is the implementation of the above approach:

## C++

`// C++ implementation to find K ` `// odd positive integers such that ` `// their sum is equal to given number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define ll long long int ` ` ` `// Function to find K odd positive ` `// integers such that their sum is N ` `void` `findDistinctOddSum(ll n, ll k) ` `{ ` ` ` `// Condition to check if there ` ` ` `// are enough values to check ` ` ` `if` `((k * k) <= n && ` ` ` `(n + k) % 2 == 0){ ` ` ` `int` `val = 1; ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 1; i < k; i++){ ` ` ` `cout << val << ` `" "` `; ` ` ` `sum += val; ` ` ` `val += 2; ` ` ` `} ` ` ` `cout << n - sum << endl; ` ` ` `} ` ` ` `else` ` ` `cout << ` `"NO \n"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `ll n = 100; ` ` ` `ll k = 4; ` ` ` `findDistinctOddSum(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find K ` `// odd positive integers such that ` `// their sum is equal to given number ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find K odd positive ` `// integers such that their sum is N ` `static` `void` `findDistinctOddSum(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Condition to check if there ` ` ` `// are enough values to check ` ` ` `if` `((k * k) <= n && ` ` ` `(n + k) % ` `2` `== ` `0` `){ ` ` ` `int` `val = ` `1` `; ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `1` `; i < k; i++){ ` ` ` `System.out.print(val+ ` `" "` `); ` ` ` `sum += val; ` ` ` `val += ` `2` `; ` ` ` `} ` ` ` `System.out.print(n - sum +` `"\n"` `); ` ` ` `} ` ` ` `else` ` ` `System.out.print(` `"NO \n"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `100` `; ` ` ` `int` `k = ` `4` `; ` ` ` `findDistinctOddSum(n, k); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find K ` `# odd positive integers such that ` `# their summ is equal to given number ` ` ` `# Function to find K odd positive ` `# integers such that their summ is N ` `def` `findDistinctOddsumm(n, k): ` ` ` ` ` `# Condition to check if there ` ` ` `# are enough values to check ` ` ` `if` `((k ` `*` `k) <` `=` `n ` `and` `(n ` `+` `k) ` `%` `2` `=` `=` `0` `): ` ` ` `val ` `=` `1` ` ` `summ ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, k): ` ` ` `print` `(val, end ` `=` `" "` `) ` ` ` `summ ` `+` `=` `val ` ` ` `val ` `+` `=` `2` ` ` `print` `(n ` `-` `summ) ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` `# Driver Code ` `n ` `=` `100` `k ` `=` `4` `findDistinctOddsumm(n, k) ` ` ` `# This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find K ` `// odd positive integers such that ` `// their sum is equal to given number ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `// Function to find K odd positive ` `// integers such that their sum is N ` `static` `void` `findDistinctOddSum(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Condition to check if there ` ` ` `// are enough values to check ` ` ` `if` `((k * k) <= n && ` ` ` `(n + k) % 2 == 0){ ` ` ` `int` `val = 1; ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 1; i < k; i++){ ` ` ` `Console.Write(val+ ` `" "` `); ` ` ` `sum += val; ` ` ` `val += 2; ` ` ` `} ` ` ` `Console.Write(n - sum +` `"\n"` `); ` ` ` `} ` ` ` `else` ` ` `Console.Write(` `"NO \n"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 100; ` ` ` `int` `k = 4; ` ` ` `findDistinctOddSum(n, k); ` `} ` `} ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

1 3 5 91

**Performance Analysis:**

**Time Complexity:**In the above-given approach, there is one loop which takes O(K) time in the worst case. Therefore, the time complexity for this approach will be**O(K)**.**Auxiliary Space:**O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check whether a number can be represented as sum of K distinct positive integers
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Find any K distinct odd integers such that their sum is equal to N
- Maximum number of distinct positive integers that can be used to represent N
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Number of arrays of size N whose elements are positive integers and sum is K
- Ways to write N as sum of two or more positive integers | Set-2
- Number of ways in which N can be represented as the sum of two positive integers
- Print all possible ways to write N as sum of two or more positive integers
- Find n positive integers that satisfy the given equations
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Find N distinct integers with zero sum
- Find N distinct integers with sum N
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Count 'd' digit positive integers with 0 as a digit
- Count positive integers with 0 as a digit and maximum 'd' digits
- Check whether product of integers from a to b is positive , negative or zero
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2

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.