Monday, April 29, 2024

📑 Difference in the Subset Sums 2

📑 Task

1) These programs divide a set into two subsets so
that the difference in the sums of the subsets is minimal
Can you describe the main principals that programs based?
2) Can you combine their solution methods in one program?


📑 Answer

1) The main principle behind these programs is dynamic programming.
The first program uses a table to store the results of subproblems
to find the minimum subset sum difference.
It iterates over the elements of the input array and
fills the table based on whether including or excluding an element results
in a subset sum closer to half of the total sum.
The second program uses a recursive approach to explore all possible subsets and find the minimum difference between the two subsets.
2)

📑 Difference in the Subset Sums

📑 Task

1) This program divides a set into two subsets so
that the difference in the sums of the subsets is minimal
The variable "table" indicates whether it is possible
to achieve a sum of j using the first i elements
Will all values be equal to 1 in the last row of the printed table?
2) During the iteration process, we actually only use the last two rows of the table
Can you modify the program using this fact?

📑 Answer

1) The last line will be [1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]
We will not be able to get sums equal to 2 and 5
from the numbers presented in the original list
2)