spakk9's blog

By spakk9, history, 4 years ago, In English
Suppose we are given two increasing arrays A and B , we need to find max((a[j]-a[i-1] )- (b[j]-b[i-1])) , where j>=i .

Can we solve it in better than O(n^2) time complexity . If so , please give me any Hint .


  • Vote: I like it
  • 0
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +17 Vote: I do not like it

You are basically asking for $$$max((a[j] - b[j]) - (a[i] - b[i]))$$$ where i < j holds. Let $$$c_i = a_i - b_i$$$. Then it becomes: $$$max(c[j] - pref[j - 1])$$$ where $$$pref[i]$$$ is minimum element of $$$c[0...i]$$$. This can be solved in linear time.