Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since the size of the array is just 14, so i won't work upon optimization. Your problem can be solved by finding all <strong>combinations using <code>bitwise operations</code></strong>. </p> <p>The idea is: Generate all the subsets of a given array (set), this set is known as a power set .For each of the subset(Combination), check whether the summations of the element(s) of the subset equals 40 or not.</p> <p>Refer the following tutorials, to learn, <strong><em>how can you find all combinations using Bit Wise Operations</em></strong>. <a href="http://www.codechef.com/wiki/tutorial-bitwise-operations" rel="nofollow">http://www.codechef.com/wiki/tutorial-bitwise-operations</a></p> <p>The C++ implementation: </p> <pre><code>int main() { int A[] = { 1, 7, 7, 4, 6, 5, 5, 2, 4, 7, 10, 3, 9, 6 }; int n = sizeof(A) / sizeof(A[0]); int desiredsum = 40; int total_soln=0; for (int i = 0; i &lt;= (1 &lt;&lt; n); ++i) { vector &lt; int &gt;v;/*The vector contains element of a subset*/ for (int j = 0; j &lt;= n; ++j) { if (i &amp; 1 &lt;&lt; j) v.push_back(A[j]); } if (v.size() == 8)/*Check whether the size of the current subset is 8 or not*/ { //if size is 8, check whether the sum of the elements of the current // subset equals to desired sum or not int sum = 0; for (int j = 0; j &lt; v.size(); ++j) { sum += v[j]; } if (sum == desiredsum) { for (int j = 0; j &lt; v.size(); ++j) { (j == v.size() - 1) ? cout &lt;&lt; v[j] &lt;&lt; "=" : cout &lt;&lt; v[j] &lt;&lt; "+"; } total_soln++; cout &lt;&lt; desiredsum &lt;&lt; " " &lt;&lt; endl; } } } cout&lt;&lt;"Total Solutions: "&lt;&lt;total_soln&lt;&lt;endl; return 0; } </code></pre> <p>IDEONE LINK: <a href="http://ideone.com/31jh6c" rel="nofollow">http://ideone.com/31jh6c</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload