Find the maximum element which is common in two arrays?

后端 未结 6 915
轻奢々
轻奢々 2021-02-13 15:51

Given two arrays, how to find the maximum element which is common to both the arrays?

I was thinking of sorting both the arrays(n log n) and then perform the binary sear

6条回答
  •  孤城傲影
    2021-02-13 16:09

    If you already know the range of numbers that would be in your arrays, you could perform counting sort, and then perform the binary search like you wanted. This would yield O(n) runtime.

提交回复
热议问题