Java Program for Given a sorted and rotated array, find if there is a pair with a given sum
Given an array that is sorted and then rotated around an unknown point. Find if the array has a pair with a given sum ‘x’. It may be assumed that all elements in the array are distinct.
Examples :
Input: arr[] = {11, 15, 6, 8, 9, 10}, x = 16
Output: true
There is a pair (6, 10) with sum 16
Input: arr[] = {11, 15, 26, 38, 9, 10}, x = 35
Output: true
There is a pair (26, 9) with sum 35
Input: arr[] = {11, 15, 26, 38, 9, 10}, x = 45
Output: false
There is no pair with sum 45.
We have discussed a O(n) solution for a sorted array (See steps 2, 3 and 4 of Method 1). We can extend this solution for rotated array as well. The idea is to first find the largest element in array which is the pivot point also and the element just after largest is the smallest element. Once we have indexes largest and smallest elements, we use similar meet in middle algorithm (as discussed here in method 1) to find if there is a pair. The only thing new here is indexes are incremented and decremented in rotational manner using modular arithmetic.
Following is the implementation of above idea.
Java
import java.io.*;
class GFG
{
static int pairsInSortedRotated( int arr[],
int n, int x)
{
int i;
for (i = 0 ; i < n - 1 ; i++)
if (arr[i] > arr[i + 1 ])
break ;
int l = (i + 1 ) % n;
int r = i;
int cnt = 0 ;
while (l != r)
{
if (arr[l] + arr[r] == x)
{
cnt++;
if (l == (r - 1 + n) % n)
{
return cnt;
}
l = (l + 1 ) % n;
r = (r - 1 + n) % n;
}
else if (arr[l] + arr[r] < x)
l = (l + 1 ) % n;
else
r = (n + r - 1 ) % n;
}
return cnt;
}
public static void main (String[] args)
{
int arr[] = { 11 , 15 , 6 , 7 , 9 , 10 };
int sum = 16 ;
int n = arr.length;
System.out.println(
pairsInSortedRotated(arr, n, sum));
}
}
|
Output :
Array has two elements with sum 16
The time complexity of the above solution is O(n). The step to find the pivot can be optimized to O(Logn) using the Binary Search approach discussed here.
Space complexity: O(1),as we only need a few variables to store intermediate values.
How to count all pairs having sum x?
The stepwise algo is:
- Find the pivot element of the sorted and the rotated array. The pivot element is the largest element in the array. The smallest element will be adjacent to it.
- Use two pointers (say left and right) with the left pointer pointing to the smallest element and the right pointer pointing to largest element.
- Find the sum of the elements pointed by both the pointers.
- If the sum is equal to x, then increment the count. If the sum is less than x, then to increase sum move the left pointer to next position by incrementing it in a rotational manner. If the sum is greater than x, then to decrease sum move the right pointer to next position by decrementing it in rotational manner.
- Repeat step 3 and 4 until the left pointer is not equal to the right pointer or until the left pointer is not equal to right pointer – 1.
- Print final count.
Below is implementation of above algorithm:
Output:
2
Time Complexity: O(n)
Auxiliary Space: O(1)
This method is suggested by Nikhil Jindal.
Exercise:
1) Extend the above solution to work for arrays with duplicates allowed.
Please refer complete article on Given a sorted and rotated array, find if there is a pair with a given sum for more details!
Last Updated :
14 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...