Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 82 of 377

Program to count number of operations required to all cells into same color in Python

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

Suppose we have a two-dimensional matrix M. Now in each cell contains a value that represents its color, and adjacent cells (top, bottom, left, right) with the same color are to be grouped together. Now, consider an operation where we set all cells in one group to some color. Then finally find the minimum number of operations required so that every cell has the same color. And when the color is transformed, it cannot be set again.So, if the input is like222211112321Then the output will be 2, as We can fill the group with 2 as color into 1 and ...

Read More

Program to count minimum number of operations to flip columns to make target in Python

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

Suppose we have a matrix M and a target matrix T with the same number of rows and columns. Now suppose an operation where we flip a particular column in matrix so that all 1s will be converted to 0s and all 0s will be converted to 1s. So if we can reorder the matrix rows for free, find the minimum number of operations required to turn M into T. If there is no solution, then return -1.So, if the input is like M =001011T =011011then the output will be 1, as first reorder the rows to−001110And then flip column ...

Read More

Program to check whether a binary tree is complete or not in Python

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

Suppose we have a binary tree; we have to check whether this is a complete binary tree or not. As we know in a complete binary tree the levels are filled with nodes except possibly the last and all nodes in the last level are as far left as possible.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps−q := a double ended queueinsert root at the end of qflag := Falsewhile q is not empty, dotemp := element after deleting from left of qif temp is null, thenflag := Trueotherwise when ...

Read More

Program to check we can visit any city from any city or not in Python

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

Suppose we have n cities represented as a number in range [0, n) and we also have a list of one-way roads that connects one city to another. We have to check whether we can reach any city from any city.So, if the input is like n = 3, roads = [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]], then the output will be True, as You can go 0 to 1 and 1 to 0To solve this, we will follow these steps−Define a function dfs() . This will take i, visited, gmark i as visitedfor ...

Read More

Program to find length of longest sublist with given condition in Python

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

Suppose we have a list of numbers called nums, we have to find the length of the longest sublist where 2 * minimum of sublist > maximum of sublist.So, if the input is like nums = [10, 2, 6, 6, 4, 4], then the output will be 4, as the sublist [6, 6, 4, 4] is the longest sublist that holds the criteria as 2 * 4 > 6.To solve this, we will follow these steps−ret := 0define two double ended queues minq and maxql := 0, r := 0while r < size of nums, don := nums[r]while minq and ...

Read More

Program to check string contains consecutively descending string or not in Python

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

Suppose we have a string s with some digits, we have to check whether it contains consecutively descending integers or not.So, if the input is like s = "99989796", then the output will be True, as this string is holding [99, 98, 97, 96]To solve this, we will follow these steps−Define a function helper() . This will take pos, prev_numif pos is same as n, thenreturn Truenum_digits := digit count of prev_numfor i in range num_digits - 1 to num_digits, doif s[from index pos to pos+i-1] and numeric form of s[from index pos to pos+i-1]) is same as prev_num - ...

Read More

Program to find the length of longest substring which has two distinct elements in Python

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

Suppose we have a string s, we have to find the length of the longest substring that contains at most 2 distinct characters.So, if the input is like s = "xyzzy", then the output will be 4, as "yzzy" is the longest substring with at most 2 unique characters.To solve this, we will follow these steps−start := 0c := a mapans := 0for end in range 0 to size of s, doc[s[end]] := c[s[end]] + 1while size of c > 2, doc[s[start]] := c[s[start]] - 1if c[s[start]] is 0, thendelete c[s[start]]start := start + 1ans := maximum of ans and ...

Read More

Program to find longest path between two nodes of a tree in Python

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

Suppose we have a binary tree; we have to find the longest path between any two nodes in the tree.So, if the input is like then the output will be 5 To solve this, we will follow these steps:ans := 0Define a function getMaxPath() . This will take nodeif node is null, thenreturn 0leftCnt := getMaxPath(left of node)rightCnt := getMaxPath(right of node)temp := 1 + maximum of leftCnt and rightCntans := maximum of ans and l+r+1From the main method do the following −getMaxPath(root)return ansLet us see the following implementation to get better understanding −Exampleclass TreeNode:    def __init__(self, val, left=None, right=None):   ...

Read More

Program to find sum of longest sum path from root to leaf of a binary tree in Python

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

Suppose we have a binary tree, we have to find the sum of the longest path from the root to a leaf node. If there are two same long paths, return the path with larger sum.So, if the input is likethen the output will be 20.To solve this, we will follow these steps −Define a function rec() . This will take currif curr is null, thenreturn(0, 0)bigger := maximum of rec(left of curr) , rec(right of curr)return a pair (bigger[0] + 1, bigger[1] + value of curr)From the main method do the following −ret := rec(root)return the 1th index of ...

Read More

Program to check whether parentheses are balanced or not in Python

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

Suppose we have a string s consisting of parenthesis "(" and ")". We have to check whether the parentheses are balanced or not.So, if the input is like s = "(()())(())", then the output will be TrueTo solve this, we will follow these steps −num_open := 0for each character c in s, doif c is same as ')', thenif num_open < 0, thennum_open := num_open - 1otherwise, return Falseotherwise, num_open := num_open + 1return inverse of num_openLet us see the following implementation to get better understanding −Exampleclass Solution:    def solve(self, s):       num_open = 0     ...

Read More
Showing 811–820 of 3,768 articles
« Prev 1 80 81 82 83 84 377 Next »
Advertisements