There are two arrays A and B. I need to find sum of triplet in B with three conditions :-
I have tried this question but I can not find a way to optimize it. The time complexity that I am getting is o(n3). I need to find the solution so that it is less than o(n3).
A straightforward and slightly faster solution runs in O(n ^ 2). For each "middle" index j, find i such that A[i] < A[j] maximizing B[i], and find k such that A[j] < A[k] maximizing B[k]. These can both be found in one pass over possible indices.
It should be possible to speed this up more by preprocessing the input arrays. As it stands, this solution is still better than O(n ^ 3).