Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>public class IncrementSybmols { public static void main(String[] args) throws Throwable { List&lt;Integer&gt; syms = Arrays.asList(1,2,3,4,5); test(syms, 3, Arrays.asList(1,2,3), Arrays.asList(1,2,4)); test(syms, 3, Arrays.asList(2,5,4), Arrays.asList(3,1,2)); test(syms, 3, Arrays.asList(4,3,5), Arrays.asList(4,5,1)); test(syms, 3, Arrays.asList(5,4,2), Arrays.asList(5,4,3)); test(syms, 3, Arrays.asList(5,4,3), null); } private static void test(List&lt;Integer&gt; syms, int n, List&lt;Integer&gt; in, List&lt;Integer&gt; exp) { List&lt;Integer&gt; out = increment(syms, n, in); System.out.println(in+" -&gt; "+out+": "+( exp==out || exp.equals(out)?"OK":"FAIL")); } private static List&lt;Integer&gt; increment(List&lt;Integer&gt; allSyms, int n, List&lt;Integer&gt; in){ TreeSet&lt;Integer&gt; availableSym = new TreeSet&lt;Integer&gt;(allSyms); availableSym.removeAll(in); LinkedList&lt;Integer&gt; current = new LinkedList&lt;Integer&gt;(in); // Remove symbols beginning from the tail until a better/greater symbols is available. while(!current.isEmpty()){ Integer last = current.removeLast(); availableSym.add(last); // look for greater symbols Integer next = availableSym.higher(last); if( next != null ){ // if there is a greater symbols, append it current.add(next); availableSym.remove(next); break; } } // if there no greater symbol, then *shrug* there is no greater number if( current.isEmpty() ) return null; // fill up with smallest symbols again while(current.size() &lt; n){ Integer next = availableSym.first(); availableSym.remove(next); current.add(next); } return current; } } </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