reformat code
This commit is contained in:
parent
2ecef7e44a
commit
47ad7ec9b3
50
Others/TwoPointers.java
Normal file
50
Others/TwoPointers.java
Normal file
@ -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.
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
}
|
@ -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(i<j)
|
||||
{
|
||||
if(A[i]+A[j]==X)
|
||||
{
|
||||
System.out.println(A[i] + " " + A[j] + " " + X);
|
||||
flag = 1;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
else if(A[i]+A[j]<X)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
else j--;
|
||||
}
|
||||
|
||||
//Implementation ends
|
||||
|
||||
if(flag==0)
|
||||
System.out.println(-1);
|
||||
}//end of function twoSum
|
||||
public static void main (String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
int t,n;
|
||||
t = in.nextInt();
|
||||
//for number of test cases
|
||||
while(t>0)
|
||||
{
|
||||
t--;
|
||||
n = in.nextInt();
|
||||
int a[] = new int[n];
|
||||
for(int i = 0; i<n; i++)
|
||||
{
|
||||
a[i] = in.nextInt();
|
||||
}
|
||||
//taking array input
|
||||
int k = in.nextInt(); //the total of the pair entered by the user
|
||||
twoSum(a, k);
|
||||
|
||||
}
|
||||
}//end of main
|
||||
}//end of class
|
||||
|
||||
/*Sample Input/Output
|
||||
Input:
|
||||
2
|
||||
7
|
||||
1 2 3 4 5 6 7
|
||||
98
|
||||
7
|
||||
1 2 3 4 5 6 7
|
||||
8
|
||||
Output:
|
||||
-1
|
||||
1 7 8
|
||||
2 6 8
|
||||
3 5 8
|
||||
*/
|
Loading…
Reference in New Issue
Block a user