C ++程序找到一个三元组,使得两个之和等于第三个元素
给定一个整数数组,你必须找到三个数字,使得两个元素的和等于第三个元素。
例子:
Input: {5, 32, 1, 7, 10, 50, 19, 21, 2}
Output: 21, 2, 19
Input: {5, 32, 1, 7, 10, 50, 19, 21, 0}
Output: no such triplet exist
问题来源:Arcesium 面试经历 | Set 7 (校内实习)
简单的方法:运行三个循环并检查是否存在一个三元组,使得两个元素的和等于第三个元素。
时间复杂度:O(n^3)
高效方法:这个想法类似于 Find a triplet that sum to a given value。
- 首先对给定的数组进行排序。
- 从后面开始固定三个中最大的元素并遍历数组以找到其他两个数字,它们的总和为第三个元素。
- 取两个指针 j(从前面)和 k(最初是 i-1)找到两个数字中的最小者,并从 i-1 找到剩余两个数字中的最大者
- 如果两个数之和仍然小于A[i],那么我们需要增加两个数之和的值,从而增加j指针,从而增加A[j] + A[ ķ] 。
- 如果两个数之和大于A[i],则需要减小两个数之和的值,从而减小k指针,从而减小A[j] + A[k的总和] .
下图是上述方法的试运行:
下面是上述方法的实现:
C++
// C++ program to find three numbers
// such that sum of two makes the
// third element in array
#include
using namespace std;
// Utility function for finding
// triplet in array
void findTriplet(int arr[], int n)
{
// Sort the array
sort(arr, arr + n);
// For every element in arr check
// if a pair exist(in array) whose
// sum is equal to arr element
for (int i = n - 1; i >= 0; i--)
{
int j = 0;
int k = i - 1;
// Iterate forward and backward to
// find the other two elements
while (j < k)
{
// If the two elements sum is
// equal to the third element
if (arr[i] == arr[j] + arr[k])
{
// Pair found
cout << "numbers are " << arr[i] <<
" " << arr[j] << " " <<
arr[k] << endl;
return;
}
// If the element is greater than
// sum of both the elements, then try
// adding a smaller number to reach the
// equality
else if (arr[i] > arr[j] + arr[k])
j += 1;
// If the element is smaller, then
// try with a smaller number
// to reach equality, so decrease K
else
k -= 1;
}
}
// No such triplet is found in array
cout << "No such triplet exists";
}
// Driver code
int main()
{
int arr[] = {5, 32, 1, 7, 10,
50, 19, 21, 2};
int n = sizeof(arr) / sizeof(arr[0]);
findTriplet(arr, n);
return 0;
}
C++
// C++ program to find three numbers
// such that sum of two makes the
// third element in array
#include
#include
using namespace std;
// Function to perform binary search
bool search(int sum, int start,
int end, int arr[])
{
while (start <= end)
{
int mid = (start + end) / 2;
if (arr[mid] == sum)
{
return true;
}
else if (arr[mid] > sum)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return false;
}
// Function to find the triplets
void findTriplet(int arr[], int n)
{
// Sorting the array
sort(arr, arr + n);
// Initialising nested loops
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
// Finding the sum of the numbers
if (search((arr[i] + arr[j]),
j, n - 1, arr))
{
// Printing out the first triplet
cout << "Numbers are: " << arr[i] <<
" " << arr[j] << " " <<
(arr[i] + arr[j]);
return;
}
}
}
// If no such triplets are found
cout << "No such numbers exist" << endl;
}
// Driver code
int main()
{
int arr[] = {5, 32, 1, 7, 10,
50, 19, 21, 2};
int n = sizeof(arr) / sizeof(arr[0]);
findTriplet(arr, n);
return 0;
}
// This code is contributed by Sarthak Delori
输出:
numbers are 21 2 19
时间复杂度:O(N^2)
另一种方法:这个想法类似于以前的方法。
- 对给定的数组进行排序。
- 开始一个嵌套循环,固定第一个元素 i(从 0 到 n-1)并移动另一个元素 j(从 i+1 到 n-1)。
- 取两个元素的总和,并使用二分搜索在剩余的数组中搜索它。
C++
// C++ program to find three numbers
// such that sum of two makes the
// third element in array
#include
#include
using namespace std;
// Function to perform binary search
bool search(int sum, int start,
int end, int arr[])
{
while (start <= end)
{
int mid = (start + end) / 2;
if (arr[mid] == sum)
{
return true;
}
else if (arr[mid] > sum)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
return false;
}
// Function to find the triplets
void findTriplet(int arr[], int n)
{
// Sorting the array
sort(arr, arr + n);
// Initialising nested loops
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
// Finding the sum of the numbers
if (search((arr[i] + arr[j]),
j, n - 1, arr))
{
// Printing out the first triplet
cout << "Numbers are: " << arr[i] <<
" " << arr[j] << " " <<
(arr[i] + arr[j]);
return;
}
}
}
// If no such triplets are found
cout << "No such numbers exist" << endl;
}
// Driver code
int main()
{
int arr[] = {5, 32, 1, 7, 10,
50, 19, 21, 2};
int n = sizeof(arr) / sizeof(arr[0]);
findTriplet(arr, n);
return 0;
}
// This code is contributed by Sarthak Delori
时间复杂度: O(N^2*log N)
空间复杂度: O(1)
有关更多详细信息,请参阅有关查找三元组的完整文章,以使两个之和等于第三个元素!