Find the sum of overlapping elements in two sets

Objective: Given two sets of integers, write an algorithm to print the sum of overlapping elements in two sets. Note: Elements in each set are unique or there are no duplicates within a set. Example: Set 1: [2, 3, 1, 6] Set 2: [4, 0, 6, 7] Sum of overlapping elements: 12 Explanation: Common element … Read more Find the sum of overlapping elements in two sets

Check the completeness of given binary tree | Set 2 – Using Level Order Traversal

Objective: Given a binary tree, write an algorithm to determine whether the tree is complete or not using Level order traversal. Earlier we had solved this problem by counting the number of nodes in the tree. (Read – Check the completeness of given binary tree | Set 1 – Using Node Count. In this article, … Read more Check the completeness of given binary tree | Set 2 – Using Level Order Traversal

Check the completeness of given binary tree | Set 1 – Using Node Count

Objective: Given a binary tree, write an algorithm to determine whether the tree is complete or not.  Complete Binary Tree: A binary tree T with n levels is complete if all levels except possibly the last are completely full, and the last level has all its nodes to the left side. Example:  Below is an … Read more Check the completeness of given binary tree | Set 1 – Using Node Count

Check if the given binary tree is Full or not.

Objective: Given a binary tree, write an algorithm to check if the tree is Full or not. Full binary tree:  A binary tree T is full if each node is either a leaf or possesses exactly two child nodes. See the example below Approach: Do postorder traversal. Check if the node is a leaf node … Read more Check if the given binary tree is Full or not.

Print Stack in reverse order.

Objective: Given a stack, write a program to print the stack elements in reverse order. Example: Approach: Use Temporary stack: Take temporary stack, and copy all the items from the given stack to a temporary stack. Elements will be stored in a temporary stack in reverse order. Now print elements from the temporary stack and … Read more Print Stack in reverse order.

Given an array, Print sum of all subsets

Objective: Given an array of numbers, write an algorithm to print all the subsets sum individually.   Example: Given Input: [1, 2] Output: 3 1 2 0 Explanation: subsets are [0], [1], [2], [1, 2] Given Input: [2, 4, 6] 12 6 8 2 10 4 6 0 Approach: Recursion Every element of the array has … Read more Given an array, Print sum of all subsets

Lexicographically previous permutation With One swap

Objective: Given an array of integers (in particular order or permutation of a set of numbers), write an algorithm to find the lexicographically previous permutation of the given permutation with only one swap.  This problem can also be asked as “Given a permutation of numbers you need to find the next smaller premutation OR largest … Read more Lexicographically previous permutation With One swap

Find all unique combinations of exact K numbers (from 1 to 9 ) with sum to N

Objective: Given two integers N and K. Write an algorithm to find all the combinations of k numbers which sum to N.  Conditions:  All K numbers must be between 1 and 9 and unique. All numbers in K are positive. Example: N= 5 K=2 Output: [1, 4] [2, 3] N =12 K=3 Output: [1, 2, … Read more Find all unique combinations of exact K numbers (from 1 to 9 ) with sum to N

Maximum number edges to make Acyclic Undirected/Directed Graph

Given- Given V vertices, what is the maximum number of edges can be added to make Acyclic Undirected Graph. Follow up – what is the maximum number of edges that can be added to make Acyclic Directed Graph. Example:  Approach: For Undirected Graph – It will be a spanning tree (read about spanning tree) where … Read more Maximum number edges to make Acyclic Undirected/Directed Graph

Evaluation of Prefix Expressions (Polish Notation) | Set 2

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

ZigZag OR Diagonal traversal in 2d array/Matrix using queue

Objective: Given a two-dimensional array or matrix, Write an algorithm to print the given matrix in a zigzag manner or in other words print all the diagonals of the matrix. Example: int [][] grid = new int[][] {            {1, 2, 3, 4},            {5, 6, 7, 8},            {9, 10, 11, 12},            {13, 14, 15, 16}} Output:  … Read more ZigZag OR Diagonal traversal in 2d array/Matrix using queue

Evaluation of Prefix Expressions (Polish Notation) | Set 1

Prefix notation is a notation for writing arithmetic expressions in which the operands appear after their operators. There are no precedence rules to learn. Let’s assume the below Operands are real numbers in real digits. (Later we will Enhance the solution for any number) Permitted operators: +,-, *, /, ^(exponentiation) Blanks are NOT permitted in … Read more Evaluation of Prefix Expressions (Polish Notation) | Set 1

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

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

Postfix notation is a notation for writing arithmetic expressions in which the operands appear before their operators. There are no precedence rules to learn, and parentheses are never needed. Because of this simplicity. Let’s assume the below Operands are real numbers in single digits. (Read: Evaluation of Postfix Expressions for any Number ) Permitted operators: … Read more Evaluation of Postfix Expressions (Polish Postfix notation) | Set 1

Find all the numbers in the range which has prime set bits.

Objective: Given a range, find all the numbers in the range which has prime set bits. Example: L = 4, R = 10 Output: 5 Explanation: 4 = 1 0 0 ( not prime) 5 = 1 0 1 (prime) 6 = 1 1 0 (prime) 7 = 1 1 1 (prime) 8 = 1 … Read more Find all the numbers in the range which has prime set bits.