Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 52 of 377

Program to find sum of all numbers formed by path of a binary tree in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 347 Views

Suppose we have a binary tree where each node is containing a single digit from 0 to 9. Now each path from the root to the leaf represents a number with its digits in order. We have to find the sum of numbers represented by all paths in the tree.So, if the input is likethen the output will be 680 as 46 (4 → 6), 432 (4 → 3 → 2), 435 (4 → 3 → 5), and their sum is 913.To solve this, we will follow these steps −Define a function solve() . This will take root, string:= blank ...

Read More

Program to count number of surrounded islands in the matrix in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 618 Views

Suppose we have a binary matrix. Where 1 represents land and 0 represents water. As we know an island is a group of 1s that are grouped together whose perimeter is surrounded by water. We have to find the number of completely surrounded islands.So, if the input is likethen the output will be 2, as there are three islands, but two of them are completely surrounded.To solve this, we will follow these steps −Define a function dfs() . This will take i, jif i and j are not in range of matrix, thenreturn Falseif matrix[i, j] is same as 0, ...

Read More

Program to find lowest sum of pairs greater than given target in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 286 Views

Suppose we have a list of numbers called nums and another value target. We have to find the lowest sum of pair of numbers that is larger than target.So, if the input is like nums = [2, 4, 6, 10, 14] target = 10, then the output will be 12, as we pick 2 and 10To solve this, we will follow these steps −sort the list numsn := size of numsanswer := 10^10i := 0, j := n - 1while i < j, doif nums[i] + nums[j] > target, thenanswer := minimum of answer and (nums[i] + nums[j])j := j ...

Read More

Program to find the final ranking of teams in order from highest to lowest rank in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 405 Views

Suppose we have a list of strings called votes, here each entry is in lowercase letters and they are representing votes on candidates in order from highest to lowest preference. Here the rank of a candidate depends first by the number of votes received on the highest preference. Now if there are ties, we shall check the number of votes received on next highest preference, and so on. If there are still ties, then they will be ranked alphabetically. So we have to find the final ranking of the teams in order from highest to lowest ranked.So, if the input ...

Read More

Program to find number of combinations of coins to reach target in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have a list of coins and another value amount, we have to find the number of combinations there are that sum to amount. If the answer is very large, then mod the result by 10^9 + 7.So, if the input is like coins = [2, 5] amount = 10, then the output will be 2, as we can make these combinations − [2, 2, 2, 2, 2], [5, 5]To solve this, we will follow these steps −m := 10^9 + 7dp := a list of size same as amount + 1, and fill it with 0dp[0] := 1for ...

Read More

Program to check whether we can color a tree where no adjacent nodes have the same color or not in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 275 Views

Suppose we have a binary tree where the value of each node represents its color. There are at most 2 colors in a tree. We have to check whether it is possible to swap the colors of the nodes any number of times so that no two connected nodes have the same color.So, if the input is likethen the output will be True as we can getTo solve this, we will follow these steps −colors := an empty mapprop := an empty mapDefine a function dfs() . This will take node, and flagif node is null, thenreturncolors[value of node] := ...

Read More

Program to generate matrix where each cell holds Manhattan distance from nearest 0 in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 267 Views

Suppose we have a binary matrix. We have to find the same matrix, but each cell's value will be the Manhattan distance to the nearest 0. We can assume at least one 0 exists in the matrix.So, if the input is like101101110then the output will be101101210as only the bottom left cell has distance of 2 to the nearest 0.To solve this, we will follow these steps −m := row size of matrix, n := column size of matrixfor y in range 0 to m, dofor x in range 0 to n, doif matrix[y, x] is non-zero, thenmatrix[y, x] := infinityfor ...

Read More

Program to count number of paths whose sum is k in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 254 Views

Suppose we have a binary tree and another value k, we have to find the number of unique node to sub child paths are there which sums to k.So, if the input is likeand k = 5, then the output will be 2, as the paths are [2, 3] and [1, 4]To solve this, we will follow these steps −count := a map initially holds value 1 for key 0ans := 0, prefix := 0Define a function dfs() . This will take nodeif node is not null, thenprefix := prefix + value of nodeans := ans + (count[prefix - target], ...

Read More

Program to find number of days it will take to burn all trees in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 914 Views

Suppose we have a 2D matrix represents a forest where there are three types of cells: 0 empty cell 1 tree cell 2 tree on fire cell Every day, a tree catches fire when there is an adjacent (top, down, left, right, not diagonal) tree is on fire. We have to find the number of days it would take for every tree to be on fire. If that is not possible return -1.So, if the input is like121101111then the output will be 4, To solve this, we will follow these steps −ans := 0twos := a new listfor i in ...

Read More

Program to find a target value inside given matrix or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 482 Views

Suppose we have a 2D matrix, where each row and column is sorted in non-decreasing order, we have to check whether given target is present inside it or not.So, if the input is like243034316632And target = 31, then the output will be TrueTo solve this, we will follow these steps −col := column size of matrix - 1for i in range 0 to row size of matrix, dowhile matrix[i, col] > target and col >= 0, docol := col - 1if matrix[i, col] is same as target, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Exampleclass ...

Read More
Showing 511–520 of 3,768 articles
« Prev 1 50 51 52 53 54 377 Next »
Advertisements