Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A quick google search returned <a href="http://www.careercup.com/question?id=2622" rel="nofollow">this</a>. In any case, if you're not looking for performance, <a href="http://en.wikipedia.org/wiki/Backtracking" rel="nofollow">backtracking</a> is always an option</p> <p>EDIT: I tried to apply backtracking. My solution is in no way efficient. You can, of course, replace the average methods to avoid another level of cycling. Also, in order to show that there is no solution, you can just count the number the solutions.</p> <pre><code>import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; public class SOQuestion { /** * just prints a solution * * @param list * @param indexes */ public static void printSolution(List list, HashSet indexes) { Iterator iter = indexes.iterator(); while (iter.hasNext()) { System.out.print(list.get((Integer) iter.next()) + " "); } System.out.println(); } /** * calculates the average of a list, but only taking into account the values * of at the given indexes * * @param list * @param indexes * @return */ public static float avg(List list, HashSet indexes) { Iterator iter = indexes.iterator(); float sum = 0; while (iter.hasNext()) { sum += (Integer) list.get((Integer) iter.next()); } return sum / indexes.size(); } /** * calculates the average of a list, ignoring the values of at the given * indexes * * @param list * @param indexes * @return */ public static float avg_e(List list, HashSet indexes) { float sum = 0; for (int i = 0; i &lt; list.size(); i++) { if (!indexes.contains(i)) { sum += (Integer) list.get(i); } } return sum / (list.size() - indexes.size()); } public static void backtrack(List list, int start, HashSet indexes) { for (int i = start; i &lt; list.size(); i++) { indexes.add(i); if (avg(list, indexes) == avg_e(list, indexes)) { System.out.println("Solution found!"); printSolution(list, indexes); } backtrack(list, i + 1, indexes); indexes.remove(i); } } public static void main(String[] args) { List test = new ArrayList(); test.add(2); test.add(1); test.add(3); backtrack(test, 0, new HashSet()); } } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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