Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First note that even if you let the immediate results overflow, the final result will always be correct if it can be represented. This is because integral types of any size act like cyclic groups under addition in most languages including Java (not in C, where integer overflow is undefined behavior, and C#, which is able to throw you an overflow exception).</p> <p>If you <em>still</em> want to prevent overflow, here's how to perform it in-place and in linear time:</p> <ul> <li><p>split the array in-place to its negative entries (in any order) and to its positive entries (in any order). Zero can end up anywhere. In other words, perform one quick-sort step with the pivot being zero.</p></li> <li><p>Let <code>ni</code> point to the start of the array (where negative entries are located).</p></li> <li>Let <code>pi</code> point to the end of the array.</li> <li>Let <code>sum</code> be zero.</li> <li><p>While <code>pi &gt;= ni</code></p> <ul> <li>if <code>sum</code> is negative <ul> <li>add <code>arr[pi]</code> to the <code>sum</code>.</li> <li>if <code>arr[pi]</code> is negative (we've run out of positive addends) and <code>sum</code> is positive (an overflow has occured), the result overflows.</li> <li>decrement <code>pi</code></li> </ul></li> <li>else <ul> <li>add <code>arr[ni]</code> to the <code>sum</code>.</li> <li>if <code>arr[ni]</code> is positive and <code>sum</code> is negative, the result overflows.</li> <li>increment <code>ni</code>.</li> </ul></li> </ul></li> <li><p>Finally, check if <code>sum</code> has more than <code>K</code> bits. If it does, declare the result overflows.</p></li> </ul>
    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. 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