fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. bool SubsetSum(vector<int> &A, int Sum)
  6. {
  7. bool dp[Sum+1][A.size()+1];
  8. int i, j;
  9. for(i=0; i<= A.size(); i++)
  10. dp[0][i] = false; // When sum = 0
  11. for(i=0; i<=Sum; i++)
  12. dp[i][0] = 1; // When num of elements = 0
  13. for(i = 1; i <= A.size(); i++)
  14. {
  15. for(j=1; j<= Sum; j++)
  16. {
  17. dp[i][j] = dp[i-1][j];
  18. if(j-A[i-1] >= 0)
  19. dp[i][j] = dp[i][j] || dp[i-1][j-A[i-1]];
  20. }
  21. }
  22. return dp[Sum][A.size()];
  23. }
  24.  
  25. void avgset(vector<int> &A) {
  26. int total = accumulate(A.begin(), A.end(), 0);
  27. int ntotal = A.size();
  28.  
  29. int i;
  30. for(i=1; i<=ntotal; i++) // Number of elements in the subset
  31. {
  32. if((total * i) % ntotal == 0)
  33. {
  34. if(SubsetSum(A, (total * i)/ntotal))
  35. cout<<"Array can be broken into 2 arrays each with equal average of "<<(total * i)/ntotal<<endl;
  36. }
  37. }
  38. }
  39.  
  40. int main()
  41. {
  42. vector<int> A = {1, 7, 15, 29, 11, 9};
  43. avgset(A);
  44. return 0;
  45. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty