-4

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;



}




}
melpomene
  • 79,257
  • 6
  • 70
  • 127
  • 1
    Can you post what you already have? – Michael Veksler Mar 31 '19 at 08:10
  • Please read [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [mcve]. – Some programmer dude Mar 31 '19 at 08:19
  • 1
    Important read: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – πάντα ῥεῖ Mar 31 '19 at 08:28
  • This is not C code, no matter how many times you try to add the `c` tag. – melpomene Mar 31 '19 at 09:13
  • @melpomene this is not C++ either. Unlike C, C++ doesn't have VLAs. – Michael Veksler Mar 31 '19 at 14:21

1 Answers1

0

First get rid of:

#include<bits/stdc++.h>  // non standard
using namespace std; // will cause name collision in the future.

Instead, include what you need:

#include <iostream>

And use the std:: prefix like:.

std::cout << mx << std::endl;

To improve your complexity, think what your goals are, and how you are achieving them now.

Currently, you count elements smaller than a[i] by going over all elements before it, linearly. You need a data structure that can answer that in O(log i) rather than in O(i).

One possibility is a red-black tree that counts the number of nodes in each subtree.

Iterate over the array from left to right. Insert each element to the rb-tree, updating the counts this is O(log i) for each a[i]. From the inserted node, go up to the root and every time you arrive from the right: sum the counts on the left + 1. This gives LL, and works in O(log i) for each a[i].

For RR, do the reverse. go from right to left, and do the same as you did before, but do it on negated values (this way the same rb-tree code can be reused easily). Store the counts in an RR array.

Michael Veksler
  • 7,337
  • 1
  • 16
  • 29