Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe you could do a divide and conquer solution:</p> <ol> <li>go through the list once to establish its length - <code>O(n)</code></li> <li>go through the list again to position a cursor on the half - you now have cursors A &amp; B - <code>O(n)</code></li> <li><p>compare if A is the reverse of B:</p> <ul> <li><p>if length left is greater than some constant, split A into cursors A1 and A2 by traversing, do the same for B1 and B2 - <code>O(n)</code>; compare A1 and B2, and A2 and B1 in the same way</p></li> <li><p>if length left is smaller than some constant, just brute force compare them - copy B into an array and read it backwards comparing it with A - <code>O(1)</code></p></li> </ul></li> </ol> <p>Note that step 3 should be repeated <code>O(logn)</code> times, therefore, the complexity of the solution is <code>O(nlogn)</code>.</p> <p>Complexity analysis in more detail:</p> <pre><code>f(const) = const f(n) = f(n/2) + f(n/2) + C*n </code></pre> <p>Use substitution (<code>n = 2^m</code>, <code>f(2^m) = g(m)</code>) to solve this equation. Solution of this recursion should yield something in the same complexity class as n*logn.</p> <p>Space complexity is <code>O(logn)</code> because of recursive calls, but that does not seem to be violating any constraints. But the solution could be modified so that it doesn't use recursive calls, but a loop instead - you should only store recursion depth and position at that depth (imagine you have the recursion tree drawn - you simply need 2 integers to store the position in that tree to know what your next step is).</p>
    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. 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