First and last occurrence for binary search in C
<p>I'm trying to understand how do I modify the binary search for it work for first and last occurrences, surely I can find some code on the web but I'm trying to reach deep understanding, here is some basic non-recursive binary search I found:</p>

<pre><code>int BinarySearch(int *array, int number_of_elements, int key)
{
    int low = 0, high = number_of_elements-1, mid;
    while(low &lt;= high)
    {
            mid = (low + high)/2;
            if(array[mid] &lt; key)
            {
                    low = mid + 1; 
            }
            else if(array[mid] == key)
            {
                    return mid;
            }
            else if(array[mid] &gt; key)
            {
                    high = mid-1;
            }

    }
    return -1;
}
</code></pre>

<p>What modifications do I need to do and what are the logic behind them?</p>

<blockquote>
  <p>Edit: I would like for it to be efficient and not done linearly.</p>
</blockquote>