Java icon indicating copy to clipboard operation
Java copied to clipboard

[FEATURE REQUEST] Optimized approach to Subset Sum problem

Open Rupa-Rd opened this issue 1 year ago • 4 comments

What would you like to Propose?

I would like to propose an space optimized solution to the Subset Sum problem.

Issue details

The existing subset sum problem solution's space complexity is O(n*sum). By optimizing the algorithm/ approach further, we can achieve the space complexity of O(sum) which is more efficient than the existing one.

Algorithm link: https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/SubsetSum.java

Additional Information

No response

Rupa-Rd avatar Oct 06 '24 06:10 Rupa-Rd

kindly assign this issue to me

ssugam10 avatar Oct 06 '24 09:10 ssugam10

please assign this task to me. It will surely help you. I will do my best.

Pranjul2002 avatar Oct 06 '24 09:10 Pranjul2002

Hey @Rupa-Rd

I'd like to propose a solution for this:

We can reduce the space complexity by using a 1D array instead of a 2D array. Since, at any point in the iteration, the result only depends on the current row and the previous row in the matrix. Therefore, we can maintain a single row and update it in reverse order (to avoid overwriting values).

Let me know if this works, I'm implementing this on my local machine until then :)

aayu5hgit avatar Oct 06 '24 18:10 aayu5hgit

Please assign to me.

lfcbird avatar Oct 07 '24 13:10 lfcbird