Design a stack that returns the minimum element in constant time.

Problem Statement: You need to design a stack that returns the minimum element from the stack in constant time. Solution For solving this problem we will use 2 stack. 1. Push Operation: 1. Push the element in to the first stack, and push into the 2nd stack if the stack is empty or new element […]

Convert Postfix to Infix

Problem Statement: Given an infix expression, convert into postfix expression. Infix expression: a op b Postfix expression: a b op Solution In this solution we shall use stack data structure. Below are the steps: 1. Start from left to right 2. If the char is a operand (value), then push into the stack else 3. […]

Linked List: Odd Even Linked List

Problem Statement: Given a singly LL group all odd nodes together followed by the even nodes. We need the node number not the value in the node. Example Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL Output: 1 -> 3 -> 5 -> 2 -> 4 -> NULL Solution Solution is […]

Matrix: Find pair with maximum sum in the matrix.

Problem Statement: You are given a matrix arr[n][n]. You need to find the sum of pair with max sum. Example Input : mat[N][M] = {{1, 2, 3, 4}, {25, 3, 5, 1}, {8, 9, 3, 9}, {3, 10, 5, 16}} Output : 41 Pair (25, 16) has the maximum sum Solution Method 1: We can […]

Strings: Second most repeated word in a sequence

Problem Statement: You are given a strings, your task is to find the second most repeated string in the given sequence. Example Input : {“aaa”, “bbb”, “ccc”, “bbb”, “aaa”, “aaa”} Output : bbb Solution The solution is very simple. Store all the words in a map and the count of the times that word is […]

Strings: Given a string convert into its equivalent ASCII form

Problem Statement: Given a string convert into its equivalent ASCII format. Example Input : hello, world! Output : 104101108108111443211911111410810033 Solution The solution is very simple. Iterate over the string and take each character and subtract NULL character. Print the result. Solution in C++ #include <vector> #include <algorithm> //visit www.ProDeveloperTutorial.com for 450+ solved questions #include <iostream> […]

Strings: Remove all consecutive duplicates from the string

Problem Statement: Given a string, remove all the consecutive duplicates Example Input : aaaaabbbbbb Output : ab Solution Solution is very simple. If the string is empty return. else, compare the adjacent character of the string. If they are same then shift the character one to the left, call recursion on string s. If not […]

Arrays: Program to check if an Array is Palindrome or not

Problem Statement: Given an unsorted array, check if it is palindrome array or not. Example: arr[] = {1, 2, 0, 2, 1} Output: Palindrome Solution: The solution is very simple, below are the steps to be follow: 1. We take 2 pointer, first pointer will point to beginning of the array, second pointer will point […]

Arrays: Find triplet with minimum sum

Problem Statement: Given an unsorted array, you need to find 3 elements whose sum is the minimum. Example: arr = {-1, 2, 3, -2}; Output: -1 The elements are: -1, -2, 2 The solution in very simple, take 3 variables that will hold minimum, second minimum and third minimum element present in the array and […]