📌  相关文章
📜  形成一个不同元素的数组,每个元素作为每个数组中一个元素的总和

📅  最后修改于: 2021-09-04 08:11:25             🧑  作者: Mango

给定两个相同大小的数组arr1[]arr2[] ,它们包含不同的元素,任务是找到另一个具有不同元素的数组,使得第三个数组的元素由arr1[] 和 arr2[]。

例子:

方法:这个问题的关键观察是两个数组都包含不同的元素,如果我们对数组进行排序,那么数组中对应元素的总和也会形成不同的元素。
上述方法的分步算法如下所述-

  • 按升序或降序对两个数组进行排序。
  • 初始化另一个数组(比如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