给定由N个整数和整数X组成的数组arr [] ,任务是从数组arr []中找到两个具有总和X的元素。如果不存在这样的数字,则打印“ -1” 。
例子:
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
方法:可以通过使用排序和二进制搜索来解决给定的问题,其思想是对数组A []进行排序,并针对每个数组元素A [i] ,搜索是否存在另一个值(X – A [i])是否在数组中。请按照以下步骤解决问题:
- 按给定数组arr []的升序排序。
- 遍历数组arr []并为每个数组元素A [i]初始化两个变量low和high分别为0和(N – 1) 。现在,按照以下步骤执行二进制搜索:
- 如果数组arr []中索引中间的值为(X – A [i]) ,则打印此当前对并退出循环。
- 更新作为中期(低+高)/ 2。
- 如果A [MID]的值小于X,然后更新低至(1中间+)。否则,更新高(中- 1)。
- 完成上述步骤后,如果找不到这样的对,则打印-1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the array has
// 2 elements whose sum is equal to
// the given value
void hasArrayTwoPairs(int nums[],
int n, int target)
{
// Sort the array in increasing order
sort(nums, nums + n);
// Traverse the array, nums[]
for (int i = 0; i < n; i++) {
// Store the required number
// to be found
int x = target - nums[i];
// Perform binary search
int low = 0, high = n - 1;
while (low <= high) {
// Store the mid value
int mid = low
+ ((high - low) / 2);
// If nums[mid] is greater
// than x, then update
// high to mid - 1
if (nums[mid] > x) {
high = mid - 1;
}
// If nums[mid] is less
// than x, then update
// low to mid + 1
else if (nums[mid] < x) {
low = mid + 1;
}
// Otherwise
else {
// If mid is equal i,
// check mid-1 and mid+1
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;
}
// Otherwise, print the
// pair and return
else {
cout << nums[i] << ", ";
cout << nums[mid];
return;
}
}
}
}
// If no such pair is found,
// then print -1
cout << -1;
}
// Driver Code
int main()
{
int A[] = { 0, -1, 2, -3, 1 };
int X = -2;
int N = sizeof(A) / sizeof(A[0]);
// Function Call
hasArrayTwoPairs(A, N, X);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if the array has
// 2 elements whose sum is equal to
// the given value
static void hasArrayTwoPairs(int nums[],
int n, int target)
{
// Sort the array in increasing order
Arrays.sort(nums);
// Traverse the array, nums[]
for (int i = 0; i < n; i++) {
// Store the required number
// to be found
int x = target - nums[i];
// Perform binary search
int low = 0, high = n - 1;
while (low <= high) {
// Store the mid value
int mid = low
+ ((high - low) / 2);
// If nums[mid] is greater
// than x, then update
// high to mid - 1
if (nums[mid] > x) {
high = mid - 1;
}
// If nums[mid] is less
// than x, then update
// low to mid + 1
else if (nums[mid] < x) {
low = mid + 1;
}
// Otherwise
else {
// If mid is equal i,
// check mid-1 and mid+1
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;
}
// Otherwise, print the
// pair and return
else {
System.out.print( nums[i] + ", ");
System.out.print(nums[mid]);
return;
}
}
}
}
// If no such pair is found,
// then print -1
System.out.print(-1);
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 0, -1, 2, -3, 1 };
int X = -2;
int N = A.length;
// Function Call
hasArrayTwoPairs(A, N, X);
}
}
// This code is contributed by code_hunt.
Python3
# Python3 program for the above approach
# Function to check if the array has
# 2 elements whose sum is equal to
# the given value
def hasArrayTwoPairs(nums, n, target):
# Sort the array in increasing order
nums = sorted(nums)
# Traverse the array, nums[]
for i in range(n):
# Store the required number
# to be found
x = target - nums[i]
# Perform binary search
low, high = 0, n - 1
while (low <= high):
# Store the mid value
mid = low + ((high - low) // 2)
# If nums[mid] is greater
# than x, then update
# high to mid - 1
if (nums[mid] > x):
high = mid - 1
# If nums[mid] is less
# than x, then update
# low to mid + 1
elif (nums[mid] < x):
low = mid + 1
# Otherwise
else:
# If mid is equal i,
# check mid-1 and mid+1
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
# Otherwise, prthe
# pair and return
else:
print(nums[i], end = ", ")
print(nums[mid])
return
# If no such pair is found,
# then pr-1
print (-1)
# Driver Code
if __name__ == '__main__':
A = [0, -1, 2, -3, 1]
X = -2
N = len(A)
# Function Call
hasArrayTwoPairs(A, N, X)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to check if the array has
// 2 elements whose sum is equal to
// the given value
static void hasArrayTwoPairs(int[] nums, int n, int target)
{
// Sort the array in increasing order
Array.Sort(nums);
// Traverse the array, nums[]
for (int i = 0; i < n; i++)
{
// Store the required number
// to be found
int x = target - nums[i];
// Perform binary search
int low = 0, high = n - 1;
while (low <= high)
{
// Store the mid value
int mid = low + ((high - low) / 2);
// If nums[mid] is greater
// than x, then update
// high to mid - 1
if (nums[mid] > x) {
high = mid - 1;
}
// If nums[mid] is less
// than x, then update
// low to mid + 1
else if (nums[mid] < x) {
low = mid + 1;
}
// Otherwise
else {
// If mid is equal i,
// check mid-1 and mid+1
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;
}
// Otherwise, print the
// pair and return
else {
Console.Write( nums[i] + ", ");
Console.Write(nums[mid]);
return;
}
}
}
}
// If no such pair is found,
// then print -1
Console.Write(-1);
}
// Driver Code
static public void Main (){
int[] A = { 0, -1, 2, -3, 1 };
int X = -2;
int N = A.Length;
// Function Call
hasArrayTwoPairs(A, N, X);
}
}
// This code is contributed by avanitrachhadiya2155
输出:
-3, 1
时间复杂度: O(N * log N)
辅助空间: O(1)
替代方法:请参阅本文的上一篇文章,以了解更多解决此问题的方法。