For a given array A of size n, find the count of subarrays of size>=3, where the minimum of the elements at both the ends of subarray is greater than the maximum of all the numbers in between.
Formally, if the subarray starts at l and ends at r, then min(A[l], A[r]) > max(A[l+1], A[l+2],..., A[r-1]).
0<= n <=1e5 1<= A[i] <=1e9








I believe that size of subarray was >=3 and not >3 in the question,
my solution for >=3 was
n=3, arr=1 1 2
this should give 0 ,but your sol would give 1
Yeah simply Next ">=" Element(monotonic stack). Was this problem part of some OA?
.
i feel we can do this in two for loops one forward and one reverse like for every i in forward for loop find the index of very next element whose value is greater than or equal to a[i] if it exists then do ans++; else do nothing now in reverse for loop for every element find the element whose value is greater than or equal to a[i] and whose index is less than given element if it exist then do ans++; also if the index where we got a element greater or equal to given element is equal to given element then do mul_count++; final ans=ans-mul_count;make sure to check the size if size is greater than 3 only add to ans
yeah I did the same ig
When n = 6 and arr = 4 3 1 2 3 5 The ans should be 3 but your code gives 4 as ans.
can you please explain the though process and how ?
This was the solution I came up with, but I am not sure if it's correct. My reasoning was that if we are at position a[i] we are only interested if we have bigger element both to the left and to the right, if that is so we can for sure form an offer (?). That way I preprocess the array so I have prefix and suffix arrays which are :
$$$prefix[i] = max(prefix[i-1], arr[i-1]$$$, and
$$$suffix[i] = max(suffix[i+1], arr[i+1])$$$
I do this, because at each local maximum the condition of having max both to the left and right will fail, and I will be able to "mark" ends. Now consecutive elements that have bigger elements both to the left and to the right form one offer, and every time I break that consecutive property I know I am out of that offer.
Examples I have come up with :
1 2 3 -> 0
5 1 2 3 4 -> 1
10 1 1 10 1 1 9 -> 2, now the last one is controversial, my code outputs 2, but we could debate what's the correct answer to it!
Anyways, here's my code :
Try n = 6 and arr = 4 3 1 2 3 5. The ans should be 3 i guess.
can u check for any case for this code,since I earlier wrongly considered the equal case. Now it should work.
n = 12 and arr = 10 19 7 10 6 17 1 10 7 12 14 8
The correct output is 7 but your code gives 10 as ans
// 19..10,17 // 10..17 // 17..10,12,14 // 10..12,14
ain't the correct output 8?
[10,7,12,14] is not valid as 10 < 12
got it , I overcomplicated things :<
Now it seems correct. You can try stress testing to check wheather there is any edge cases that might cause problem or not. It helps during contest to debug your wrong code
yeah, usually I either directly get the code or I get stuck on edge cases by overcomplicating ,nothing in between
First of all compresed value of array. each value is maximum n.
Now lets fix the maximum element of inside interval, for all i find maximum j < i such that a[j] > a[i] mark L[i], and find minimum j > i such that a[j] > a[i] mark this with R[i].
Now you know where is ith element is maximum.
Fix the ith, and you need to find how many l > L[i] ^ l < i and r < R[i] ^ r > i such that min(a[l], a[r]) > a[i].
lets consider : left block is interval (L[i], i) right block is interval (i, R[i]) A = how many number from left block such that > a[i] B = how many number from right block such that > a[i]
we are almost done. you only need to sum for all i, A * B.
to find A and B you can do offline fenwick tree, i think that is easier way to do that.
that is all.
correct me if i am wrong.
Can you check if this works?
UPD: This will only work if all the elements are unique.
Your code gives wrong output when n = 6 and arr = 4 3 1 2 3 5. The ans is 3 but your code gives 4
There shoud be 4 possible subarray.
[1, 6], [2, 4], [2, 5], [2, 6]
UPD: I assumed all elements will be unique. there will be double counting in my code for cases like this.
a = [3, 1, 3]
so, I fixed my code a bit.
[2,6] subarray is not possible as min(a[2],a[6]) == max(a[3],a[4],a[5]) which violates the condition min(A[l], A[r]) > max(A[l+1], A[l+2],..., A[r-1]).
Sorry, I missed that. I encountered a similar problem on a different blog earlier, which appeared in Amazon coding interview. It was mentioned that all elements are unique on that problem.
For duplicate elements monotonic stack seems like the only option.
RachitKansal's solution seems promising for this problem. He provided his solution in the first comment of this blog
Auto comment: topic has been updated by Venkatesh2112001 (previous revision, new revision, compare).
it's just a simple stack q
~~~~~ void solve() { int n; cin >> n; vi a(n); cin >> a; ll res = 0; vi s; for(int i = 0; i < n; i++) { while(!s.empty() && a[s.back()] < a[i]) s.pop_back(); if(!s.empty() && i - s.back() + 1 >= 3) res++; s.pb(i); } vi().swap(s); for(int i = n — 1; i >= 0; i--) { while(!s.empty() && a[s.back()] < a[i]) s.pop_back(); if(!s.empty() && s.back() - i + 1 >= 3 && a[i] != a[s.back()]) res++; s.pb(i); } cout << res << '\n'; } ~~~~~Here is a straightforward solution in
O(nlogn + nlogn)using sparse table and binary search (for subarray size >= 3):AM I CORRECT ?
no
4
2 1 1 2
ans should be 1 your code gives 2
Use the max of the interval l..r as the pivot, then divide and conquer & use a monotone stack?