Longest substring with at most two unique characters

Objective: Given a string, write an algorithm to find the longest substring with at most two characters. Example: Input: aabbccddc Output: ccddc, length: 5 Input: aabacbeeeebeaabb Output: beeeebe, length 7 Input: aaaaaa Output: Only one unique character is present in the input string Approach: Brute Force Identify all the substrings of the given input string … Read more

Convert Prefix to Infix Expression

Objective: Given a Prefix expression, write an algorithm to convert it into Infix expression. Example: Input: Prefix expression: + A B Output: Infix expression- (A + B) Input: Prefix expression: *-A/BC-/AKL Output: Infix expression: ((A-(B/C))*((A/K)-L)) Approach: Use Stacks Algorithm: Iterate the given expression from right to left (in reverse order), one character at a time … Read more

Find duplicates Characters in the given String

Objective: Given a string, write an algorithm to find all the duplicate characters in the string and print its count. Example: Input String: tutorial horizon Output: Duplicate Characters: r – 2 t – 2 i – 2 o – 3 Approach: Hash Map Maintain a Hash Map with Character as key and count as value. … Read more

Max Flow Problem – Ford-Fulkerson Algorithm

Objective: Given a directed graph that represents a flow network involving source(S) vertex and Sink (T) vertex.  Each edge in the graph has an individual capacity which is the maximum flow that edge allows. Flow in the network has the following restrictions- Input flow must match to output flow for each node in the graph, … Read more

Convert Infix to Postfix Expression

Objective: Given an Infix expression, write an algorithm to convert it into Postfix expression. Example: Input: Infix expression – A + B Output: Postfix expression- AB+ Input: Infix expression – A+B*(C^D-E) Output: Postfix expression- ABCD^E-*+ Approach: Use Stacks Operator stack: This stack will be used to keep operations (+, -, *, /, ^) Order of … Read more

Find three smallest elements in a given array

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

Check if array contains all unique or distinct numbers.

Objective: Given an array of integers, write a program to check if array contains all unique numbers. Example: Integer [] arrA = { 1, 2, 3, 6, 4, 9, 8}; Output: Array has all distinct elements Integer [] arrB = { 1, 2, 3, 6, 4, 9, 8, 2}; Output: Array contains duplicates Approach: Brute … Read more

Reverse a given number – Java Code

Objective: Given a number, write a java program to reverse a number Example: Given Number: 1234 Output: 4321 Given Number: 1020 Output: 201 Approach:  Using loop Keep divide the number by 10 and add the remainder to the reversed number (after multiple reversed number with 10) See the code. Run This Code Java Code: Output: … Read more

Check if given number is perfect square – O(√N) Solution

Objective: Given a number, write a program to check if given number is perfect sqaure. Example: N = 16 Output: True N = 32 Output: False Approach: Naive Approach: If N = 1 return true. Iterate through 1 to N/2 and check for each number whether square of each number is equal to N, if … Read more

Find first three largest elements in a given array

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

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 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

Print all Unique elements in a given array

Objective: Given an array of integers which contains duplicates as well. Write a program to print all unique elements in the array. This problem is also referred as print all distinct elements in the array Example: [] arrA ={1, 6, 4, 3, 2, 2, 3, 8, 1}; Output: Unique elements are: 1, 6, 4, 3, … Read more

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