Find all differences in an array in O(n)

前端 未结 7 1022
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 05:39

Question: Given a sorted array A find all possible difference of elements from A.

My solution:

for (int i=0; i

        
7条回答
  •  时光取名叫无心
    2020-12-30 06:04

    For example for an array with the elements {21, 22, ..., 2n} there are n⋅(n-1)/2 possible differences, and no two of them are equal. So there are O(n2) differences.

    Since you have to enumerate all of them, you also need at least O(n2) time.

提交回复
热议问题