## Check for Balanced Parentheses using Stack – Java Code

Write a java code to check for balanced parentheses using stack. Given an expression containing characters ‘{‘,’}’,'(‘,’)’,'[‘,’]’. We have to write a java code to check whether an input string has valid parentheses.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.

Example –

i) {[]}) – Invalid
ii) {()}[] – Valid

## Queue using Two Stacks – Java Code & Algorithm

How to implement a queue using two stacks. In this tutorial, We are going to discuss an algorithm and java code to implement a queue using two stacks.

Subscribe Our Tutorials

## 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 is the common element between three sorted arrays.

## Get Minimum Element from Stack in O(1)

Design a stack to get minimum element from stack in O(1) (constant time).

For example : Suppose, If we push following elements in a stack.

9  ==> Top
1
2
3

Then if we call getMin() method, It will return 1 in O(1) (constant time). In a stack, Element which pushed last in the stack is the first element to be popped out. Think for a moment how we can solve this problem.

## Find Middle Element of a Linked List – Java Code

In this tutorial, We are going to explain how to find middle element of a linked list in Java. This programming question is mostly asked in a technical interviews.

For example: Given a linked list, the middle element in this linked list is 8.

15 -> 9 -> 8 -> 5 -> 6 -> NULL

There are multiple approaches to solve this problem. In this tutorial, We are going to learn two approaches through which we can find middle element of a singly linked list. Also, i have also added video tutorial at the end of this tutorial.

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

Java code to find pairs with given sum in a sorted array. Given an array A of size N. Write a java 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.  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 )

## How to Check if a Number is a Power of 2

How to check if a number is a power of 2.  To understand this question, let’s take some example.

Example –

Input – 16   – 16 is a power of 2 (2^4).

Input – 15 – 15 is not a power of 2.

Input – 32- 32 is a power of 2 (2^5).

We can use multiple approaches to check whether a number is a power of 2 or not.

## Segregate 0s and 1s in an Array – Java Code

Segregate 0s and 1s in an Array. Given an array of 0s and 1s in random order. Write a java program to segregate 0’s on left side and 1’s on right side of the array.

For example –

Input array = [0, 1, 0, 1, 0, 0, 1]
Output array = [0, 0, 0, 0, 1, 1, 1]

## Find First and Last Position of a Number in a Sorted Array – Java Code

Given a sorted array which contains duplicate elements, Write a java code to find first and last position of a number in a sorted array. Given a number x, write a code to find it’s first and last index in an array.

For example –

Suppose the input array is arr[] : {1, 4, 7, 8, 8, 11, 11, 11, 11, 12, 13, 13} and the number to be searched is 11. Then the first index of 11 is 5 and last index is 8.