## Find the nearest building which has bike | Find nearest specific vertex from source in a graph.

Objective: Given a matrix (NxN) which represents the buildings in community. You are in a building and you need a bike to travel.  There are few buildings in community which has bikes which you can take for your ride. Write an algorithm to find the nearest building which has bike and distance from your building. … Read more Find the nearest building which has bike | Find nearest specific vertex from source in a graph.

## Valid Brackets – Part 2 | Stack Method

Objective: Given a string containing just the characters ( , ) determine if the input string is valid. Example: ()()(()(()))() valid: true )()()( valid: false ()()) valid: false Approach: Earlier we discussed the solution which keeps the count of open and closed parentheses. In this solution we will solve this problem using stack. Using Stack: Initialize a … Read more Valid Brackets – Part 2 | Stack Method

## Find two smallest elements in a given array

Objective: Given an array of integers, write an algorithm to find the two smallest elements in the array. Example: Int [] a = { 6, 8, 1, 9, 2, 10}; Output: 1, 2 Int [] a = { 6, 8, 1, 9, 2, 1, 10, 10}; Output: 1, 1 Int [] a ={6}; Output: Invalid … Read more Find two smallest elements in a given array

## Find Number of reverse pairs in an array

Objective: Given an array of integers A[],find no of reverse pairs means no of (i, j) pairs where i < j and A[i]>A[j]. This problem is also asked as Count Inversions in an array. Example: A[] = {10, 3, 4, 2, 5, 7, 9, 11} Output:7 Reversed pairs: (10, 3) (10, 4) (10, 2) (10, … Read more Find Number of reverse pairs in an array

## Calculate Logn base r – Java Implementation

Objective: Given a number n and r, write a program to calculate Logrn Example: N = 32, r =2 Log232= 5 N = 64, r = 4 Log464= 3 Approach: Without using built-In function- Initialize result = 0. Keep dividing the given number by r till number is greater than 0 and add one to … Read more Calculate Logn base r – Java Implementation

## Dijkstra Algorithm Implementation – TreeSet and Pair Class

Earlier we have seen what Dijkstra algorithm is and how it works. In this article, we will see its implementation using adjacency list and TreeSet. brief: What is Dijkstra’s algorithm? Dijkstra algorithm is a greedy algorithm. It finds a shortest-path tree for a weighted undirected graph. This means it finds the shortest paths between nodes in a … Read more Dijkstra Algorithm Implementation – TreeSet and Pair Class

## Check if Given Number is power of 2.

Objective: Given a number, write a program to find if number is power of two. Example: N = 5 Output: false. N = 8 Output: true (23) N = 512 Output: true (29) Approach: This problem can be solved in multiple ways; we will discuss three solutions here. Log2 Method Check the Remainder Convert number … Read more Check if Given Number is power of 2.

## Rotate the given array in cycles

Objective: Given an array of integers, write a program to rotate the array in cyclic manner by one. Example: Int a [] = {1, 2, 3, 4, 5} Output:  {2, 3, 4, 5, 1} Approach: Store the first element of array in temporary variable. From 2nd element to last element, left shift all the elements … Read more Rotate the given array in cycles

## Heap Sort – Java Implementation

What is Heap Sort?? Heap sort is comparison based sorting algorithm. Heap sort is considered as improved selection sort, it divides the input into sorted and unsorted region. The improvement from selection sort is to use Heap Data Structure instead of using linear search algorithm to reduce of the time complexity. Pre-requisite: Binary Heap (min … Read more Heap Sort – Java Implementation

## Find number of Distinct Permutations of a String

Objective – Given a string, find the number of distinct permutations or anagrams of that string. Example: String x = “abc” Output: 6 (abc, acb, bac, bca, cab, cba) String x = “aab” Output: 3 (aab, aba, baa) Approach: Number of ways to arrange n distinct items are = n!. If we have duplicate items, … Read more Find number of Distinct Permutations of a String