Find all differences in an array in O(n)

前端 未结 7 989
没有蜡笔的小新
没有蜡笔的小新 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:09

    If the interviewer is fond of theoretical games, perhaps he was thinking of using a table of inputs and results? Any problem with a limit on the size of the input, and that has a known solution, can be solved by table lookup. Given that you have first created and stored that table, which might be large.

    So if the array size is limited, the problem can be solved by table lookup, which (given some assumptions) can even be done in constant time. Granted, even for a maximum array size of two (assuming 32-bit integers) the table will not fit in a normal computer's memory, or on the disks. For larger max sizes of the array, you're into "won't fit in the known universe" size. But, theoretically, it can be done.

    (But in reality, I think that Jens Gustedt's comment is more likely.)

提交回复
热议问题