Form an array of distinct elements with each element as sum of an element from each array Last Updated : 16 Oct, 2022 Comments Improve Suggest changes Like Article Like Report Given two arrays, arr1[] and arr2[] of equal size, which contain 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 Explanation: 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. 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 an increasing or decreasing order.Initialize another array(say arr3[]) to store the distinct elements formed by the sum of two elements of the arrayIterate over a loop from 0 to the length of the arrayElements 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 <bits/stdc++.h> 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 <script> // javascript 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 function thirdArray(a, b, n) { var c = new Array(n); a = a.sort(function(a, b) { return a - b; }); b = b.sort(function(a, b) { return a - b; }); var i,j; // Loop to find the array // such that each element is // sum of other two elements for (i = 0; i < n; i++) { c[i] = a[i] + b[i]; } // Loop to print the array for (i = 0; i < n; i++) document.write(c[i] + " "); } // Driver code var a = [1, 7, 8, 3]; var b = [6, 5, 10, 2]; var size = a.length; thirdArray(a, b, size); </script> 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).Auxiliary Space: As in the above approach, extra space for array c is being used, Hence the space complexity will be O(N). Comment More infoAdvertise with us Next Article Form an array of distinct elements with each element as sum of an element from each array N nitinkr8991 Follow Improve Article Tags : Misc DSA Arrays Practice Tags : ArraysMisc Similar Reads Count all distinct pairs of repeating elements from the array for every array element Given an array arr[] of N integers. For each element in the array, the task is to count the possible pairs (i, j), excluding the current element, such that i < j and arr[i] = arr[j]. Examples: Input: arr[] = {1, 1, 2, 1, 2}Output: 2 2 3 2 3Explanation:For index 1, remaining elements after excludi 7 min read Maximize distinct elements of Array by combining two elements or splitting an element Given an array arr[] of length N, the task is to maximize the number of distinct elements in the array by performing either of the following operations, any number of times: For an index i(0 ⤠i < N), replace arr[i] with a and b such that arr[i] = a + b.For two indices i (0 ⤠i < N) and n (0 ⤠9 min read Construct a distinct elements array with given size, sum and element upper bound Given N, size of the original array, SUM total sum of all elements present in the array and K such that no element in array is greater than K, construct the original array where all elements in the array are unique. If there is no solution, print "Not Possible". Note: All elements should be positive 10 min read Count array elements that can be represented as sum of at least two consecutive array elements Given an array A[] consisting of N integers from a range [1, N], the task is to calculate the count of array elements (non-distinct) that can be represented as the sum of two or more consecutive array elements. Examples: Input: a[] = {3, 1, 4, 1, 5, 9, 2, 6, 5}Output: 5Explanation:The array elements 11 min read Check if sum can be formed by selecting an element for each index from two Arrays Given two arrays arr1[] and arr2[] of size N each. Given target sum M, Choose either a[i] or b[i] for each i where (0 ? i < N), the task is to check if it is possible to achieve the target sum print "Yes" otherwise "No". Examples: Input: arr1[] = {3, 4}, arr2[] = {6, 5}, M = 10Output: YesExplanat 15+ min read Sum of distinct elements when elements are in range 1 to n Given an array of n elements such that every element of the array is an integer in the range 1 to n, find the sum of all the distinct elements of the array. Examples: Input: arr[] = {5, 1, 2, 4, 6, 7, 3, 6, 7} Output: 28 The distinct elements in the array are 1, 2, 3, 4, 5, 6, 7 Input: arr[] = {1, 1 5 min read Find original array from encrypted array (An array of sums of other elements) Find original array from a given encrypted array of size n. Encrypted array is obtained by replacing each element of the original array by the sum of the remaining array elements. Examples : Input : arr[] = {10, 14, 12, 13, 11} Output : {5, 1, 3, 2, 4} Original array {5, 1, 3, 2, 4} Encrypted array 5 min read Find original Array from given Array where each element is sum of prefix and postfix sum Given an array arr[] of length N, where arr is derived from an array nums[] which is lost. Array arr[] is derived as: arr[i] = (nums[0] + nums[1] + ... + nums[i]) + (nums[i] + nums[i+1] + ... + nums[N-1]). The task is to find nums[] array of length N. Examples: Input: N = 4, arr[] = {9, 10, 11, 10}O 10 min read Maximum sum of K-length subarray consisting of same number of distinct elements as the given array Given an array arr[] consisting of N integers and an integer K, the task is to find a subarray of size K with maximum sum and count of distinct elements same as that of the original array. Examples: Input: arr[] = {7, 7, 2, 4, 2, 7, 4, 6, 6, 6}, K = 6Output: 31Explanation: The given array consists o 15+ min read Find pair i, j such that |A[i]âA[j]| is same as sum of difference of them with any Array element Given an array A[] having N non-negative integers, find a pair of indices i and j such that the absolute difference between them is same as the sum of differences of those with any other array element i.e., | A[i] â A[k] | + | A[k]â A[j] | = | A[i] â A[j] |, where k can be any index. Examples: Input 7 min read Like