Skip to content

Commit

Permalink
Two Sum II - Input Array Is Sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
dksifoua committed May 19, 2024
1 parent 08b8e29 commit 4399f1c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 13 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ int sum(int[] A, int n) {

## Solutions

| ID | Difficulty | Problem | Topics | Link |
|------|------------|---------------------------------|-----------------------------------|------------------------------------------------------------|
| 0001 | Easy | Two Sum | Array, HashMap | [solution](./docs/0001-Two-Sum.md) |
| 0002 | Medium | Add Two numbers | LinkedList, Recursion | |
| 0049 | Medium | Group Anagrams | Array, HashTable, String, Sorting | [solution](./docs/0049-Group-Anagrams.md ) |
| 0121 | Easy | Best Time to Buy and Sell Stock | Array | [solution](./docs/0121-Best-Time-to-Buy-and-Sell-Stock.md) |
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |
| 0169 | Easy | Majority Element | Array, HashTable, Counting | [solution](./docs/0169-Majority-Element.md) |
| 0217 | Easy | Contains Duplicate | Array | [solution](./docs/0217-Contains-Duplicate.md) |
| 0219 | Easy | Contains Duplicate II | Array | [solution](./docs/0219-Contains-Duplicate-II.md) |
| 0238 | Medium | Product of Array Except Self | Array | [solution](./docs/0238-Product-Of-Array-Except-Self.md) |
| 0242 | Easy | Valid Anagram | String, HashTable | [solution](./docs/0242-Valid-Anagram.md) |
| 0347 | Medium | Top K Frequent Elements | Array, HashMap, Bucket Sort | [Solution](./docs/0347-Top-K-Frequent-Elements.md) |
| ID | Difficulty | Problem | Topics | Link |
|------|------------|------------------------------------|-----------------------------------|------------------------------------------------------------|
| 0001 | Easy | Two Sum | Array, HashMap | [solution](./docs/0001-Two-Sum.md) |
| 0002 | Medium | Add Two numbers | LinkedList, Recursion | |
| 0049 | Medium | Group Anagrams | Array, HashTable, String, Sorting | [solution](./docs/0049-Group-Anagrams.md ) |
| 0121 | Easy | Best Time to Buy and Sell Stock | Array | [solution](./docs/0121-Best-Time-to-Buy-and-Sell-Stock.md) |
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |
| 0167 | Medium | Two Sum II - Input Array Is Sorted | Array, Two Pointers | [solution](./docs/0167-Two-Sum-II-Array-Is-Sorted.md) |
| 0169 | Easy | Majority Element | Array, HashTable, Counting | [solution](./docs/0169-Majority-Element.md) |
| 0217 | Easy | Contains Duplicate | Array | [solution](./docs/0217-Contains-Duplicate.md) |
| 0219 | Easy | Contains Duplicate II | Array | [solution](./docs/0219-Contains-Duplicate-II.md) |
| 0238 | Medium | Product of Array Except Self | Array | [solution](./docs/0238-Product-Of-Array-Except-Self.md) |
| 0242 | Easy | Valid Anagram | String, HashTable | [solution](./docs/0242-Valid-Anagram.md) |
| 0347 | Medium | Top K Frequent Elements | Array, HashMap, Bucket Sort | [Solution](./docs/0347-Top-K-Frequent-Elements.md) |
24 changes: 24 additions & 0 deletions docs/0167-Two-Sum-II-Array-Is-Sorted.md
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)
20 changes: 20 additions & 0 deletions src/main/java/io/dksifoua/leetcode/twosum2/Solution.java
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 src/test/java/io/dksifoua/leetcode/twosum2/SolutionTest.java
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);
}
}

0 comments on commit 4399f1c

Please sign in to comment.