Given an array.For each element we have to compute two values LL and RR.
LL=number of elements to left of particular array element which are less then it .
RR=number of elements to right of particular array element which are greater then it
We need to find maximum value of absolute(LL-RR) from array.
Solved in o(n^2) but wanted either O(n) or O(nlogn) approach .
INPUT: 1 5
1 2 1 3 4
OUTPUT: 4
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
int ll=0;
int rr=0;
int mx=INT_MIN;
for(int i=0;i<n;i++)
{
ll=rr=0;
for(int j=i-1;j>=0;j--)
{
if(a[j]<a[i])ll++;
}
for(int k=i+1;k<n;k++)
{
if(a[k]>a[i])rr++;
}
int t=abs(ll-rr);
if(t>mx)mx=t;
}
cout<<mx<<endl;
}
}