We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Search
- Pairs
- Discussions
Pairs
Pairs
Sort by
recency
|
1260 Discussions
|
Please Login in order to post a comment
My Java 8 solution, which passes all test cases, for a Max Score of 50:
(1) Sort the Array in Ascending Order.
(2) Iterate through the array sequentially from
arrIndex = 0
all the way toarrIndex = arr.size() - 2
, and do the following:(2) (a) Make a
subList
which spans fromarrIndex + 1
to the last index of the array. This is our way of narrowing our search space. We expect the other value in the pair to occur in this space.(2) (b) For the element at
arr[arrIndex]
: do a "Binary Search" through the Narrowed Sorted Sublist (costingO(log (n))
- to attempt to find the "other corresponding expected value", which would result in the "target difference". If search is successful, then Another Pair has been FOUND. At this point, we also increment thesubListStartIndex
, narrowing our search space further.Overall Expected Worst Case Time Complexity:
O(n * log(n))
.Kotlin
Sort the array then use binary search