```// Java program to find a triplet
class FindTriplet {

// returns true if there is triplet with sum equal
// to 'sum' present in A[]. Also, prints the triplet
boolean find3Numbers(int A[], int arr_size, int sum)
{
int l, r;

/* Sort the elements */
quickSort(A, 0, arr_size - 1);

/* Now fix the first element one by one and find the
other two elements */
for (int i = 0; i < arr_size - 2; i++) {

// To find the other two elements, start two index variables
// from two corners of the array and move them toward each
// other
l = i + 1; // index of the first element in the remaining elements
r = arr_size - 1; // index of the last element
while (l < r) {
if (A[i] + A[l] + A[r] == sum) {
System.out.print("Triplet is " + A[i] + ", " + A[l] + ", " + A[r]);
return true;
}
else if (A[i] + A[l] + A[r] < sum)
l++;

else // A[i] + A[l] + A[r] > sum
r--;
}
}

// If we reach here, then no triplet was found
return false;
}

int partition(int A[], int si, int ei)
{
int x = A[ei];
int i = (si - 1);
int j;

for (j = si; j <= ei - 1; j++) {
if (A[j] <= x) {
i++;
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
int temp = A[i + 1];
A[i + 1] = A[ei];
A[ei] = temp;
return (i + 1);
}

/* Implementation of Quick Sort
A[] --> Array to be sorted
si --> Starting index
ei --> Ending index
*/
void quickSort(int A[], int si, int ei)
{
int pi;

/* Partitioning index */
if (si < ei) {
pi = partition(A, si, ei);
quickSort(A, si, pi - 1);
quickSort(A, pi + 1, ei);
}
}

// Driver program to test above functions
public static void main(String[] args)
{
FindTriplet triplet = new FindTriplet();
int A[] = { 1, 4, 45, 6, 10, 8 };
int sum = 22;
int arr_size = A.length;

triplet.find3Numbers(A, arr_size, sum);    // OUTPUT : Triplet is 4, 8, 10
}
}
```
```import java.util.*;
import java.io.*;

class Solution
{
static int isPresent(int arr[], int n, int sum)
{
int l = 0, h = n-1;

while(l <= h)
{
if(arr[l] + arr[h] == sum)
return 1;
else if(arr[l] + arr[h] > sum)
h--;
else l++;
}

return 0;
}

public static void main (String[] args)
{
int arr[] = new int[]{2, 3, 7, 8, 11};
int n = arr.length;
int sum = 14;

System.out.println(isPresent(arr, n, sum));    // OUTPUT : 1
}
}```
```// Java implementation using Hashing
import java.io.*;
import java.util.HashSet;

class PairSum {

static void printpairs(int arr[], int sum)
{
HashSet<Integer> s = new HashSet<Integer>();
for (int i = 0; i < arr.length; ++i) {
int temp = sum - arr[i];

// checking for condition
if (s.contains(temp)) {
System.out.println("Pair with given sum " + sum + " is (" + arr[i] + ", " + temp + ")");
}
}
}

// Main to test the above function
public static void main(String[] args)
{
int A[] = { 1, 4, 45, 6, 10, 8 };
int n = 16;
printpairs(A, n);
}
}

// OUTPUT : Pair with given sum 16 is (10, 6)```
star

Tue Feb 08 2022 14:19:02 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #searching #twopointer #sortedarray #quicksort
star

Tue Feb 08 2022 14:16:08 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #searching #twopointer #sortedarray