Simple Text Editor Hackerrank


 Problem Statement : Approach Maintain a stack with each version of S . Each time S is modified, push a copy of it onto the stack. Each time undo is called, pop the previous current value from the top of the stack and set the new current value at the top of the stack… Read More »

Min Priority Queue C++ Using STL


 By Default, priority_queue present in STL library supports Max Priority Queue, In max Priority queue top element of the queue is Maximum Number. To Implement Min Priority Queue i will tell you two approaches. 1. Using Operator Over loading In this Approach you need to remember the syntax. 2. Use the Default priority queue which… Read More »

Category: STL

Square Root of an Integer Binary Search


 Given an integer x, find square root of it. If x is not a perfect square, then return floor(√x). Approach We can use Binary Search to find square root. Take 2 variable first = 1 and last = N. While First <= Last, find mid and check mid*mid==N return Mid. If mid*mid>N It mean Mid… Read More »

Max Product Subarray


 Find the contiguous subarray within an array which has the largest product.  Max product Subarray I/P : 2 3 -2 5 O/P : 6 Approach Max Product Subarray  Max Product Subarray Max Product Subarray Max Product subarray Max Product Subarray Max Product Subarray Max Product Subarray Max Product Subarray Max Product Subarray Intuition, If all… Read More »

Set Matrix Zeroes


 Given an m * n matrix . If any element is zero, set entire row and column to zero of the corresponding element. Input : 1 0 1 1 1 0 1 1 1 Output : 0 0 0 0 0 0 1 0 0 Approach Above Problem can be easily solved by taking extra… Read More »

Find all Duplicate in a array in Linear Time


 Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Input : [ 1 1 3 4 5 5 6 ] Output : [ 1 5 ] Approach 1. We can solve… Read More »

Counting Bits


 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array. N = 5 OUTPUT : 0 1 1 2 1 2 Approach Simplest Approach is to count number of set bits… Read More »

Island Perimeter


 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn’t have “lakes” (water inside that… Read More »