Faster algorithm to find unique element between two arrays?

后端 未结 9 1844
野性不改
野性不改 2020-12-22 20:20

EDIT: For anyone new to this question, I have posted an answer clarifying what was going on. The accepted answer is the one I feel best answers my question

9条回答
  •  执念已碎
    2020-12-22 20:41

    Assuming only one element was added, and the arrays were identical to start with, you could hit O(log(base 2) n).

    The rationale is that any array is subject to searching binary-ly O(log n). Except that in this case you are not searching for a value in an ordered array, you are searching for the first non-matching element. In such a circumstance a[n] == b[n] means that you are too low, and a[n] != b[n] means that you might be too high, unless a[n-1] == b[n-1].

    The rest is basic binary search. Check the middle element, decide which division must have the answer, and do a sub-search on that division.

提交回复
热议问题