# Category Archives: Stack

## Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X

Recommended: Please try your approach on {IDE} first, before moving on to the solution. Given an integer X and a binary tree, the task is… Read More »

## Remove all continuous occurrences of ‘a’ and all occurrences of ‘b’

Given a string str, the task is to remove all the continuous occurrences of a and all occurrences of b and print the resultant string.… Read More »

## Subroutine, Subroutine nesting and Stack memory

1. Subroutine – A set of Instructions which are used repeatedly in a program can be referred to as Subroutine. Only one copy of this… Read More »

## Solve the Logical Expression given by string

Given a string str representing a logical expression which consists of the operators | (OR) , & (AND) , ! (NOT) , 0 , 1… Read More »

## Check if the given push and pop sequences of Stack is valid or not

Given push[] and pop[] sequences with distinct values. The task is to check if this could have been the result of a sequence of push… Read More »

## Infix to Postfix using different Precedence Values for In-Stack and Out-Stack

Conversion of infix to postfix expression can be done elegantly using two precedence function. Each operator is assigned a value (larger value means higher precedence)… Read More »

## Sum of minimum elements of all subarrays

Given an array A of n integers. The task is to find the sum of minimum of all possible (contiguous) subarray of A. Examples: Input:… Read More »

## Clone a stack without extra space

Given a source stack, copy the contents of the source stack to destination stack maintaining the same order without using extra space. Examples: Input :… Read More »

## Implement a stack using singly linked list

Implement a stack using single linked list concept. all the single linked list operations perform based on Stack operations LIFO(last in first out) and with… Read More »

## Reduce the string by removing K consecutive identical characters

Given a string ‘str’ and an integer ‘k’, the task is to reduce the string by applying the following operation: Choose a group of ‘k’… Read More »

## Construct a BST from given postorder traversal using Stack

Given postorder traversal of a binary search tree, construct the BST. For example, 1. If the given traversal is {1, 7, 5, 50, 40, 10},… Read More »

## Find the product of last N nodes of the given Linked List

Given a linked list and a number N. Find the product of last n nodes of the linked list. Constraints : 0 <= N <=… Read More »

## Count the number of pop operations on stack to get each element of the array

Prerequisite: Stack, Hashing Given a stack of N numbers and an array of numbers. Count the numbers of pop operations required to get each element… Read More »

## Find maximum in a stack in O(1) time and O(1) extra space

Given a stack of integers. The task is to design a special stack such that maximum element can be found in O(1) time and O(1)… Read More »

## Check if the elements of stack are pairwise sorted

Given a stack of integers, write a function pairWiseSorted() that checks whether numbers in the stack are pairwise sorted or not. The pairs must be… Read More »