Longest subarray difference

Revision en1, by aman_naughty, 2019-09-09 09:31:35

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. 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)