How to print all increasing-index triplets in an array?

a 夏天 提交于 2019-12-08 05:27:51

问题


Given an array ar of length n, how can I print all triplets (ar[i], ar[j], ar[k]) in better than O(n^3) time where 0<=i<j<=k<n ?

Example Input:

ar[]={5,6,7}

Output:

5 6 6
5 6 7
6 7 7

回答1:


You can't.

Suppose that the array has n elements. Included in the output is each combination of values from the first third of the array, second third, and third third. That right there is n^3/27 = O(n^3) output.

You cannot produce O(n^3) output with less than O(n^3) work.



来源:https://stackoverflow.com/questions/57352141/how-to-print-all-increasing-index-triplets-in-an-array

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