给定两个相同大小的数组arr1[]和arr2[] ,它们包含不同的元素,任务是找到另一个具有不同元素的数组,使得第三个数组的元素由arr1[] 和 arr2[]。
例子:
Input: arr[] = {1, 7, 8, 3}, arr2[] = {6, 5, 10, 2}
Output: 3 8 13 18
Explantion:
Index 0: 1 + 2 = 3
Index 1: 3 + 5 = 8
Index 2: 7 + 6 = 13
Index 3: 8 + 10 = 18
The elements of the array are distinct.
Input: arr1[] = {15, 20, 3}, arr2[] = {5, 4, 3}
Output: 6 19 25
Explanation:
Index 0: 3 + 3 = 6
Index 1: 15 + 4 = 19
Index 2: 20 + 5 = 25
The elements of the array are distinct.
方法:这个问题的关键观察是两个数组都包含不同的元素,如果我们对数组进行排序,那么数组中对应元素的总和也会形成不同的元素。
上述方法的分步算法如下所述-
- 按升序或降序对两个数组进行排序。
- 初始化另一个数组(比如arr3[] )以存储由数组的两个元素之和形成的不同元素
- 遍历从 0 到数组长度的循环
- 第三个数组的元素将是前两个数组元素的总和-
arr3[i] = arr1[i] + arr2[i]
下面是上述方法的实现:
C++
// C++ implementation to find distinct
// array such that the elements of the
// array is sum of two
// elements of other two arrays
#include
using namespace std;
// Function to find a distinct array
// such that elements of the third
// array is sum of two elements
// of other two arrays
void thirdArray(int a[], int b[], int n)
{
int c[n];
sort(a, a + n);
sort(b, b + n);
// Loop to find the array
// such that each element is
// sum of other two elements
for (int i = 0; i < n; i++) {
c[i] = a[i] + b[i];
}
// Loop to print the array
for (int i = 0; i < n; i++)
cout << c[i] << " ";
}
// Driver code
int main()
{
int a[] = { 1, 7, 8, 3 };
int b[] = { 6, 5, 10, 2 };
int size = sizeof(a) / sizeof(a[0]);
thirdArray(a, b, size);
return 0;
}
Java
// Java implementation to find distinct
// array such that the elements of the
// array is sum of two
// elements of other two arrays
import java.util.*;
class GFG
{
// Function to find a distinct array
// such that elements of the third
// array is sum of two elements
// of other two arrays
static void thirdArray(int a[], int b[], int n)
{
int[] c = new int[20];;
Arrays.sort(a);
Arrays.sort(b);
// Loop to find the array
// such that each element is
// sum of other two elements
for (int i = 0; i < n; i++) {
c[i] = a[i] + b[i];
}
// Loop to print the array
for (int i = 0; i < n; i++)
System.out.print(c[i] + " ");
}
// Driver code
public static void main(String args[])
{
int a[] = { 1, 7, 8, 3 };
int b[] = { 6, 5, 10, 2 };
int size = a.length;
thirdArray(a, b, size);
}
}
// This code is contributed by shubhamsingh10
Python3
# Python3 implementation to find distinct
# array such that the elements of the
# array is sum of two
# elements of other two arrays
# Function to find a distinct array
# such that elements of the third
# array is sum of two elements
# of other two arrays
def thirdArray(a, b, n):
c = [0]*n
a = sorted(a)
b = sorted(b)
# Loop to find the array
# such that each element is
# sum of other two elements
for i in range(n):
c[i] = a[i] + b[i]
# Loop to print the array
for i in range(n):
print(c[i], end=" ")
# Driver code
a = [1, 7, 8, 3]
b = [6, 5, 10, 2]
size = len(a)
thirdArray(a, b, size)
# This code is contributed by mohit kumar 29
C#
// C# implementation to find distinct
// array such that the elements of the
// array is sum of two
// elements of other two arrays
using System;
class GFG
{
// Function to find a distinct array
// such that elements of the third
// array is sum of two elements
// of other two arrays
static void thirdArray(int []a, int []b, int n)
{
int[] c = new int[20];;
Array.Sort(a);
Array.Sort(b);
// Loop to find the array
// such that each element is
// sum of other two elements
for (int i = 0; i < n; i++) {
c[i] = a[i] + b[i];
}
// Loop to print the array
for (int i = 0; i < n; i++)
Console.Write(c[i] + " ");
}
// Driver code
public static void Main(String []args)
{
int []a = { 1, 7, 8, 3 };
int []b = { 6, 5, 10, 2 };
int size = a.Length;
thirdArray(a, b, size);
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
3 8 13 18
性能分析:
- 时间复杂度:与上述方法一样,对大小为 N 的数组进行排序需要 O(N*logN) 时间,因此时间复杂度将为O(N*logN) 。
- 空间复杂度:与上述方法一样,没有使用额外的空间,因此空间复杂度为O(1) 。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live