Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int checkSum(int array[], int size, int sum){
  6.  
  7. int cache[size+1][sum+1];
  8. int counter = 0;
  9.  
  10. cache[0][0]=false;
  11. for (int i=1;i<=size;i++){
  12. int value = 0;
  13. for (int j=0;j<=sum;j++){
  14. value+=array[j];
  15. if (array[i]<value){
  16. cache[i][j]=true; // true if the values to the right are less than the sums to the left
  17. }
  18. }
  19. }
  20.  
  21. for (int i=1;i<=size;i++){
  22. for (int j=1;j<=sum;j++){
  23. if (array[i-1] > j){
  24. cache[i][j] = cache[i-1][j];
  25. } else {
  26. cache[i][j] = cache[i-1][j] || cache[i-1][j-array[i-1]];
  27. if (cache[i][j]==true){
  28. counter+=1; // counter of how many times the condition is met
  29. }
  30. }
  31. }
  32. }
  33.  
  34. return counter;
  35. }
  36.  
  37. int partition(int array[], int size){ // function that partitions by putting together the elements of the set one by one since the partitons are contiguous anyway
  38. int sum=0;
  39. int total=0;
  40. for (int i=0;i<size;i++){
  41. sum+=array[i];
  42. total+=checkSum(array,size,sum);
  43. }
  44. return total;
  45.  
  46. }
  47.  
  48. int main(){
  49. int array[] = {1,2,3,4,5};
  50. int size = 5;
  51. cout << partition(array,size) << "n";
  52. return 0;
  53. }
  54.  
  55. {1}{2}{3}{4}{5}
  56. {1}{2}{3}{4,5}
  57. {1}{2,3}{4,5}
  58. {1}{2}{3,4,5}
  59. {1}{2,3,4,5}
  60. {1,2}{3,4,5}
  61. {1,2,3}{4,5}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement