As mysterious it may sound, this is a problem in Hacker-earth practice problems under data structure criteria. The problem statement is as below:
The problem is given an array A having N integers, for each i(1≤i≤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 such that A[x]>A[i], then take x=−1. Similarly, if there is no y>i such that A[y]>A[i], then take y=−1.
Input Format:
First line consists of a single integer denoting N.
Second line consists of N space separated integers denoting the array A.
First line consists of a single integer denoting N.
Second line consists of N space separated integers denoting the array A.
Output Format:
Print N space separated integers, denoting x+y for each i(1≤i≤N)
Print N space separated integers, denoting x+y for each i(1≤i≤N)
Constraints:
1≤N≤10⁶
1≤A[i]≤10¹⁸
1≤N≤10⁶
1≤A[i]≤10¹⁸
This problem was set by vaibhab jamini and was tested by prateek garg. It also appeared on a hacker-earth challenge named codemonk with some other data structure related problems.
Discussion:
So first of all, in someone’s mind, a very easy algorithm will pop up which is that parse through the array and for each array element, find the x and y and then we are done. Finding the x and y in this way is a easy way. But as soon as you write this, you understand that, this algorithm is O(n²) order algorithm and therefore it will give you a time limit when the constraint is as high as 10⁶.
This happens too. On writing the following C code, I have got 3 cases solved and other inputs provide a “time limit exceeded” notice. This means that you need a better algorithm.
Here, I mention my naive method for referral.
#include <stdio.h>
int main()
{ int N;int i; int j;
scanf(“%d”,&N);
long long *A=(long long *)calloc(N+2,sizeof(long long));
long long *B=(long long *)calloc(N+2,sizeof(long long));
for(i=0;i<N;i++)
{
scanf(“%lld”,&A[i]);
}
for(i=0;i<N;i++)
{
int x=-1; int y=-1;
for(j=i-1;j>-1;j — )
{
if(A[j]>A[i])
{
x=j+1;
break;
}
}
for(j=i+1;j<N;j++)
{
if(A[j]>A[i])
{
y=j+1;
break;
}
}
B[i]=x
B[i+1]=i;
B[i+2]=y;
printf(“%d “,B[i]);
}
}
int main()
{ int N;int i; int j;
scanf(“%d”,&N);
long long *A=(long long *)calloc(N+2,sizeof(long long));
long long *B=(long long *)calloc(N+2,sizeof(long long));
for(i=0;i<N;i++)
{
scanf(“%lld”,&A[i]);
}
for(i=0;i<N;i++)
{
int x=-1; int y=-1;
for(j=i-1;j>-1;j — )
{
if(A[j]>A[i])
{
x=j+1;
break;
}
}
for(j=i+1;j<N;j++)
{
if(A[j]>A[i])
{
y=j+1;
break;
}
}
B[i]=x
B[i+1]=i;
B[i+2]=y;
printf(“%d “,B[i]);
}
}
Now, I start to search for a better algorithm. Now as it seems, it is a variation problem of a so called “stock span problem” in which one is given an array of par day stock price and is supposed to find the number of days for which the stock price was less than that day’s price. This question is solved using a stack implementation. Statement of the stock span problem can be found here. Also, if you want to know what a stack is or want to take a casual look at a sample code, please look here.
First one takes i. Then one finds the index h(i) which denotes the maximum index less than i such that A[h(i)]>A[i]. Then one saves the i,h(i),h(h(i)).. in a stack. Now, when one goes from i to i+1, then,
if A[i+1]<A[i], then x=i; else A[i+1]>A[i], so the stack can directly provide the next best solution for this as the previous element greater than A[i] is the previous element in the stack. So, we basically do the following:
(a)we create the stock from right i=0. We save h(0).
(b) In each step, i, we compare the top of the stack and A[i]. We pop until top>A[i]. Then we push i. If the stack becomes empty, then we push i as the first node and the required x for i becomes -1.
This algorithm clearly gives a better solution. The code of this is not added to the post to just leave a little work for the readers. Also, better algorithms can be thought for the same problem.
So, this was my take of the problem,comment to let me know what you think of the problem.
Comments
Post a Comment