Java Program for Two Pointers Technique Last Updated : 22 Dec, 2023 Comments Improve Suggest changes Like Article Like Report Two pointers is really an easy and effective technique which is typically used for searching pairs in a sorted array.Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X. Let’s see the naive solution. Java import java.io.*; class GFG { public static void main(String[] args) { int arr[] = { 3, 5, 9, 2, 8, 10, 11 }; int val = 17; System.out.println(isPairSum(arr, arr.length, val)); } private static int isPairSum(int A[], int N, int X) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { // Skip the same element (i and j are equal) if (i == j) continue; // Check if the sum of the pair equals X if (A[i] + A[j] == X) return 1; // Pair exists // Since the array is sorted, if the sum exceeds X, break the inner loop if (A[i] + A[j] > X) break; } } // No pair found with the given sum return 0; } } Output1 Time Complexity: O(n2). Auxiliary Space: O(1) Now let’s see how the two-pointer technique works. We take two pointers, one representing the first element and other representing the last element of the array, and then we add the values kept at both the pointers. If their sum is smaller than X then we shift the left pointer to right or if their sum is greater than X then we shift the right pointer to left, in order to get closer to the sum. We keep moving the pointers until we get the sum as X. Java import java.io.*; class GFG { // Two pointer technique based solution to find // if there is a pair in A[0..N-1] with a given sum. public static int isPairSum(int A[], int N, int X) { // represents first pointer int i = 0; // represents second pointer int j = N - 1; while (i < j) { // If we find a pair if (A[i] + A[j] == X) return 1; // If sum of elements at current // pointers is less, we move towards // higher values by doing i++ else if (A[i] + A[j] < X) i++; // If sum of elements at current // pointers is more, we move towards // lower values by doing j-- else j--; } return 0; } // Driver code public static void main(String[] args) { // array declaration int arr[] = { 3, 5, 9, 2, 8, 10, 11 }; // value to search int val = 17; // size of the array int arrSize = arr.length; // Function call System.out.println(isPairSum(arr, arrSize, val)); } } Output1 Illustration : Time Complexity: O(n) Auxiliary Space: O(1) since using constant space How does this work? The algorithm basically uses the fact that the input array is sorted. We start the sum of extreme values (smallest and largest) and conditionally move both pointers. We move left pointer i when the sum of A[i] and A[j] is less than X. We do not miss any pair because the sum is already smaller than X. Same logic applies for right pointer j. More problems based on two pointer technique. Find the closest pair from two sorted arraysFind the pair in array whose sum is closest to xFind all triplets with zero sumFind a triplet that sum to a given valueFind a triplet such that sum of two equals to third elementFind four elements that sum to a given valuePlease refer complete article on Two Pointers Technique for more details! Comment More infoAdvertise with us Next Article C Program for Two Pointers Technique kartik Follow Improve Article Tags : Java Searching Technical Scripter Java Programs DSA Arrays two-pointer-algorithm +3 More Practice Tags : ArraysJavaSearchingtwo-pointer-algorithm Similar Reads Two Pointers Technique Two pointers is really an easy and effective technique that is typically used for Two Sum in Sorted Arrays, Closest Two Sum, Three Sum, Four Sum, Trapping Rain Water and many other popular interview questions. Given a sorted array arr (sorted in ascending order) and a target, find if there exists an 11 min read Two Pointers Technique in Different languagesJava Program for Two Pointers Technique Two pointers is really an easy and effective technique which is typically used for searching pairs in a sorted array.Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X. Letâs see the naive so 4 min read C Program for Two Pointers Technique Two pointers is really an easy and effective technique which is typically used for searching pairs in a sorted array.Given a sorted array A (sorted in ascending order), having N integers, find if there exists any pair of elements (A[i], A[j]) such that their sum is equal to X. Letâs see the naive so 3 min read Problems based on Two-Pointer TechniqueFind four elements that sum to a given value | Two-Pointer approach Given an array arr of integers of size N and a target number, the task is to find all unique quadruplets in it, whose sum is equal to the target number. Examples:Input: arr[] = {4, 1, 2, -1, 1, -3], target = 1 Output: [[-3, -1, 1, 4], [-3, 1, 1, 2]] Explanation: Both the quadruplets sum equals to ta 10 min read Rearrange an array in maximum minimum form using Two Pointer Technique Given a sorted array of positive integers, rearrange the array alternately i.e first element should be a maximum value, at second position minimum value, at third position second max, at fourth position second min, and so on. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2 6 min read Pair with largest sum which is less than K in the array Given an array arr of size N and an integer K. The task is to find the pair of integers such that their sum is maximum and but less than K Examples: Input : arr = {30, 20, 50} , K = 70 Output : 30, 20 30 + 20 = 50, which is the maximum possible sum which is less than K Input : arr = {5, 20, 110, 100 10 min read Longest increasing sequence by the boundary elements of an Array Given an array arr[] of length N with unique elements, the task is to find the length of the longest increasing subsequence that can be formed by the elements from either end of the array.Examples: Input: arr[] = {3, 5, 1, 4, 2} Output: 4 Explanation: The longest sequence is: {2, 3, 4, 5} Pick 2, Se 8 min read Two Sum - Pair with given Sum Given an array arr[] of n integers and a target value, the task is to find whether there is a pair of elements in the array whose sum is equal to target. This problem is a variation of 2Sum problem.Examples: Input: arr[] = [0, -1, 2, -3, 1], target = -2Output: trueExplanation: There is a pair (1, -3 15+ min read Like