Find all triplets in array with sum less than or equal to given sum

后端 未结 5 2198
星月不相逢
星月不相逢 2020-12-15 10:57

This was recently asked to a friend in an interview and we do not know of any solution other than the simple O(n3) one.

Is there some better algorithm?

5条回答
  •  不知归路
    2020-12-15 11:35

    I have an idea, but I'm not sure if it works.

    Preprocess (remove elements > S) and sort the array first.

    Then, after you picking up arr[i] and arr[j] where i < j, you may binary search S - arr[i] - arr[j] in the remaining array[j+1...n]. Once you binary-searched the index m, the k could lie between j+1 and m.

    I think this may reduce the complexity. What do you think?

提交回复
热议问题