mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
This commit is contained in:
@ -1,109 +0,0 @@
|
||||
package com.thealgorithms.misc;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TwoSumProblem {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
System.out.print("Enter the target sum ");
|
||||
int ts = scan.nextInt();
|
||||
System.out.print("Enter the number of elements in the array ");
|
||||
int n = scan.nextInt();
|
||||
System.out.println("Enter all your array elements:");
|
||||
int[] arr = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[i] = scan.nextInt();
|
||||
}
|
||||
TwoSumProblem t = new TwoSumProblem();
|
||||
System.out.println(
|
||||
"Brute Force Approach\n" +
|
||||
Arrays.toString(t.BruteForce(arr, ts)) +
|
||||
"\n"
|
||||
);
|
||||
System.out.println(
|
||||
"Two Pointer Approach\n" +
|
||||
Arrays.toString(t.TwoPointer(arr, ts)) +
|
||||
"\n"
|
||||
);
|
||||
System.out.println(
|
||||
"Hashmap Approach\n" + Arrays.toString(t.HashMap(arr, ts))
|
||||
);
|
||||
}
|
||||
|
||||
public int[] BruteForce(int[] nums, int target) {
|
||||
//Brute Force Approach
|
||||
int[] ans = new int[2];
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
for (int j = i + 1; j < nums.length; j++) {
|
||||
if (nums[i] + nums[j] == target) {
|
||||
ans[0] = i;
|
||||
ans[1] = j;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
public int[] TwoPointer(int[] nums, int target) {
|
||||
// HashMap Approach
|
||||
int[] ans = new int[2];
|
||||
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
hm.put(i, nums[i]);
|
||||
}
|
||||
HashMap<Integer, Integer> temp = hm
|
||||
.entrySet()
|
||||
.stream()
|
||||
.sorted((i1, i2) -> i1.getValue().compareTo(i2.getValue()))
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(e1, e2) -> e1,
|
||||
LinkedHashMap::new
|
||||
)
|
||||
);
|
||||
|
||||
int start = 0;
|
||||
int end = nums.length - 1;
|
||||
while (start < end) {
|
||||
int currSum = (Integer) temp.values().toArray()[start] +
|
||||
(Integer) temp.values().toArray()[end];
|
||||
|
||||
if (currSum == target) {
|
||||
ans[0] = (Integer) temp.keySet().toArray()[start];
|
||||
ans[1] = (Integer) temp.keySet().toArray()[end];
|
||||
break;
|
||||
} else if (currSum > target) {
|
||||
end -= 1;
|
||||
} else if (currSum < target) {
|
||||
start += 1;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public int[] HashMap(int[] nums, int target) {
|
||||
//Using Hashmaps
|
||||
int[] ans = new int[2];
|
||||
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
hm.put(nums[i], i);
|
||||
}
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
int t = target - nums[i];
|
||||
if (hm.containsKey(t) && hm.get(t) != i) {
|
||||
ans[0] = i;
|
||||
ans[1] = hm.get(t);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ans;
|
||||
}
|
||||
}
|
@ -11,21 +11,6 @@ import java.util.Arrays;
|
||||
*/
|
||||
class TwoPointers {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] arr = { 10, 20, 35, 50, 75, 80 };
|
||||
int key = 70;
|
||||
assert isPairedSum(arr, key);
|
||||
/* 20 + 60 == 70 */
|
||||
|
||||
arr = new int[] { 1, 2, 3, 4, 5, 6, 7 };
|
||||
key = 13;
|
||||
assert isPairedSum(arr, key);
|
||||
/* 6 + 7 == 13 */
|
||||
|
||||
key = 14;
|
||||
assert !isPairedSum(arr, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a sorted array arr (sorted in ascending order). Find if there
|
||||
* exists any pair of elements such that their sum is equal to key.
|
||||
@ -35,7 +20,7 @@ class TwoPointers {
|
||||
* @return {@code true} if there exists a pair of elements, {@code false}
|
||||
* otherwise.
|
||||
*/
|
||||
private static boolean isPairedSum(int[] arr, int key) {
|
||||
public static boolean isPairedSum(int[] arr, int key) {
|
||||
/* array sorting is necessary for this algorithm to function correctly */
|
||||
Arrays.sort(arr);
|
||||
int i = 0;
|
||||
|
44
src/test/java/com/thealgorithms/others/TwoPointersTest.java
Normal file
44
src/test/java/com/thealgorithms/others/TwoPointersTest.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
||||
public class TwoPointersTest {
|
||||
|
||||
|
||||
@Test
|
||||
void twoPointersFirstTestCase(){
|
||||
int[] arr = {2,6,9,22,121};
|
||||
int key = 28;
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr,key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPointersSecondTestCase(){
|
||||
int[] arr = {-1,-12,12,0,8};
|
||||
int key = 0;
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr,key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPointersThirdTestCase(){
|
||||
int[] arr = {12,35,12,152,0};
|
||||
int key = 13;
|
||||
assertEquals(false, TwoPointers.isPairedSum(arr,key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPointersFourthTestCase(){
|
||||
int[] arr = {-2,5,-1,52,31};
|
||||
int key = -3;
|
||||
assertEquals(true, TwoPointers.isPairedSum(arr,key));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPointersFiftiethTestCase(){
|
||||
int[] arr = {25,1,0,61,21};
|
||||
int key = 12;
|
||||
assertEquals(false, TwoPointers.isPairedSum(arr,key));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user