Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 31 of 377

Program to find top view of a binary tree in Python

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

Suppose we have a binary tree, we have to find the top view of the tree, they will be sorted left−to−right.So, if the input is like image, then the output will be [3, 5, 8, 6, 9], as 3 is above 2 and 5 is above 7 so they are not visible.To solve this, we will follow these steps −view := a new empty mapq := a double ended queueinsert pair (root, 0) at the end of qstart := inf, end := −infwhile q is not empty, do(node, coord) := left element of q, then remove left element of qstart ...

Read More

Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python

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

Suppose we have a number k, now consider the smallest positive integer value x where all values from 1 to k divide evenly. In other words, consider the smallest value x where x is divisible by all numbers from 1 through k. We have to find the number of trailing zeroes in x.So, if the input is like k = 6, then the output will be 0, as the smallest x here is 60, 60 can be divided using 1, 2, 3, 4, 5 and 6. There is only one trailing zeroes in 60.To solve this, we will follow these steps −res := 0x := 1while x * 5

Read More

Program to count number of configurations are there to fill area with dominos and trominos in C++

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

Suppose we have two shapes, Domino and Tromino. Dominos are 2 x 1 shape and Trominos are ‘L’ like shape. They can be rotated like below −If we have a number n, we have to find number of configurations to fill a 2 x n board with these two types of pieces. As we know in tiling, every square must be covered by a tile.So if the input is 3, then the output will be 5. So the arrangements can be [XYZ XXZ XYY XXY XYY] and [XYZ YYZ XZZ XYY XXY], here different letters are used for different tiles.To ...

Read More

Check if elements of Linked List are present in pair in Python

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

Suppose we have a singly linked list. We have to check whether each element in the given linked list is present in a pair, in other words all elements occur even no. of times.So, if the input is like list = [2, 5, 5, 2, 3, 3], then the output will be True.To solve this, we will follow these steps −xor_res := 0, current_node := head of linked listwhile current_node is not null, doxor_res := xor_res XOR value of current_nodecurrent_node := next of current_nodereturn False when xor_res is non-zero otherwise TrueExampleLet us see the following implementation to get better understanding ...

Read More

Program to count number of distinct characters of every substring of a string in Python

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

Suppose we have a lowercase string s, we have to find the sum of the count of characters that are distinct in every substring of s. If the answer is very large then return result mod 10^9+7.So, if the input is like s = "xxy", then the output will be 6, as the substrings and their counts are −"x" : 1"x" : 1"y" : 1"xx" : 0 (as "x" is not distinct)"xy" : 2"xxy" : 1 ("as "x" is not distinct)To solve this, we will follow these steps −m := 10^9 + 7prev_seen := a new empty mapans := 0Define ...

Read More

Check If every group of a is followed by a group of b of same length in Python

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

Suppose we have a lowercase string s with only two characters a and b. We have to check whether every group of consecutive a's is followed by group of consecutive b's of equal length.So, if the input is like s = "abaaabbbaabbaabbab", then the output will be True, as all groups are (ab), (aaabbb), (aabb), (aabb), (ab).To solve this, we will follow these steps −a_count := 0, string_len := size of si := 0while i < string_len, dowhile i < string_len and s[i] is 'a', doa_count := a_count + 1i := i + 1while i < string_len and s[i] is ...

Read More

Program to find number of unique subsequences same as target in C++

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

Suppose we have two lowercase strings s and t, we have to find the number of subsequences of s that are equal to t. If the answer is very large then return result by 10^9 + 7.So, if the input is like s = "abbd" t = "bd", then the output will be 2, as there are two possible subsequences "bd".s[1] concatenate s[3]s[2] concatenate s[3].To solve this, we will follow these steps −m := 10^9 + 7if size of t is same as 0, then −return 0if t is same as s, then −return 1if size of t > size ...

Read More

Program to count number of walls required to partition top-left and bottom-right cells in Python

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

Suppose we have a 2d binary matrix where 0 represents empty cell and 1 represents a wall. We have to find the minimum number cells that need to become walls so that there will be no path between top−left cell and bottom-right cell. We cannot put walls on the top−left cell and the bottom−right cell. We can move only left, right, up and down not diagonally.So, if the input is like0000010001100000then the output will be 2, 0100010001100010To solve this, we will follow these steps −R := row count of matrix, C := column count of matrixvisited := a new settin ...

Read More

Program to check person can reach top-left or bottomright cell avoiding fire or not in Python

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

Suppose we have a 2D matrix with few different values like below −0 for empty cell1 for a person2 for fire3 for a wallNow assume there is only one person and in each turn the fire expands in all four directions (up, down, left and right) but fire cannot expand through walls. We have to check whether the person can move to either the top-left corner or the bottom-right corner or the matrix. We have to keep in mind that in each turn, the person moves first, then the fire expands. If the person makes it to any of the ...

Read More

Program to find maximum profit we can make after k Buy and Sell in python

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

Suppose we have a list of numbers called nums that is representing the stock prices of a company in chronological order and we also have another value k, we have to find the maximum profit we can make from up to k buys and sells (We must buy before sell, and sell before buy).So, if the input is like prices = [7, 3, 5, 2, 3] k = 2, then the output will be 3, as we can buy at 3, then sell at 5, again buy at 2, and sell at 3.To solve this, we will follow these steps ...

Read More
Showing 301–310 of 3,768 articles
« Prev 1 29 30 31 32 33 377 Next »
Advertisements