Next larger number in an array
<p>You are given an unsorted array A of n elements, now construct an array B for which 
B[i] = A[j] where j is the least number such that A[j] > A[i] and j>i
if such a j does not exist B[i] = -1
Eg:</p>

<pre><code>A={1,3,5,7,6,4,8}
B = {3 5 7 8 8 8 -1}
</code></pre>

<p>My solution</p>

<pre><code>#include&lt;iostream&gt;
using namespace std;
int main()
{
    int a[7]={9,3,5,2,6,4,8},b[7];
    int i,j,largest = -1;
    for(i=0;i&lt;7;i++)
    {
        j=i+1;
        while(j&lt;7)
        {
         if(a[j]&gt;a[i])
         {
             largest=a[j];
             break;
         }
         j++;
        }
        if(j == 7)
        largest = -1;
        b[i]= largest;
    }
    for(j=0;j&lt;7;j++)
    cout&lt;&lt;b[j]&lt;&lt;endl;
    return 0;
}
</code></pre>

<p>can any one suggest o(nlogn) solution.</p>
