Given an array arr[] consisting of N integers, and an integer X, the task is to find two elements from the array arr[] having sum X. If there doesn’t exist such numbers, then print “-1”.
Examples:
Input: arr[] = {0, -1, 2, -3, 1}, X = -2
Output: -3, 1
Explanation:
From the given array the sum of -3 and 1 is equal to -2 (= X).
Input: arr[] = {1, -2, 1, 0, 5}, X = 0
Output: -1
Approach: The given problem can be solved by using sorting and binary search, The idea is to sort the array A[] and for each array element A[i], search whether there is another value (X – A[i]) present in the array or not. Follow the steps below to solve the problem:
- Sort the given array arr[] in increasing order.
- Traverse the array arr[] and for each array element A[i], initialize two variables low and high as 0 and (N – 1) respectively. Now, perform the Binary Search as per the following steps:
- If the value at index mid in the array arr[] is (X – A[i]), then print this current pair and break out of the loop.
- Update mid as (low + high)/2.
- If the value of A[mid] is less than X, then update low as (mid + 1). Otherwise, update high as (mid – 1).
- After completing the above steps, if no such pair is found, then print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void hasArrayTwoPairs( int nums[],
int n, int target)
{
sort(nums, nums + n);
for ( int i = 0; i < n; i++) {
int x = target - nums[i];
int low = 0, high = n - 1;
while (low <= high) {
int mid = low
+ ((high - low) / 2);
if (nums[mid] > x) {
high = mid - 1;
}
else if (nums[mid] < x) {
low = mid + 1;
}
else {
if (mid == i) {
if ((mid - 1 >= 0)
&& nums[mid - 1] == x) {
cout << nums[i] << ", " ;
cout << nums[mid - 1];
return ;
}
if ((mid + 1 < n)
&& nums[mid + 1] == x) {
cout << nums[i] << ", " ;
cout << nums[mid + 1];
return ;
}
break ;
}
else {
cout << nums[i] << ", " ;
cout << nums[mid];
return ;
}
}
}
}
cout << -1;
}
int main()
{
int A[] = { 0, -1, 2, -3, 1 };
int X = -2;
int N = sizeof (A) / sizeof (A[0]);
hasArrayTwoPairs(A, N, X);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void hasArrayTwoPairs( int nums[],
int n, int target)
{
Arrays.sort(nums);
for ( int i = 0 ; i < n; i++) {
int x = target - nums[i];
int low = 0 , high = n - 1 ;
while (low <= high) {
int mid = low
+ ((high - low) / 2 );
if (nums[mid] > x) {
high = mid - 1 ;
}
else if (nums[mid] < x) {
low = mid + 1 ;
}
else {
if (mid == i) {
if ((mid - 1 >= 0 )
&& nums[mid - 1 ] == x) {
System.out.print(nums[i] + ", " );
System.out.print( nums[mid - 1 ]);
return ;
}
if ((mid + 1 < n)
&& nums[mid + 1 ] == x) {
System.out.print( nums[i] + ", " );
System.out.print( nums[mid + 1 ]);
return ;
}
break ;
}
else {
System.out.print( nums[i] + ", " );
System.out.print(nums[mid]);
return ;
}
}
}
}
System.out.print(- 1 );
}
public static void main(String[] args)
{
int A[] = { 0 , - 1 , 2 , - 3 , 1 };
int X = - 2 ;
int N = A.length;
hasArrayTwoPairs(A, N, X);
}
}
|
Python3
def hasArrayTwoPairs(nums, n, target):
nums = sorted (nums)
for i in range (n):
x = target - nums[i]
low, high = 0 , n - 1
while (low < = high):
mid = low + ((high - low) / / 2 )
if (nums[mid] > x):
high = mid - 1
elif (nums[mid] < x):
low = mid + 1
else :
if (mid = = i):
if ((mid - 1 > = 0 ) and nums[mid - 1 ] = = x):
print (nums[i], end = ", " )
print (nums[mid - 1 ])
return
if ((mid + 1 < n) and nums[mid + 1 ] = = x):
print (nums[i], end = ", " )
print (nums[mid + 1 ])
return
break
else :
print (nums[i], end = ", " )
print (nums[mid])
return
print ( - 1 )
if __name__ = = '__main__' :
A = [ 0 , - 1 , 2 , - 3 , 1 ]
X = - 2
N = len (A)
hasArrayTwoPairs(A, N, X)
|
C#
using System;
public class GFG
{
static void hasArrayTwoPairs( int [] nums, int n, int target)
{
Array.Sort(nums);
for ( int i = 0; i < n; i++)
{
int x = target - nums[i];
int low = 0, high = n - 1;
while (low <= high)
{
int mid = low + ((high - low) / 2);
if (nums[mid] > x) {
high = mid - 1;
}
else if (nums[mid] < x) {
low = mid + 1;
}
else {
if (mid == i) {
if ((mid - 1 >= 0) && nums[mid - 1] == x) {
Console.Write(nums[i] + ", " );
Console.Write( nums[mid - 1]);
return ;
}
if ((mid + 1 < n) && nums[mid + 1] == x) {
Console.Write( nums[i] + ", " );
Console.Write( nums[mid + 1]);
return ;
}
break ;
}
else {
Console.Write( nums[i] + ", " );
Console.Write(nums[mid]);
return ;
}
}
}
}
Console.Write(-1);
}
static public void Main (){
int [] A = { 0, -1, 2, -3, 1 };
int X = -2;
int N = A.Length;
hasArrayTwoPairs(A, N, X);
}
}
|
Javascript
<script>
function hasArrayTwoPairs(nums, n, target)
{
nums.sort();
var i;
for (i = 0; i < n; i++) {
var x = target - nums[i];
var low = 0, high = n - 1;
while (low <= high) {
var mid = low
+ (Math.floor((high - low) / 2));
if (nums[mid] > x) {
high = mid - 1;
}
else if (nums[mid] < x) {
low = mid + 1;
}
else {
if (mid == i) {
if ((mid - 1 >= 0)
&& nums[mid - 1] == x) {
document.write(nums[i] + ", " );
document.write(nums[mid - 1]);
return ;
}
if ((mid + 1 < n) && nums[mid + 1] == x) {
document.write(nums[i] + ", " );
document.write(nums[mid + 1]);
return ;
}
break ;
}
else {
document.write(nums[i] + ", " );
document.write(nums[mid]);
return ;
}
}
}
}
document.write(-1);
}
var A = [0, -1, 2, -3, 1];
var X = -2;
var N = A.length;
hasArrayTwoPairs(A, N, X);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Alternate Approaches: Refer to the previous post of this article to know about more approaches to solve this problem.