Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a solution with a single outer pass. As <code>i</code> goes from <code>0</code> to <code>n-1</code>, <code>j</code> goes to <code>n-1</code> to <code>0</code>. The <code>l</code> and <code>r</code> indexes point to the first available "flex" spot (where <code>b[i] != 0</code>). If at any point <code>l</code> passes <code>r</code>, then there are no more free flex spots, and the next time <code>b[i] != 0</code> the outer loop will break prematurely with a "no solution."</p> <p>It seemed to be accurate, but if it does fail on some cases, then adding a few more conditions to the loops that advance the flex indexes should be sufficient to fix it.</p> <p>There is an extraneous assignment that will happen (when <code>b[i] == 0</code>, <code>c</code> will be set by both <code>i</code> and <code>j</code>), but it is harmless. (Same goes for the <code>l &gt; r</code> check.)</p> <pre><code>#include &lt;stdio.h&gt; #define EMPTY 0 int main() { int a[] = {1, 2, 3}; int b[] = {1, -1, -1}; int c[] = {EMPTY, EMPTY, EMPTY}; int n = sizeof(a) / sizeof(int); int l = 0, r = n - 1; int i, j; /* Work from both ends at once. * i = 0 .. n-1 * j = n-1 .. 0 * l = left most free "flex" (c[i] != 0) slot * r = right most free flex slot * * if (l &gt; r) then there are no more free flex spots * * when going right with i, check for -1 values * when going left with j, check for 1 values * ... but only after checking for 0 values */ for (i = 0, j = n - 1; i &lt; n; ++i, --j) { /* checking i from left to right... */ if (b[i] == 0) { c[i] = a[i]; /* advance l to the next free spot */ while (l &lt;= i &amp;&amp; c[l] != EMPTY) ++l; } else if (b[i] == -1) { if (i &lt;= l) break; c[l] = a[i]; /* advance l to the next free spot, * skipping over anything already set by c[i] = 0 */ do ++l; while (l &lt;= i &amp;&amp; c[l] != EMPTY); } /* checking j from right to left... */ if (b[j] == 0) { c[j] = a[j]; while (r &gt;= j &amp;&amp; c[r] != EMPTY) --r; } else if (b[j] == 1) { if (j &gt;= r) break; c[r] = a[j]; do --r; while (r &gt;= j &amp;&amp; c[r] != EMPTY); } if (l &gt; r) { /* there cannot be any more free flex spots, so advance l,r to the end points. */ l = n; r = -1; } } if (i &lt; n) printf("Unsolvable"); else { for (i = 0; i &lt; n; ++i) printf("%d ", c[i]); } printf("\n"); return 0; } </code></pre>
    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.
    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