Вопрос

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 !!!

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top