Panda Guru LogoPanda
Guru

Josh Technology | SDE interview Round 3 Question | Help me to find solution

Round 3

Questions: Q. Create all unique N-size subarrays where the sum is M without repetitions.
(here repetitions of numbers are allowed but not repeated elements subarrays.)
I have to directly print those subarrays, so space complexity should be O(1).

Example:
If N = 4 and M = 10, the solution should be:

[1,1,1,7], [1,1,2,6], [1,1,3,5], [1,1,4,4], [1,2,3,4], [1,2,2,5], [1,3,3,3], [2,2,2,4]

( [1,1,5,3] is not allowed because it is the same as [1,1,3,5])

Candidate's Approach

My first approach was using a recursive tree, but since I couldn't use a hashset, I stopped thinking about this. My secondary approach involved recursion, but it was not optimized. I realized that I should not form all combinations and that I have to directly print the subarrays, which requires much calculation (using recursion, etc.). I got confused here and was unable to optimize this question, leading to my rejection.

Interviewer's Feedback

No feedback provided.