Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def hasPair(l1: List[Int], l2: List[Int], target: Int): Option[Pair[Int, Int]] = {
- val sortedList = (l1.toVector ++ l2.toVector).sorted
- var x = 0
- var y = sortedList.length - 1
- while (x < y) {
- if (sortedList(x) + sortedList(y) == target) {
- if ((l1.exists(_ == sortedList(x)) && l2.exists(_ == sortedList(y))) || (l2.exists(_ == sortedList(x)) && l1.exists(_ == sortedList(y))) ) {
- return Some(sortedList(y), sortedList(x))
- }
- } else if (sortedList(x) + sortedList(y) < target) {
- x = x + 1
- } else { // sum too big!
- y = y - 1
- }
- }
- return None
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement