Number of Intervals in which given value lies

Objective: Given a list of intervals with start and end for each interval. You have given a value V, write an algorithm to find the number of intervals in which the value V lies.  Example: Given Interval: [[1,7], [3,10], [12,15]] Value : 6 lies in Intervals: 2 Given Interval: [[1,7], [3,10], [12,15]] Value : 11 … Read more Number of Intervals in which given value lies

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

Given two coordinates, Print the line equation

Objective: Given two coordinates (x1, y1) and (x2, y2). Write a java program to construct a line equation that passes through the given coordinates. Line Equation: Given two coordinates (x1, y1) and (x2, y2) the line equation is  y – y1 = m(x – x1) where m is the slope of the line, where slope … Read more Given two coordinates, Print the line equation

Print all middle elements of the given matrix/2D array.

Objective: Given a two-dimensional array, write a program to print all middle elements of the array. Or In other words, print all the elements from the array except boundary elements. Example: int [][] grid = new int[][] { {1, 2, 3, 4, 8}, {5, 6, 7, 8, 9}, {9, 8, 7, 6, 7}, {5, 4, … Read more Print all middle elements of the given matrix/2D array.

Depth-First Search (DFS) in 2D Matrix/2D-Array – Recursive Solution

Objective: Given a two-dimensional array or matrix, Do the depth-First Search (DFS) to print the elements of the given matrix. Implement the Depth-first traversal in a recursive manner. Example: int [][] grid = new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output: Depth-First Traversal: … Read more Depth-First Search (DFS) in 2D Matrix/2D-Array – Recursive Solution

Hamming Distance between two given integers

Objective: Given two integers, write an algorithm to calculate the hamming distance between the integers.  Hamming Distance: Hamming distance between two integers is the number of positions at which the bits are different. Example: X = 2, Y = 4 Hamming distance: 2 2 = 0 1 0 4 = 1 0 0 There are … Read more Hamming Distance between two given integers

Breadth-First Search (BFS) in 2D Matrix/2D-Array

Objective: Given a two-dimensional array or matrix, Do the breadth-First Search (BFS) to print the elements of the given matrix. Implement a Breadth-first traversal in an iterative manner. Example: int [][] grid = new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output: Breadth-First Traversal: … Read more Breadth-First Search (BFS) in 2D Matrix/2D-Array

Find all the Armstrong numbers in the given range

Objective: Given a range of integers, wrote a program to find all the Armstrong numbers in that range. What is Armstrong number: Determine the number of digits in the number. Call that n. Then take every digit in the number and raise it to the n power. Add all those together, and if your answer … Read more Find all the Armstrong numbers in the given range

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.

Check if given undirected graph is connected or not

Objective: Given an undirected graph, write an algorithm to find out whether the graph is connected or not.  Graph Connectivity: If each vertex of a graph is connected to one or multiple vertices then the graph is called a Connected graph whereas if there exists even one vertex which is not connected to any vertex … Read more Check if given undirected graph is connected or not

Depth-First Search (DFS) in 2D Matrix/2D-Array – Iterative Solution

Objective: Given a two-dimensional array or matrix, Do the depth-First Search (DFS) to print the elements of the given matrix. Implement the Depth-first traversal in an iterative manner. Example: int [][] grid = new int[][] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output: Depth-First Traversal: … Read more Depth-First Search (DFS) in 2D Matrix/2D-Array – Iterative Solution

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