Evaluation of Postfix Expressions (Polish Postfix notation) | Set 2

Earlier we had discussed how to evaluate postfix expressions where operands are of single-digit. In this article, we will discuss how to evaluate postfix expressions for any number ( not necessarily single digit.) Postfix notation is a notation for writing arithmetic expressions in which the operands appear before their operators. Let’s assume the below Operands … Read more Evaluation of Postfix Expressions (Polish Postfix notation) | Set 2

Round Price Problem

Problem Statement: When customers book an Airbnb the total price, which includes base price + service fee + cleaning fee. all these prices are in decimals. Write an algorithm to round each price such that the sum of the prices equals the round of the total sum of all decimal prices and minimize the rounding. … Read more Round Price Problem

Reverse a Stack using recursion – In Place (Without using extra memory)

Objective: Given a Stack, write an algorithm to reverse the stack. Example: Original Stack: [14, 9, 67, 91, 101, 25] Reversed Stack: [25, 101, 91, 67, 9, 14] Approach: Use Recursion In this solution, we need two recursive functions. reverse() and insert_at_bottom(). reverse() – this function will be called by the driver. In this function, … Read more Reverse a Stack using recursion – In Place (Without using extra memory)

Sort a given stack – Using Temporary Stack

Objective: Given a stack of integers, write an algorithm to sort the stack using a temporary stack.  Example: Given Stack: [14, 9, 67, 91, 101, 25] Sorted Stack: [9, 14, 25, 67, 91, 101] Approach: Use another stack, let’s call it a temporary stack. While given original is not empty Pop the element from the … Read more Sort a given stack – Using Temporary Stack

Grouping of Anagrams

Objective: Given an array of strings, write an algorithm to group the anagrams. Example: Input: [rat, art, cat, act, dog, god, tar, pat] Output: [rat, art, tar] [cat, act] [pat] [dog, god] Approach:  If two strings are an anagram, then characters count in both the string must match. So if we sort both the strings, … Read more Grouping of Anagrams

Linear Search vs Binary Search

Earlier we have seen linear search and binary search and how these work individually, In this article we will compare these two search algorithms. If you are new to these, please read the prerequisites below- Prerequisites: Linear Search Binary Search SNo Linear Search Binary Search 1 Works with a sorted or unsorted array. Works with … Read more Linear Search vs Binary Search

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

Java program to find the largest element in array

Objective– Given an array of numbers, write a java program to find the largest element in the given array. Example: int [] a = {1, 5, 3, 9, 2, 8, 2} Largest Element: 9 Approach: Linear Search Initialize a variable largest_element = a[0]. Run a loop from 2nd element till the last element in the … Read more Java program to find the largest element in array

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

Stack Java Class – Explained

Earlier we saw about Stack and its implementation using Linked List. Java has a built in class for Stack. In this article we will discuss about it in detail. First brief about Stack. What is Stack?? Stack is an abstract data type (ADT) and very useful in programming. In computer science, a stack is an abstract data type that serves … Read more Stack Java Class – Explained

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.

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