Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2419 from krishna1m/0167-two-sum-ii-in…
Browse files Browse the repository at this point in the history
…put-array-is-sorted

Create: 0167-two-sum-ii-input-array-is-sorted.scala
  • Loading branch information
krishna1m authored Jul 6, 2023
2 parents ee29457 + 165e20f commit 4e8e4de
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions scala/0167-two-sum-ii-input-array-is-sorted.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Solution {
def twoSum(numbers: Array[Int], target: Int): Array[Int] = {
def loop(p1: Int, p2: Int): Array[Int] = {
if(numbers(p1) + numbers(p2) == target) Array(p1 + 1, p2 + 1)
else if(numbers(p1) + numbers(p2) > target) loop(p1, p2 - 1)
else loop(p1 + 1, p2)
}
loop(0, numbers.length - 1)
}
}

0 comments on commit 4e8e4de

Please sign in to comment.