# Form an array of distinct elements with each element as sum of an element from each array

Given two arrays arr1[] and arr2[] of equal size which contains distinct elements, the task is to find another array with distinct elements such that elements of the third array are formed by the addition of the one-one element of the arr1[] and arr2[]

Examples:

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 is 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 is distinct.

Approach: The key observation in this problem is that both arrays contain distinct elements and If we sort the array then the sum of corresponding elements of the array will also form distinct elements.

The step-by-step algorithm for the above approach is described below –

• Sort both the arrays in such in increasing or decreasing order.
• Intialize another array(say arr3[]) to store the distinct elements formed by sum of two elements of the array
• Iterate over a loop from 0 to length of the array
• Elements of the third array will be the sum of the elements of the first two arrays –
` arr3[i] = arr1[i] + arr2[i]`

Below is the implementation of the above approach:

## 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); ` ` `  `    ``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 `

## Python

 `# 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 prthe 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``;; ` `        ``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 `

Output:

```3 8 13 18
```

Performance Analysis:

• Time Complexity: As in the above approach, there is sorting the array of size N which takes O(N*logN) time, Hence the Time Complexity will be O(N*logN).
• Space Complexity: As in the above approach, there is no extra space used, Hence the space complexity will be O(1).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Interns at Geeksforgeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.