Longest subarray difference

Revision en2, by aman_naughty, 2019-09-09 09:49:51

Problem link : link

code : link

I am considering each element as a maximum element and then determining the size of the subarray in which that element can be maximum. If x is my maximum element and y is my minimum element in the optimal subarray, then we have (x-y)<B which implies that my y should be -> x-B< y <= x . Now I store the elements in a vector of pair where the first element is the element at index i and the second element is the index i. Then I sort it based on the first element. After that, I build a segment tree on the modified index array to get the maximum and minimum index of y which will then determine our ans.

In other words, if v[i] is my maximum element of the optimal subarray I need to find the maximum and minimum index of an element in the entire array which has a value y satisfying v[i]-B< y <=v[i]. For this purpose, I am using a segment tree to store the maximum and minimum index in a range and that range I am finding with the help of lower_bound and upper_bound.

If you look at the code maybe you can understand what I am trying to say.

This is giving wrong answer.

Can someone please help me, whether my approach is wrong or am I doing some implementation mistake.

Sorry for poor explanation of my code .

Tags #segment tree, #interview, #help, #beginner

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English aman_naughty 2019-09-09 09:49:51 378
en1 English aman_naughty 2019-09-09 09:31:35 1052 Initial revision (published)