Home
Gaurav's GitHub Page
Cancel

Find the Smallest Sub-Array to Sort

Problem Description You are given an array which could be unsorted. Return the start and end index of the smallest sub-array which if-sorted will make the complete array sorted. Solution Think a...

Trapping Rain Water

This question is part of NeetCode150 series. Problem Description Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap a...

Sum of all Sub-Arrays

Problem Description Return the total sum of all sub-arrays of the given array. Solution BRUTE-FORCE The brute force method is to check all sub-array, calculate its sum and add that to the total...

Sum of Linked Lists

PROBLEM DESCRIPTION Given two numbers as LinkedList in reverseOrder (most significant digit as the right most digit), return a LinkedList (reversed) as the sum. Example: LinkedListOne: 2 -&...

Kadane's Algorithm

Problem Description Given an array of numbers, find the maximum sum possible for any subarray. Solution We can use Kadane’s Algorithm to solve this. <—–positive numbers—–>&...

Sort Stack (In Place)

PROBLEM DESCRIPTION Given a stack, sort the elements in-place. (smallest element at the bottom of the stack) SOLUTION Think about: If we are given an already sorted stack and we need to add an e...

Max Path Sum in Binary Tree

PROBLEM DESCRIPTION Given a binary tree, return its max path sum. SOLUTION The apprach to this problem is similar to Binary Tree Diameter. We keep a track of two things, the sum in LST and the s...

Height Balanced Binary Tree

PROBLEM DESCRIPTION A binary tree is balanced if for each node in the tree, the difference between the height of its LST and RST is at most 1. SOLUTION public int heightBalancedBinaryTreeX(Bin...

Find Successor in Binary Tree

PROBLEM DESCRIPTION We are given a tree in which every Node has another attribute called parent which points to its immediate ancestor. We are also given another Node as input, which is present in...

Binary Tree Diameter

PROBLEM DESCRIPTION Return the diameter of a given binary tree. Diameter is defined as the length of its longest path, even if that path doesn’t pass through the node of the tree. SOLUTION This ...