There is an incoming array of numbers, for example: {3, 2, 1, 6}. It is necessary to find the same sum of its subsets, i.e. in this case, 6 (3 + 2 + 1 = 6).
A detailed description of the solution to the problem is http://www.geeksforgeeks.org/dynamic-programming-set-18-partition-problem/ (all data hereinafter from this link). In the process of solving dynamic programming, we fill in the logic table:
int sum = 0; int i, j; // Caculcate sun of all elements for (i = 0; i < n; i++) sum += arr[i]; if (sum%2 != 0) return false; bool part[sum/2+1][n+1]; // initialize top row as true for (i = 0; i <= n; i++) part[0][i] = true; // initialize leftmost column, except part[0][0], as 0 for (i = 1; i <= sum/2; i++) part[i][0] = false; // Fill the partition table in botton up manner for (i = 1; i <= sum/2; i++) { for (j = 1; j <= n; j++) { part[i][j] = part[i][j-1]; if (i >= arr[j-1]) part[i][j] = part[i][j] || part[i - arr[j-1]][j-1]; } } /* // uncomment this part to print table for (i = 0; i <= sum/2; i++) { for (j = 0; j <= n; j++) printf ("%4d", part[i][j]); printf("\n"); } */ return part[sum/2][n]; I can not understand the logic of filling, namely the line:
part[i][j] = part[i][j] || part[i - arr[j-1]][j-1]; It is necessary to explain the language for monkeys, why do we take the cell part [i - arr [j-1]] [j-1] with such indices?
