Category Archives: Stack

Find maximum sum possible equal sum of three stacks

Given three stack of the positive numbers, the task is to find the possible equal maximum sum of the stacks with removal of top elements allowed. Stacks are represented as array, and the first index of the array represent the top element of the stack. Examples: Input : stack1[] = { 3, 10} stack2[] =… Read More »

Find next Smaller of next Greater in an array

Given array of integer, find the next smaller of next greater element of every element in array. Note : Elements for which no greater element exists or no smaller of greater element exist, print -1. Examples: Input : arr[] = {5, 1, 9, 2, 5, 1, 7} Output: 2 2 -1 1 -1 -1 -1… Read More »

Find if an expression has duplicate parenthesis or not

Given an balanced expression, find if it contains duplicate parenthesis or not. A set of parenthesis are duplicate if same subexpression is surrounded by multiple parenthesis. Examples: Below expressions have duplicate parenthesis – ((a+b)+((c+d))) The subexpression “c+d” is surrounded by two pairs of brackets. (((a+(b)))+(c+d)) The subexpression “a+(b)” is surrounded by two pairs of brackets.… Read More »

Implement a stack using single queue

We are given queue data structure, the task is to implement stack using only given queue data structure. We have discussed a solution that uses two queues. In this article, a new solution is discussed that uses only one queue. This solution assumes that we can find size of queue at any point. The idea… Read More »

Iterative Depth First Traversal of Graph

Depth First Traversal (or Search) for a graph is similar to Depth First Traversal (DFS) of a tree. The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. For example, a… Read More »