28/02/2023

Union And Intersection of Two Sorted Arrays in C++ | Ascending Order

Union of two arrays in C++. Union and the intersection of two sorted arrays. The intersection of two arrays. Find the union and intersection of the two sorted arrays. Union of sets or union of two arrays in C++, the intersection of two arrays in C++ or finding the union and intersection of the two sorted arrays. Union and intersection of two sets in C++.

Union And Intersection of Two Sorted Arrays

If you have any doubts ask in a comment. C++ Program To Find The Union And Intersection Of Two Array In Increasing Order.

Try This C++ Program For Draw A Perfect Christmas Tree

Union And Intersection of Two-Sorted Arrays in C++


#include <iostream>
using namespace std;

void unionofarray(int a[], int b[], int m, int n)
{
	int i = 0, j = 0;
	cout << "\n\nUnion Of Array\n\n";

	while (i < m && j < n)
	{
		if (a[i] < b[j])
			cout << a[i++] << " ";
		else if (a[i] > b[j])
			cout << b[j++] << " ";
		else
		{
			cout << a[i++] << " ";
			j++;
		}
	}

	while (i < m)
		cout << a[i++] << " ";
	while (j < n)
		cout << b[j++] << " ";
}

void intersection(int a[], int b[], int m, int n)
{
	int i = 0, j = 0;
	cout << "\n\nIntersection Of Array\n\n";
	while (i < m && j < n)
	{
		if (a[i] < b[j])
			i++;
		else if (a[i] > b[j])
			j++;
		else
		{
			cout << a[i++] << " ";
			j++;
		}
	}
}

int main()
{
	int m, i, j, n, a[100], b[100];

	cout << "Enter The Size Of First Array \n";
	cin >> m;

	cout << "\nEnter The Element In First Array \n\n";

	for (i = 0; i < m; i++)
	{
		cin >> a[i];
	}

	cout << "\nEnter The Size Of Second Array \n";
	cin >> n;

	cout << "\nEnter The Element In Second Array \n\n";

	for (j = 0; j < n; j++)
	{
		cin >> b[j];
	}

	unionofarray(a, b, m, n);
	intersection(a, b, m, n);

	return 0;
}

What is Union?


In set theory, the union (denoted by ∪) of a collection of sets is the set of all elements in the collection. It is one of the fundamental operations through which sets can be combined and related to each other.

What Is Intersection?


In mathematics, the intersection A ∩ B of two sets A and B is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements. For an explanation of the symbols used in this article, refer to the table of mathematical symbols.

The Output of Union And Intersection of Two-Sorted Arrays


The Output of Union And Intersection of Two Sorted Arrays

Similar to Union And Intersection


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: