Skip to content

Commit b129c38

Browse files
committed
Update Pair sum sorted
1 parent 6435b96 commit b129c38

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

kotlin/src/main/kotlin/common/twopointers/pairsumsorted/PairSumSorted.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ class PairSumSorted {
77
var right = nums.size - 1
88
while (left < right) {
99
val sum = nums[left] + nums[right]
10+
// If the sum is smaller, increment the left pointer, aiming
11+
// to increase the sum toward the target value.
1012
if (sum < target) {
1113
left++
14+
// If the sum is larger, decrement the right pointer, aiming
15+
// to decrease the sum toward the target value.
1216
} else if (sum > target) {
1317
right--
18+
// If the target pair is found, return its indexes.
1419
} else {
1520
return listOf(left, right)
1621
}

kotlin/src/main/kotlin/common/twopointers/pairsumsorted/PairSumSortedBruteForce.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package common.twopointers.pairsumsorted
33
class PairSumSortedBruteForce {
44

55
fun solution(nums: IntArray, target: Int): List<Int> {
6-
for (i in nums.indices) {
6+
val n = nums.size
7+
for (i in 0 until n) {
78
for (j in i + 1 until nums.size) {
89
if (nums[i] + nums[j] == target) {
910
return listOf(i, j)

0 commit comments

Comments
 (0)