Skip to content

Commit cd3f0fc

Browse files
dhananjay12echeyne
authored andcommitted
BAEL-2391 Two pointer technique
* two pointer technique * Corrected two sum * Changed asserts in unit test
1 parent 1695857 commit cd3f0fc

7 files changed

Lines changed: 215 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.algorithms.twopointertechnique;
2+
3+
public class LinkedListFindMiddle {
4+
5+
public <T> T findMiddle(MyNode<T> head) {
6+
MyNode<T> slowPointer = head;
7+
MyNode<T> fastPointer = head;
8+
9+
while (fastPointer.next != null && fastPointer.next.next != null) {
10+
fastPointer = fastPointer.next.next;
11+
slowPointer = slowPointer.next;
12+
}
13+
return slowPointer.data;
14+
}
15+
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.algorithms.twopointertechnique;
2+
3+
public class MyNode<E> {
4+
MyNode<E> next;
5+
E data;
6+
7+
public MyNode(E value) {
8+
data = value;
9+
next = null;
10+
}
11+
12+
public MyNode(E value, MyNode<E> n) {
13+
data = value;
14+
next = n;
15+
}
16+
17+
public void setNext(MyNode<E> n) {
18+
next = n;
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.algorithms.twopointertechnique;
2+
3+
public class RotateArray {
4+
5+
public void rotate(int[] input, int step) {
6+
step %= input.length;
7+
reverse(input, 0, input.length - 1);
8+
reverse(input, 0, step - 1);
9+
reverse(input, step, input.length - 1);
10+
}
11+
12+
private void reverse(int[] input, int start, int end) {
13+
while (start < end) {
14+
int temp = input[start];
15+
input[start] = input[end];
16+
input[end] = temp;
17+
start++;
18+
end--;
19+
}
20+
}
21+
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.algorithms.twopointertechnique;
2+
3+
public class TwoSum {
4+
5+
public boolean twoSum(int[] input, int targetValue) {
6+
7+
int pointerOne = 0;
8+
int pointerTwo = input.length - 1;
9+
10+
while (pointerOne < pointerTwo) {
11+
int sum = input[pointerOne] + input[pointerTwo];
12+
13+
if (sum == targetValue) {
14+
return true;
15+
} else if (sum < targetValue) {
16+
pointerOne++;
17+
} else {
18+
pointerTwo--;
19+
}
20+
}
21+
22+
return false;
23+
}
24+
25+
public boolean twoSumSlow(int[] input, int targetValue) {
26+
27+
for (int i = 0; i < input.length; i++) {
28+
for (int j = 1; j < input.length; j++) {
29+
if (input[i] + input[j] == targetValue) {
30+
return true;
31+
}
32+
}
33+
}
34+
35+
return false;
36+
}
37+
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.algorithms.twopointertechnique;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class LinkedListFindMiddleUnitTest {
8+
9+
LinkedListFindMiddle linkedListFindMiddle = new LinkedListFindMiddle();
10+
11+
@Test
12+
public void givenLinkedListOfMyNodes_whenLinkedListFindMiddle_thenCorrect() {
13+
14+
MyNode<String> head = createNodesList(8);
15+
16+
assertThat(linkedListFindMiddle.findMiddle(head)).isEqualTo("4");
17+
18+
head = createNodesList(9);
19+
20+
assertThat(linkedListFindMiddle.findMiddle(head)).isEqualTo("5");
21+
}
22+
23+
private static MyNode<String> createNodesList(int n) {
24+
25+
MyNode<String> head = new MyNode<String>("1");
26+
MyNode<String> current = head;
27+
28+
for (int i = 2; i <= n; i++) {
29+
MyNode<String> newNode = new MyNode<String>(String.valueOf(i));
30+
current.setNext(newNode);
31+
current = newNode;
32+
}
33+
34+
return head;
35+
}
36+
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.algorithms.twopointertechnique;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class RotateArrayUnitTest {
8+
9+
private RotateArray rotateArray = new RotateArray();
10+
11+
private int[] inputArray;
12+
13+
private int step;
14+
15+
@Test
16+
public void givenAnArrayOfIntegers_whenRotateKsteps_thenCorrect() {
17+
18+
inputArray = new int[] { 1, 2, 3, 4, 5, 6, 7 };
19+
step = 4;
20+
21+
rotateArray.rotate(inputArray, step);
22+
23+
assertThat(inputArray).containsExactly(new int[] { 4, 5, 6, 7, 1, 2, 3 });
24+
}
25+
26+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.algorithms.twopointertechnique;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
8+
public class TwoSumUnitTest {
9+
10+
private TwoSum twoSum = new TwoSum();
11+
12+
private int[] sortedArray;
13+
14+
private int targetValue;
15+
16+
@Test
17+
public void givenASortedArrayOfIntegers_whenTwoSumSlow_thenPairExists() {
18+
19+
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
20+
21+
targetValue = 12;
22+
23+
assertTrue(twoSum.twoSumSlow(sortedArray, targetValue));
24+
}
25+
26+
@Test
27+
public void givenASortedArrayOfIntegers_whenTwoSumSlow_thenPairDoesNotExists() {
28+
29+
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
30+
31+
targetValue = 20;
32+
33+
assertFalse(twoSum.twoSumSlow(sortedArray, targetValue));
34+
}
35+
36+
@Test
37+
public void givenASortedArrayOfIntegers_whenTwoSum_thenPairExists() {
38+
39+
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
40+
41+
targetValue = 12;
42+
43+
assertTrue(twoSum.twoSum(sortedArray, targetValue));
44+
}
45+
46+
@Test
47+
public void givenASortedArrayOfIntegers_whenTwoSum_thenPairDoesNotExists() {
48+
49+
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
50+
51+
targetValue = 20;
52+
53+
assertFalse(twoSum.twoSum(sortedArray, targetValue));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)