Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 42 of 377

Program to find shortest cycle length holding target in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 452 Views

Finding the shortest cycle containing a specific target node in a directed graph is a common graph problem. We can solve this using Breadth-First Search (BFS) to explore paths from the target node until we find a cycle back to it. Problem Understanding Given an adjacency list representation of a directed graph and a target node, we need to find the shortest cycle that contains the target node. If no such cycle exists, return -1. ...

Read More

Program to find total cost for completing all shipments in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 364 Views

We need to find the total cost for completing all shipments between ports, where the cost is the shortest path distance between each pair of ports. This problem uses the Floyd-Warshall algorithm to find all shortest paths in a graph. Problem Understanding Given a list of ports where ports[i] represents connected ports from port i, and shipments as pairs [i, j], we need to find the minimum total cost. Each edge has weight 1, so the cost is the shortest path length. For the example: ports = [[1, 4], [2], [3], [0, 1]], shipments = [[1, 4]], ...

Read More

Program to convert a string to zigzag string of line count k in python

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

The zigzag string conversion arranges characters in a zigzag pattern across k lines. Starting from the top line, we move diagonally down until reaching the bottom (kth line), then move diagonally up back to the top, and repeat this pattern. For example, if the input is s = "ilovepythonprogramming" and k = 5, the zigzag pattern looks like this: i h r l t o a o y ...

Read More

Program to find length of longest alternating path of a binary tree in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 337 Views

Suppose we have a binary tree, we have to find the longest path that alternates between left and right child and going down. An alternating path means we switch between going left and right at each level. So, if the input is like ? 2 3 4 5 ...

Read More

Program to find length of longest word that can be formed from given letters in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 385 Views

Suppose we have a list of words and a string called letters, we have to find the size of the longest word that can be made by rearranging the given letters. In the letters there may be asterisk character (*) which can match any character. It is not necessary to use all the letters. So, if the input is like words = ["prince", "rice", "price", "limit", "hello"] letters = "*r**ce*", then the output will be 6, as the longest word we can make is "prince" with length 6. Algorithm To solve this, we will follow these steps ...

Read More

Program to count number of word concatenations are there in the list in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 239 Views

This problem finds how many words in a list are formed by concatenating other words from the same list. We can reuse words multiple times during concatenation. Given the input words = ["hello", "world", "helloworld", "famous", "worldfamous", "programming"], the output is 2 because "helloworld" is formed by concatenating "hello" + "world", and "worldfamous" is formed by "world" + "famous". Algorithm We use a Trie data structure combined with depth-first search (DFS) ? Build a Trie: Store all words in a trie for efficient prefix matching DFS Search: For each ...

Read More

Program to find nearest time by reusing same digits of given time in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 409 Views

Given a 24-hour time string in "hh:mm" format, we need to find the next closest time that can be formed by reusing the same digits. We can reuse any digit from the original time as many times as needed. For example, if the input is "03:15", the output will be "03:30" since it's the nearest valid time using only digits 0, 3, 1, and 5. Algorithm Approach We'll use backtracking to generate all possible valid times using the given digits, then find the next time in chronological order ? Extract all four digits from the ...

Read More

Program to find minimum number of days to wait to make profit in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 310 Views

Suppose we have a list of prices representing the daily stock market prices of a company in chronological sequence. We have to find a same length list where the value at index i will be the minimum number of days we would have to wait until we make a profit. If there is no such way to make a profit, the value should be 0. So, if the input is like prices = [4, 3, 5, 9, 7, 6], then the output will be [2, 1, 1, 0, 0, 0] Algorithm To solve this problem, we will ...

Read More

Program to find all possible strings typed using phone keypad in python

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

Suppose we have a string containing digits from 2-9. We have to find all possible letter combinations that the number could generate. One mapping of digit to letters (just like on the telephone buttons) is given below ? 1 2a b c 3d e f 4g h i 5j k l 6m n o 7p q r s 8t u v 9w x y z * 0 # For example, if the given string is "49", then the possible strings will be ['gw', 'gx', 'gy', 'gz', ...

Read More

Program to check whether a board is valid N queens solution or not in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 967 Views

The N-Queens puzzle is a classic problem where we need to place N queens on an N×N chessboard such that no two queens attack each other. In this article, we'll learn how to verify whether a given board configuration is a valid N-Queens solution. A valid N-Queens solution must satisfy these conditions: Exactly N queens are placed on the board No two queens share the same row, column, or diagonal Understanding the Problem Given an N×N matrix with 1s representing queens and 0s representing empty cells, we need to check if it's a valid N-Queens ...

Read More
Showing 411–420 of 3,768 articles
« Prev 1 40 41 42 43 44 377 Next »
Advertisements