Convert Infix to Prefix Expression

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

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 Find duplicates Characters in the given String

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 Max Flow Problem – Ford-Fulkerson Algorithm

Sum of all Unique elements in a given array

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

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 Convert Infix to Postfix Expression

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 Find three smallest elements in a given array

Evaluation of Infix expressions

Infix notation is commonly used in arithmetic formula or statements, the operators are written in-between their operands. Let’s assume the below Operands are real numbers. Permitted operators: +,-, *, /, ^(exponentiation) Blanks are permitted in expression. Parenthesis are permitted Example: A * ( B + C ) / D 2 * (5 + 3) / … Read more Evaluation of Infix expressions

Least Recently Used (LRU) Cache – Using LinkedHashSet and Deque | Set 2

Objective: Design and Implement a data structure Least Recently Used (LRU) Cache. Earlier we had seen Least Recently Used (LRU) Cache – Using HashMap and Doubly Linked List. In this article, we will see the implementation using LinkedHashSet and Deque. Lets first brief about LRU- Given a cache (or memory) with capacity. The cache will … Read more Least Recently Used (LRU) Cache – Using LinkedHashSet and Deque | Set 2

Merge K sorted Linked List – Using Priority Queue

Objective: Given, K sorted linked list, Write an algorithm to merge all the linked list into one linked list which will be also be sorted. Example: List 1: –>1–>5 List 2: –>4–>8 List 3: –>4–>6–>9 List 4: –>2–>7–>10 Merged Linked List: –>1–>2–>4–>4–>5–>6–>7–>8–>9–>10 Approach: Use Priority Queue Please read Priority Queue and Linked List if needed. … Read more Merge K sorted Linked List – Using Priority Queue

Count Set bits in a given Number

Bit Manipulation

Objective: Given a Number, find all the set bits in that number. Example: Number: 23 Set bits: 4 (10111) Number: 15 Set bits: 4 (1111) Number: 21 Set bits: 3 (10101) Approach: Check the last bit of number, if it is 1 then add it to the result. Right shift the number by 1. Repeat … Read more Count Set bits in a given Number

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 Check if array contains all unique or distinct numbers.

Product of all Unique elements in a given array.

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

Valid Multiple Parentheses

Objective: Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. Example: ()()([]({}))[] valid: true ()({]) valid: false []{}() valid: true [(}{)] valid: false Earlier we had seen problem Valid Parentheses, where only one type of parentheses was present in the input string, in this problem we can have multiple types … Read more Valid Multiple Parentheses

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 Check if given number is perfect square – O(√N) Solution

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 first three largest elements in a given array