Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find all matching numbers, that sums to 'N' in a given array
    text
    copied!<p>My goal here is to find all possible combinations that sums to a given total. For example, if the array is 2 59 3 43 5 9 8 62 10 4 and if the total is 12, then possible combinations are</p> <pre><code>2 10 3 9 8 4 5 3 4 </code></pre> <p>Here is the first set of code, that I've written. Wondering the best improvements that can be done on this.</p> <pre><code> int find_numbers_matching_sum(int *number_coll, int total) { int *search_till = lower_bound(number_coll,number_coll+TOTAL_SIZE, total); int location = search_till - number_coll; if (*search_till &gt; total &amp;&amp; location &gt; 0 ) { --location; } while ( location &gt;= 0 ) { find_totals(number_coll,total,location); --location; } return 1; } int find_totals(int *number_coll, int total, int end_location) { int left_ones = total - number_coll[end_location]; int curloc = end_location; int *search_till = 0; int location ; int all_numbers[10]; int i = 0; all_numbers[i] = number_coll[end_location]; while ( left_ones &amp;&amp; curloc &gt;= 0 ) { search_till = lower_bound(number_coll,number_coll+end_location, left_ones); location = search_till - number_coll; if (*search_till &gt; left_ones &amp;&amp; location &gt; 0 ) { --location; } else if ( left_ones &lt; *search_till ) { break; } curloc=location; left_ones = left_ones - number_coll[curloc]; all_numbers[++i] = number_coll[curloc]; end_location = curloc - 1; } if ( !left_ones ) { while ( i&gt;=0) { cout &lt;&lt; all_numbers[i--] &lt;&lt; ' '; } } cout &lt;&lt; endl; return 1; } </code></pre>
 

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