is it possible to find all the triplets in the given array for the O (n) time?

前端 未结 1 2020
后悔当初
后悔当初 2021-01-24 05:44

Given an array of numbers find all such triplets that satisfy the given condition.

Condition: a[i] < a[j] < a[k] where I < j < k.

相关标签:
1条回答
  • 2021-01-24 06:33

    The size of the output (worst case) is a lower bound on the complexity.

    Since there are possibly O(n^3) such triplets, the complexity cannot be O(n).

    For example if the array is sorted from lowest to highest, you will have n choose 3 such triplets which is order of n^3.

    If the question refers to finding the number of triplets, here is the most efficient solution I saw:

    https://cs.stackexchange.com/questions/7409/count-unique-increasing-subsequences-of-length-3-in-on-log-n

    0 讨论(0)
提交回复
热议问题