Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
This is the algorithm to reverse the linked list.
In this algorithm we just need length of longest sub string of given string. We are going to solve this by O(n) Example : Input : zabac Output : 3 Just take a queue and add character in it one by one. Just before adding any new char, check if it is already there then… Read more
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Rotate an array of n elements to the right by k steps and then find an element at particular position in array. For example, with n = 5 and k = 3, the array [1,2,3,4,5] is rotated to [4,5,1,2,3]. Now after rotation we need element at position 3 which is 2 Consider this array as… Read more
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice.
Find below algorithm to find loop/cycle in graph, Here in each stage I am checking for child node visited state. If any of the parent node has more than one child with visited state then it means graph has a loop. We can also use Disjoint Set (Or Union-Find) which I’ll show you in next… Read more
Activity Selection problem, You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person.
Algorithm to find next greater element in array for current element. If we do linear search then it would take O(n^2) but here we are going to use stack to implement this algorithm. It will going to take O(n) time.
Here we are going to use runner techniques, where there will be a two pointers slow and fast. Fast will going to take double steps then slow one, Lets see in below image.