Find count of pair of nodes at even distance | Set 2 (Using BFS)
Given a connected acyclic graph with N nodes numbered from 1 to N and N-1 edges, find out the pair of nodes that are at even distance from each other....
read more
Size of all connected non-empty cells of a Matrix
Given a binary matrix mat[][], the task is to find the size of all possible non-empty connected cells. An empty cell is denoted by 0 while a non-empty cell is denoted by 1. Two cells are said to be connected if they are adjacent to each other horizontally or vertically, i.e. mat[i][j] = mat[i][j – 1] or mat[i][j] = mat[i][j + 1] or mat[i][j] = mat[i – 1][j] or mat[i][j] = mat[i + 1][j]....
read more
Minimum total power consumption by both the current “+” & “-“
Given a matrix of size rows x cols called “power,” which represents the power associated with each cell in the field. There are two types of current flow in the field: “+” current starts from the top left corner, while “-” current starts from the top right corner. Both types of current can move in three directions: from a cell (i, j) to either (i + 1, j – 1), (i + 1, j), or (i + 1, j + 1). The power consumption between two cells is calculated as the absolute difference between the power values of the two cells: abs(power[i + 1][j + 1] – power[i][j]). The task is to calculate the minimum total power consumption by both the current “+” & “-” to reach the last row....
read more
Count pairs of nodes having minimum distance between them equal to the difference of their distances from root
Given an N-ary Tree consisting of N nodes valued from [1, N], where node 1 is the root, the task is to count the pairs of nodes having minimum distance between them equal to the difference between the distances of both the nodes from the root....
read more
Count of distinct colors in a subtree of a Colored Tree with given min frequency for Q queries
Given a N-ary tree with some colour associated with every node and Q queries. Each query contains two integers A and X. The task is to count all distinct colors in a subtree rooted at A, having frequency of colors greater than or equal to X in that subtree.Examples:...
read more
Python Program To Recursively Remove All Adjacent Duplicates
Given a string, recursively remove adjacent duplicate characters from the string. The output string should not have any adjacent duplicates. See the following examples....
read more
Find all subsequences with sum equals to K
Given an array arr[] of length N and a number K, the task is to find all the subsequences of the array whose sum of elements is K...
read more
Print all combinations generated by characters of a numeric string which does not exceed N
Given a numeric string S of length M and an integer N, the task is to find all distinct combinations of S (repetitions allowed) that are at most N....
read more
Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
Given three integers N, M and K. The task is to find the number of ways to fill N positions using M colors such that there are exactly K pairs of different adjacent colors....
read more
Python Code for time Complexity plot of Heap Sort
Prerequisite : HeapSort...
read more
Count all possible paths from source to destination in given 3D array
Given three integers M, N and K, the task is to count all the possible paths from the cell (0, 0, 0) to cell (M-1, N-1, K-1) in a matrix of size (M, N, K).  Movement is allowed only in three directions, which are along the positive direction of the three axes i.e. from any cell (i, j, k) movement is allowed to cells (i+1, j, k), (i, j+1, k) and (i, j, k+1)....
read more
Level order traversal in spiral form using stack and multimap
Given a binary tree of N nodes, the task is to print level order traversal in a spiral form. In spiral form, nodes at the first and second level of tree are printed normally (left to right), after which nodes at alternate levels are printed in reverse order. Examples:...
read more