Let M be an integer in range [1; 1,000,000,000].
A decomposition of M is a set of unique integers whose sum is equal to M.
A decomposition is odd if it contains only odd integers.
A decomposition of M is maximal if there is no other decomposition of M greater in size of the set.
Write a function:
int[] maxOddDecomposition(int M)
that returns an array with a maximal odd decomposition of M. The numbers in array should be in ascending order. If M does not have any odd decomposition, an array should be empty. If there is more than one correct answer, the function may return any of them.
For example, M = 6 has four decompositions:
6 = 1 + 2 + 3 = 1 + 5 = 2 + 4 = 6
Only 1 + 5 is an odd decomposition, thus is the maximal odd decomposition. We should return it in array such that array[0] = 1 and array[1] = 5.








What is the problem source?
Not sure, but I think placing consecutive odd numbers starting from 1 except the last one, will always give optimal ans.
That is definitely the answer because you're maximizing the ammount of numbers in the set, but you would need to see the case where you would need to merge the last two elements in the set (for example, for m = 10 you would innitially have {1,3,5} but the only way to reach an answer is by making a set of size 2 like {1,9}).
The smallest number that can be formed by adding $$$k$$$ odd distinct numbers is $$$\displaystyle\sum_{i=1}^k (2i-1) = k^2$$$. Hence, $$$ans(M) \le \sqrt{M}$$$, where $$$ans(M)$$$ is the length of a maximal odd decomposition of $$$M$$$.
Now, let's add the first $$$k = \lfloor\sqrt{M}\rfloor$$$ odd numbers, and that sum is equal to $$$k^2 \le M$$$; then, there are two cases:
Explained here: https://stackoverflow.com/questions/31277297/how-to-find-a-maximal-odd-decomposition-of-integer-m