- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given two sorted arrays arr1[] and arr2[] of size m and an element N. Our task is to Find Nth item in a set formed by the sum of two arrays.

**Code description **− Here, we will create a set that consists of the sum of element one of arr1 and one of arr2 i.e. sum = arr1[i] + arr2[j], where are i , j < m. For N, we need to find the value of the Nth element of the set.

**Let’s take an example to understand the problem,**

arr1[] = {3, 1, 5} , arr2[] = {6, 2, 8} , N = 4

**Output**

The elements of the set are −

9 (3+6 and 1 +8) , 5 (3 + 2) , 11 (3+8, 5+6), 7 (1+6, 5+2), 3 (1+2), 13 (5+8). The fourth element is 7.

The solution approach is simply finding the elements of the set by finding the sum of elements of the arrays. For this, we will use nested loops, outer loop to iterate elements of arr1 and the inner loop to iterate the elements of arr2. And for each iteration of the inner loop, we will store the sum to the set, which will not allow duplicate elements. After all sum values are feeded, we will traverse the set and return the Nth element.

**Program to illustrate the working of our solution,**

#include <iostream> using namespace std; void calcSumVariables(int sum[], int n) { float SUMX = 0; for (int i = 0; i < n; i++) { SUMX += sum[i]; } SUMX /= (n - 1); for (int i = 0; i < n; i++) cout<<"\nx"<<(i + 1)<<" = "<<(SUMX - sum[i]); } int main(){ int sum[] = {3, 8, 6, 7, 4, 5, 9 }; int N = sizeof(sum) / sizeof(sum[0]); cout<<"The value of variables that form the sum are "; calcSumVariables(sum, N); return 0; }

The value of variables that form the sum are x1 = 4 x2 = -1 x3 = 1 x4 = 0 x5 = 3 x6 = 2 x7 = -2

- Related Questions & Answers
- Sum of all subsets of a set formed by first n natural numbers
- Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++
- n’th Pentagonal Number in C++
- N’th Smart Number in C++
- N’th palindrome of K digits in C++
- K-th Element of Two Sorted Arrays in C++
- Program to find last digit of n’th Fibonnaci Number in C++
- Set 6-item tuple in C#’
- Find n-th element from Stern’s Diatomic Series in C++
- Find n-th lexicographically permutation of a strings in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- Find size of the largest ‘+’ formed by all ones in a binary matrix in C++
- Find n-th Fortunate Number in C++
- Find n-th node of inorder traversal in C++
- Find k’th character of decrypted string in C++

Advertisements