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

Find first two largest elements in a given array

Objective: Given an array of integers, write an algorithm to find the first two largest elements in the array. Example: Int [] a = { 6, 8, 1, 9, 2, 1, 10}; Output: 10, 9 Int [] a = { 6, 8, 1, 9, 2, 1, 10, 10}; Output: 10, 10 Int [] a = … Read more Find first two largest elements in a given 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 no of reverse pairs in an array which is sorted in two parts in O(N)

Objective: Given an array of integers A[] which is sorted in two parts (both parts are individually sorted), find no of reverse pairs means no of (i, j) pairs where i belongs to the part one and j belongs to part 2 and A[i]>A[j]. Example: A[] = {4, 6, 8, 9, 1, 5, 10, 11} … Read more Find no of reverse pairs in an array which is sorted in two parts in O(N)

Get the Sum of Digits in a number till it become a single digit

Objective – Given a number, Write a program to get the sum of digits in a number till it become a single digit. Example: N = 999 -> 9+9+9 = 27-> 2+7 = 9 N = 789 -> 7+8+9= 24-> 2+4 = 6 Approach: Recursion– Find the sum of all digits. Make a recursive call … Read more Get the Sum of Digits in a number till it become a single digit

Check if given number is Prime – O(√N) Solution – Java Program

Objective: Given a number, write a program to check if the number is prime or not. Prime Number: A number is called a prime number when number is not divisible by 1 or by number itself. 1 is not considered as prime number. Example: N = 10 Output: ‘10’ is not a prime number N … Read more Check if given number is Prime – O(√N) Solution – Java Program

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

Fizz Buzz Challenge – Java Implementation

Objective: Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz” Output: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, … Read more Fizz Buzz Challenge – Java Implementation