Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Edit</strong>: just figured out the <code>O(n)</code> solution for the unsorted case :) see below!</p> <h1>Sorted:</h1> <p>This can be solved in <code>O(log N</code>) for a sorted array, by means of a binary search on <code>n</code>. I'll be using OP's notation here, where <code>N = # of elements</code> and <code>n</code> is the answer we are looking for.</p> <p>If the array is sorted, it basically means we need to find a position <code>[N - n]</code> so that such position in the array contains a value greater than <code>n</code> - if it does, then there are at least <code>n</code> values greater than it, regardless of repeated values.</p> <p>Note an answer is always possible, as in the worst case the answer would be <code>0</code>, and there are always <em>at least</em> 0 elements greater than it. The answer always gets "easier" for lower values, obviously, as it is easier to find 1 element greater than 1, than 10 elements greater than 10. But more importantly, this function follows a monotonic (non-decreasing) behavior which allows us to use a binary search on it.</p> <p>The idea is as follows:</p> <pre><code>int N = 9; int arr[10] = {0,2,5,7,8,9,19,23,45}; int lo = 0, hi = N+1, mid; while(hi-lo &gt; 1){ mid = (hi+lo)/2; if(arr[N-mid] &gt; mid) lo = mid; else hi = mid; } n = lo; //highest value that worked </code></pre> <p><em>Breakdown:</em> The array has size <code>9</code>. A binary search may begin trying value <code>n = 5</code>, so we just check whether the 5th element from the end of the array is greater than 5. In this case, <code>8 &gt; 5</code> so we can try a better answer. The search would then attempt <code>7</code>, but the element at position <code>[N-7]</code> is <code>5</code>, which is lower than 7 and does not satisfy our constraints. Thus the search's last attempt is the value <code>6</code>, which returns true as <code>7 &gt; 6</code>.</p> <h1>Unsorted:</h1> <p>For the unsorted case, the idea is incredibly very similar! We can solve it in <code>O(n)</code> by using a <a href="http://en.wikipedia.org/wiki/Selection_algorithm" rel="noreferrer">Selection Algorithm</a> to identify the [N-n]th element, and at each step divide the search space in the same manner as the binary search.</p> <p>We begin by searching from <code>[0]</code> to <code>[N-1]</code> to find the median <em><code>(N/2 th)</code></em> element, and we can rearrange the array in another <code>O(N)</code> step such that the median element is placed in its correct position, and every element before it has a value <code>&lt;= median</code>, while every element after it has a value <code>&gt;=median</code>.</p> <p>Now, if that value is <em>greater</em> than <code>n</code> (in this case <code>N/2</code>), we showed above there are at least <code>n</code> elements greater than <code>n</code>, and <strong>thus we only need to search further in the lower half of the array</strong>. (If the median value is lower than <code>n</code>, we instead consider only the greater half of the array)</p> <p>Now, assuming <code>median &gt;= N/2</code> we will repeat the same process from index <code>[0]</code> to <code>[N/2]</code>, using a selection "sort" in <code>O(N/2)</code>, and so on, each time dividing the search space by 2. </p> <p>C++ code is as follows:</p> <pre><code>int N = 9; int arr[9] = {0,2,7,8,19,5,45,9,23}; int lo = 0, hi = N, mid; while(hi-lo &gt; 1){ mid = (hi+lo)/2; std::nth_element(arr+lo, arr+mid, arr+hi); if(arr[mid] &gt; N-mid) hi = mid; else lo = mid; } n = N-hi; </code></pre> <p>In the end, we achieve a complexity of <code>O(N) + O(N/2) + O(N/4) + ... = O(2*N) = O(N)</code></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. 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