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.
Input: N = 10, K = 2
Output: 1 9
Two odd positive integers such that their sum is 10 can be (1, 9) or (3, 7).
Input: N = 10, K = 4
There does not exists four odd positive integers with sum 10.
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:
1 3 5 91
- 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)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Maximum number of distinct positive integers that can be used to represent N
- Check whether a number can be represented as sum of K distinct positive integers
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Find n positive integers that satisfy the given equations
- Find N distinct integers with sum N
- Find N distinct integers with zero sum
- Find any K distinct odd integers such that their sum is equal to N
- Find distinct integers for a triplet with given product
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Ways to write N as sum of two or more positive integers | Set-2
- Check whether product of integers from a to b is positive , negative or zero
- Number of ways in which N can be represented as the sum of two positive integers
- Number of arrays of size N whose elements are positive integers and sum is K
- Count positive integers with 0 as a digit and maximum 'd' digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.