tutorialcup@gmail.com

Find all common elements in given three sorted arrays

Arrays
Reverse Array
Pair Sum Equals Number
Floor and Ceil
Largest Sum Subarray
Missing Number
Odd Occuring Number
Sort by Frequency
First and Second Smallest
Majority Element
Difference between elements
Arrange Even and Odd
Closest Sum Elements
Distinct Elements
First Repeating Number
Common Elements in Arrays
Smallest Missing Number
Max Adjacent Sum
Find Occurrences of Number
Rotate Image
Distance Between Numbers
Pair with given difference
Product Array Puzzle
Replace Elements
First Repeating Element
Move Zeroes to End
Pythagorean Triplets
Maximum Average Subarray
Subarray Sum Equal to X
Leaders in Array
Largest Pair Sum
Segregate 0s 1s and 2s
Find Duplicates
Consecutive Elements Check
Triplet with given Sum
Binary Search in Sorted Array
Fixed point in sorted array
Reorder Elements by Indexes
Merging two sorted arrays
Find next greater number
Reorder array using given indexes
Count of triplets with sum less than given value
Merge two sorted arrays
Subarray and Subsequence
Rearrange array maximum minimum form
Find the lost element from a duplicated array
Count minimum steps to get the given array
Maximum element in an array which is increasing and then decreasing
Minimum number of jumps to reach the end of an array
Subarray with given sum
Length of Longest Increasing Subsequence
Smallest positive number missing unsorted array
The Celebrity Problem
Sorted subsequence of size 3
Partition Problem
Find pair with given difference
Maximum length of chain pairs
Four elements that sum to given
Maximum circular subarray sum
Count possible triangles
Longest Increasing Subsequence
Petrol Bunks Tour
Tug of War
Counting Sort
Maximum Repeating Number
Positive Negative Arrangement
Find a Peak element
Elements More Than n/k
Max Product Subsequence
Longest Bitonic subarray in an array
Number of smaller elements on right side
Implement two stacks in an array
Maximum sum increasing subsequence
Find the two numbers with odd occurrences in an unsorted array
Largest subarray with equal number of 0's and 1's
Maximum product subarray
Replace every element with the greatest on the right side in an array
Sorting a k sorted array
Find the row with maximum number of 1's
Shuffle a given array
Iterative Implementation of quick sort
Arrange given numbers to form the biggest number
Pancake sorting
Pancake sorting Problem
Maximum Subarray Sum using Divide and Conquer
Merge Overlapping Intervals
Stock Buy Sell to Maximize Profit
Sort Elements by frequency
Print all possible combinations of r elements in a given array of size n
Monotonically increasing function
Minimum element in a sorted and rotated array
Merge k Sorted Arrays
Flip Zeroes for Consecutive 1's
Least Average Subarray
Longest span with same sum in two binary arrays
Form minimum number from given sequence of D's and I's
Number of strictly increasing subarrays
Minimum difference between any two elements in an array
Number of pairs with given sum
Make Array Palindrome
Dynamic Programming, Longest Bitonic SubSequence

We have three sorted arrays and you have to find common numbers which is present in all three arrays

INPUT: 

ar1[] = {1, 5, 10, 20, 40, 80}

ar2[] = {6, 7, 20, 80, 100}

ar3[] = {3, 4, 15, 20, 30, 70, 80, 120}

OUTPUT: 20 80

ALGORITHM

TIME COMPLEXITY – O(max(M,N,P)) where M,N and P are sizes of the three arrays

SPACE COMPLEXITY – O(1)

1. Take three pointer like variables i , j and k pointing to the starting index of the three arrays respectively.

2. Check if the three numbers pointed by the variables are same or not

3. If same then print the value and increment all the three variables hence moving forward in the respective arrays.

4. Else increment the variable which is pointing to the smaller ones.

5. If an array gets traversed completely then we check for the remaining two arrays plainly.

6. If two arrays are traversed then simply print the untraversed part of the third array because we know that it is already sorted.

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int arr1[] = {2, 8, 15 , 20, 35, 45, 100};
	int arr2[] = {5, 9, 20, 45, 110};
    	int arr3[] = {3, 4, 15, 20, 30, 45, 80, 120};
    	int n1 = sizeof(arr1)/sizeof(arr1[0]);
    	int n2 = sizeof(arr2)/sizeof(arr2[0]);
    	int n3 = sizeof(arr3)/sizeof(arr3[0]);
	
	
	int  i = 0 ,  j = 0 , k = 0; // i ,j and k are pointing at the  start of 1st , 2nd and 3rd array resepectively.
	
	while(i < n1 and j < n2 and k < n3)
	{
		if(arr1[i] == arr2[j] and arr3[k] == arr1[i]) //if all elements are same then
		{
			cout << arr1[i] <<" ";
			i++; j++; k++;
		}
		//increase the array index variable of those which are small
		else if(arr1[i] < arr2[j])
			i++;
		else if(arr2[j] < arr3[k])
			j++;
		else
			k++;
		
	}
		
	return 0;
}
Try It




C++ Online Test - Check Your Rank across the World


Next > < Prev
Scroll to Top