- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Find N’th item in a set formed by sum of two arrays in C++

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,**

## Input

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

**Output**

## Explanation

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.

## Solution Approach

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,**

## Example

#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; }

## Output

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

- Related Articles
- Find Sum of pair from two arrays with maximum sum in C++
- Find the overlapping sum of two arrays using C++
- Maximum OR sum of sub-arrays of two different arrays in C++
- Maximum Sum of Products of Two Arrays in C++
- Find a pair of elements swapping which makes sum of two arrays same in C++
- Maximum Sum Path in Two Arrays in C++
- Maximum sum by picking elements from two arrays in order in C++ Program
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
- Count pairs formed by distinct element sub-arrays in C++
- Reverse sum of two arrays in JavaScript
- Sum of all subsets of a set formed by first n natural numbers
- Recursive sum of digits of a number formed by repeated appends in C++
- Set 6-item tuple in C#’
- Find relative complement of two sorted arrays in C++
- Count Pairs from two arrays with even sum in C++