From 4d301033b179c0706278f2241be00e35a80e4c9d Mon Sep 17 00:00:00 2001 From: varunvjha Date: Thu, 17 Sep 2020 10:58:26 +0530 Subject: [PATCH 1/5] added TwoPointersAlgo --- Others/TwoPointersAlgo.java | 73 +++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Others/TwoPointersAlgo.java diff --git a/Others/TwoPointersAlgo.java b/Others/TwoPointersAlgo.java new file mode 100644 index 000000000..31e2b367c --- /dev/null +++ b/Others/TwoPointersAlgo.java @@ -0,0 +1,73 @@ +import java.util.*; +import java.lang.*; +import java.io.*; + +class TwoPointersAlgo { + //This function prints all pairs in the array that sum to a number X. If no such pair exists then output will be -1. + static void twoSum(int A[], int X) + { + Arrays.sort(A); + + //Array sorting is necessary for this algo to function correctly + + int n = A.length; + int i = 0, j = n-1, flag=0; + //Implementation of the algorithm starts + while(i0) + { + t--; + n = in.nextInt(); + int a[] = new int[n]; + for(int i = 0; i Date: Thu, 17 Sep 2020 11:07:43 +0530 Subject: [PATCH 2/5] Update TwoPointersAlgo.java --- Others/TwoPointersAlgo.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Others/TwoPointersAlgo.java b/Others/TwoPointersAlgo.java index 31e2b367c..903c9d79c 100644 --- a/Others/TwoPointersAlgo.java +++ b/Others/TwoPointersAlgo.java @@ -2,6 +2,8 @@ import java.util.*; import java.lang.*; import java.io.*; +//https://www.geeksforgeeks.org/two-pointers-technique/ + class TwoPointersAlgo { //This function prints all pairs in the array that sum to a number X. If no such pair exists then output will be -1. static void twoSum(int A[], int X) @@ -70,4 +72,4 @@ Output: 1 7 8 2 6 8 3 5 8 -*/ \ No newline at end of file +*/ From 2ecef7e44a5318b1dbc23171a75edaaee95be34d Mon Sep 17 00:00:00 2001 From: Varun Vaibhav Jha <60656060+varunvjha@users.noreply.github.com> Date: Thu, 17 Sep 2020 11:15:14 +0530 Subject: [PATCH 3/5] Update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 85c876250..d52a9543d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -198,6 +198,7 @@ * [StringMatchFiniteAutomata](https://github.com/TheAlgorithms/Java/blob/master/Others/StringMatchFiniteAutomata.java) * [TopKWords](https://github.com/TheAlgorithms/Java/blob/master/Others/TopKWords.java) * [TowerOfHanoi](https://github.com/TheAlgorithms/Java/blob/master/Others/TowerOfHanoi.java) + * [TwoPointersAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/Others/TwoPointersAlgo.java) * [WorstFit](https://github.com/TheAlgorithms/Java/blob/master/Others/WorstFit.java) ## ProjectEuler From 47ad7ec9b32be9dc8ae225dfb12cdef0c29724d7 Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 17 Sep 2020 15:19:53 +0800 Subject: [PATCH 4/5] reformat code --- Others/TwoPointers.java | 50 +++++++++++++++++++++++++ Others/TwoPointersAlgo.java | 75 ------------------------------------- 2 files changed, 50 insertions(+), 75 deletions(-) create mode 100644 Others/TwoPointers.java delete mode 100644 Others/TwoPointersAlgo.java diff --git a/Others/TwoPointers.java b/Others/TwoPointers.java new file mode 100644 index 000000000..99040b552 --- /dev/null +++ b/Others/TwoPointers.java @@ -0,0 +1,50 @@ +package Others; + +import java.util.Arrays; + +/** + * The two pointer technique is a useful tool to utilize when searching for pairs in a sorted array. + *

+ * link: https://www.geeksforgeeks.org/two-pointers-technique/ + */ +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. + * + * @param arr the array contains elements + * @param key the number to search + * @return {@code true} if there exists a pair of elements, {@code false} otherwise. + */ + private static boolean isPairedSum(int[] arr, int key) { + /* array sorting is necessary for this algorithm to function correctly */ + Arrays.sort(arr); + int i = 0; /* index of first element */ + int j = arr.length - 1; /* index of last element */ + + while (i < j) { + if (arr[i] + arr[j] == key) { + return true; + } else if (arr[i] + arr[j] < key) { + i++; + } else { + j--; + } + } + return false; + } +} \ No newline at end of file diff --git a/Others/TwoPointersAlgo.java b/Others/TwoPointersAlgo.java deleted file mode 100644 index 903c9d79c..000000000 --- a/Others/TwoPointersAlgo.java +++ /dev/null @@ -1,75 +0,0 @@ -import java.util.*; -import java.lang.*; -import java.io.*; - -//https://www.geeksforgeeks.org/two-pointers-technique/ - -class TwoPointersAlgo { - //This function prints all pairs in the array that sum to a number X. If no such pair exists then output will be -1. - static void twoSum(int A[], int X) - { - Arrays.sort(A); - - //Array sorting is necessary for this algo to function correctly - - int n = A.length; - int i = 0, j = n-1, flag=0; - //Implementation of the algorithm starts - while(i0) - { - t--; - n = in.nextInt(); - int a[] = new int[n]; - for(int i = 0; i Date: Thu, 17 Sep 2020 15:27:02 +0800 Subject: [PATCH 5/5] remove link --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index d52a9543d..85c876250 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -198,7 +198,6 @@ * [StringMatchFiniteAutomata](https://github.com/TheAlgorithms/Java/blob/master/Others/StringMatchFiniteAutomata.java) * [TopKWords](https://github.com/TheAlgorithms/Java/blob/master/Others/TopKWords.java) * [TowerOfHanoi](https://github.com/TheAlgorithms/Java/blob/master/Others/TowerOfHanoi.java) - * [TwoPointersAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/Others/TwoPointersAlgo.java) * [WorstFit](https://github.com/TheAlgorithms/Java/blob/master/Others/WorstFit.java) ## ProjectEuler