C++ Program to Print uncommon elements from two sorted arrays Last Updated : 13 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20, 30} arr2[] = {40, 50} Output : 10 20 30 40 50 The idea is based on merge process of merge sort. We traverse both arrays and skip common elements. C++ // C++ program to find uncommon elements of // two sorted arrays #include <bits/stdc++.h> using namespace std; void printUncommon(int arr1[], int arr2[], int n1, int n2) { int i = 0, j = 0, k = 0; while (i < n1 && j < n2) { // If not common, print smaller if (arr1[i] < arr2[j]) { cout << arr1[i] << " "; i++; k++; } else if (arr2[j] < arr1[i]) { cout << arr2[j] << " "; k++; j++; } // Skip common element else { i++; j++; } } // printing remaining elements while (i < n1) { cout << arr1[i] << " "; i++; k++; } while (j < n2) { cout << arr2[j] << " "; j++; k++; } } // Driver code int main() { int arr1[] = {10, 20, 30}; int arr2[] = {20, 25, 30, 40, 50}; int n1 = sizeof(arr1) / sizeof(arr1[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); printUncommon(arr1, arr2, n1, n2); return 0; } Output : 10 25 40 50 Time Complexity: O(n1 + n2), where n1 and n2 represents the size of the given two arrays.Auxiliary Space: O(1), no extra space is required, so it is a constant. Please refer complete article on Print uncommon elements from two sorted arrays for more details! Comment More infoAdvertise with us Next Article C++ Program to Print uncommon elements from two sorted arrays K kartik Follow Improve Article Tags : Searching C++ Programs C++ DSA Arrays two-pointer-algorithm +2 More Practice Tags : CPPArraysSearchingtwo-pointer-algorithm Similar Reads C++ Program for Last duplicate element in a sorted array We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu 2 min read C++ Program to Sort the Elements of an Array in Ascending Order Here, we will see how to sort the elements of an array in ascending order using a C++ program. Below are the examples: Input: 3 4 5 8 1 10Output: 1 3 4 5 8 10 Input: 11 34 6 20 40 3Output: 3 6 11 20 34 40 There are 2 ways to sort an array in ascending order in C++: Brute-force Approach Using Bubble 4 min read C++ Program To Remove Duplicates From Sorted Array Given a sorted array, the task is to remove the duplicate elements from the array.Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} new size = 1 Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} new size = 5 Recommended PracticeRemove duplicate elements from sorte 3 min read C++ Program for Sorting array except elements in a subarray Given an array A positive integers, sort the array in ascending order such that element in given subarray (start and end indexes are input) in unsorted array stay unmoved and all other elements are sorted.Examples : Input : arr[] = {10, 4, 11, 7, 6, 20} l = 1, u = 3 Output : arr[] = {6, 4, 11, 7, 10 2 min read C++ Program to Maximize elements using another array Given two arrays with size n, maximize the first array by using the elements from the second array such that the new array formed contains n greatest but unique elements of both the arrays giving the second array priority (All elements of second array appear before first array). The order of appeara 4 min read Like