# Tag Archives: Data Structure

## Find Minimum Platforms Needed to Avoid Delay in the Train Arrival

Problem The objective is to identify the minimal number of train platforms necessary for the railway station, as all trains arriving and departing reach the railway station. We have two arrays which show trains that cease arriving and departing time. Sample Input Trains arrival   = { 200, 210, 300, 320, 350, 500 } Trains departure = {… Read More »

## Maximum Difference Between Two Elements Such that Larger Element Appears After the Smaller Number

Problem Find the maximum difference between two array elements such that the bigger element appears after the smaller. Brute Force approach Utilize two loops. Pick items one by one in the outer loop, and in the inner loop, compute the difference between the selected element and every other element in the array, and compare the difference to the… Read More »

## Check if a Binary Tree is Height-Balanced or Not

Problem Given a binary tree. You need to check if it is height-balanced or not. A tree is height-balanced if the absolute difference between the height of the left and right subtrees is not more than 1. Sample Input Sample Output YES Explanation Each node’s left and right subtree difference does not exceed 1. Approach We can use… Read More »

## Convert a Binary Tree to Its Mirror Tree

Problem  Given a binary tree, you need to convert it into its mirror image. This problem is also known as inverting a binary tree problem. Example  Approach We can use DFS to solve this problem. Consider a case when you need to convert the following binary tree into the mirror tree:      1    /     \  2 … Read More »

## Find Maximum Sequence of Continuous 1s Formed by Replacing at-most ‘k’ 0s by 1s

Problem Given a binary array and an integer k, return the maximum number of consecutive 1‘s in the array if you can flip at most k  0‘s. Brute Force Approach You may travel ahead for each index and see how far you can get by substituting at max k 0’s. This technique will work, however, it is inefficient… Read More »

## Check for pairs in an array with a particular sum

Consider an array arr[] with n elements where the values in the array consist of both positive values and negative values including zero. Consider a number x. Find out whether the value x can be formed by adding any two values from the given array arr[].  Example 1 Input: Consider the array arr[] = {8,5,1,0} Let n =… Read More »

## Find maximum of minimum for every window size in a given array

Problem Statement We have an array of integers of size ‘n’, We need to print the maximum of minimums of every valid window size in the array. Where the window size ranges from 1 to n. Example – Input – arr[ ] = {15, 20, 8, 30, 12, 20} Output – 30, 15, 12, 8, 8, 8 Explanation:… Read More »

## Count all Possible Paths from Top Left to Bottom Right in a Matrix

You are given a matrix of m*n dimensions. You have to calculate the total number of possible paths in which you can reach from the top left to the bottom right of the matrix. But you are only allowed to move either top or bottom from any cell. You will be provided the value of m and n… Read More »