From 4d301033b179c0706278f2241be00e35a80e4c9d Mon Sep 17 00:00:00 2001 From: varunvjha Date: Thu, 17 Sep 2020 10:58:26 +0530 Subject: [PATCH] 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 00000000..31e2b367 --- /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