Given an array A with N number of elements, for each element \$i\$  (\$1 \le i \le N\$), find \$x + y\$, where \$x\$ is the largest number less than \$i\$ such that \$A[x] > A[i]\$ and \$y\$ is the smallest number greater than \$i\$ such that \$A[y] > A[i]\$.If there is no \$x < i\$ where \$A[x] > A[i]\$, \$x=-1\$, similarlyIf there is no \$y > i\$ where \$A[y] > A[i]\$, \$y=-1\$The tricky part is:\$1 \le N \le 10^6\$\$1 \le A[i] \le 10^{18}\$SampleInput: 55 4 1 3 2Output:-2 0 6 1 3This is failing a few test cases:#include <iostream>using namespace std;int main(){    int n,i;     cin >> n;    long int a[n];    for(i=1;i<=n;i++)        cin >> a[i];    int x,y,c,d,s1,s2;    for(i=1;i<=n;i++){        c=0;        d=0;        s1=-1;        s2=-1;        x=i-1;        while(x>0){            if(a[x]>a[i]){                s1=x;                break;            }            else{                 x--;            }        }        y=i+1;            while(y<=n){                if(a[y]>a[i]){                    s2=y;                    break;                }                else                     y++;            }        int s=s1+s2;        cout << s <<  ;    }    return 0;}
Sum of indexes of the nearest larger neighbours of each array element
c++;programming challenge;time limit exceeded