-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# [Two Sum II - Input Array Is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) | ||
|
||
## Intuition | ||
|
||
Given a sorted array, we need to find two numbers that sum up to a specific target. Since the array is sorted, we can use a two-pointer technique to find the two numbers efficiently. This approach leverages the sorted nature of the array to eliminate the need for nested loops, thus optimizing the search process. | ||
|
||
## Approach | ||
|
||
1. **Initialize Two Pointers:** Start with one pointer `leftIndex` at the beginning of the array and the other pointer `rightIndex` at the end of the array. | ||
2. **Iterate with Two Pointers:** | ||
- Calculate the sum of the elements at the two pointers. | ||
- If the sum is less than the target, move the `leftIndex` pointer to the right to increase the sum. | ||
- If the sum is greater than the target, move the `rightIndex` pointer to the left to decrease the sum. | ||
- If the sum equals the target, return the 1-indexed positions of the two elements. | ||
3. **Return Result:** If the loop exits without finding the target sum (which is guaranteed not to happen due to problem constraints), throw an `IllegalArgumentException. | ||
|
||
## Complexity | ||
|
||
- **Time Complexity: `O(N)`**, where `N` is the length of the array. Each element is checked at most once, making the time complexity linear. | ||
- **Time Complexity: `O(1)`**, as we are using only a constant amount of extra space for the pointers and a few variables. | ||
|
||
## Code | ||
|
||
- [Java](../src/main/java/io/dksifoua/leetcode/twosum2/Solution.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.dksifoua.leetcode.twosum2; | ||
|
||
public class Solution { | ||
|
||
public int[] twoSum(int[] numbers, int target) { | ||
int leftIndex = 0, rightIndex = numbers.length - 1; | ||
while (leftIndex < rightIndex) { | ||
int sum = numbers[leftIndex] + numbers[rightIndex]; | ||
if (sum < target) { | ||
leftIndex += 1; | ||
} else if (sum > target) { | ||
rightIndex -= 1; | ||
} else { | ||
return new int[] { leftIndex + 1, rightIndex + 1 }; | ||
} | ||
} | ||
|
||
throw new IllegalArgumentException(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/io/dksifoua/leetcode/twosum2/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.dksifoua.leetcode.twosum2; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SolutionTest { | ||
|
||
public final Solution solution = new Solution(); | ||
|
||
@Test | ||
void test1() { | ||
int[] expected = { 1, 2 }; | ||
int[] actual = solution.twoSum(new int[] { 2, 7, 11, 15 }, 9); | ||
Assertions.assertArrayEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
void test2() { | ||
int[] expected = { 1, 3 }; | ||
int[] actual = solution.twoSum(new int[] { 2, 3, 4 }, 6); | ||
Assertions.assertArrayEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
void test3() { | ||
int[] expected = { 1, 2 }; | ||
int[] actual = solution.twoSum(new int[] { -1, 0 }, -1); | ||
Assertions.assertArrayEquals(expected, actual); | ||
} | ||
} |