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

久未见 提交于 2019-12-04 05:16:38

问题


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.

it is possible to solve this problem in O (n) time?

This is not home work !!!


回答1:


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



来源:https://stackoverflow.com/questions/21532143/is-it-possible-to-find-all-the-triplets-in-the-given-array-for-the-o-n-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!