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.
This algorithm to print m*n matrix diagonally. Algorithm going to take Big O(m*n/2) time
This algorithm is to generate random tree based on max integer value. This is very useful when you want tree for your algorithm.
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.
This algorithm to find peak element in an array, Peak element is an element which has greater value than its left and right element. one way to get peak element is using linear search which takes O(n), but we can also search using binary algo.
This example to generate balanced binary search tree from an array. A binary tree is balanced if for each node it holds that the number of inner nodes in the left subtree and the number of inner nodes in the right subtree differ by at most 1.