Find Common Elements in Three Sorted Arrays – Java Code

Write a java program to find common elements in three sorted arrays.

Given three sorted arrays, write a code to print intersection of three sorted arrays.

For example –

arr1 = {1, 5, 10, 20, 40, 80};
arr2 = {6, 7, 20, 80, 100};
arr3 = {3, 4, 15, 20, 30, 70, 80, 120};

Output : {20, 80}

20 and 80 are the common elements in 3 arrays. These two elements are the intersection of 3 sorted arrays.

Java Program to Reverse a String using Stack

Write a java program to reverse a string using stack data structure. Given an input string, We have to write a java code to reverse a string using stack.

In my previous tutorial, I have explained java program to reverse a string using recursion.

Before solving this problem, Let’s understand what is a stack data structure.

Find Pairs with Given Sum in a Sorted Array – Java Code

Find pairs with given sum in a sorted array.

Given an array A of size N. Write a code to find all pairs in the array that sum to a number equal to K. If no such pair exists then output will be 1.

NOTE – The array elements are distinct and in a sorted order.

For example –

Input :

arr[] = {1, 2, 3, 4, 5, 6, 7};
sum = 9

Output:

Pairs with given sum 9 is

Pair (2 , 7 )
Pair (3 , 6 )
Pair (4 , 5 )