Python Articles

Page 634 of 855

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

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

Suppose we have n cities represented as numbers 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 or not. 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 from any city to any other city through the available roads. Approach To solve this problem, we need to check if the ...

Read More

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

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

Suppose we have a binary tree; we have to check whether this is a complete binary tree or not. A complete binary tree is one where all levels are completely filled except possibly the last level, and all nodes in the last level are as far left as possible. So, if the input is like ? 9 7 10 ...

Read More

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

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

Suppose we have a matrix M and a target matrix T with the same number of rows and columns. We can perform an operation where we flip a particular column in the matrix so that all 1s become 0s and all 0s become 1s. If we can reorder the matrix rows for free, we need to find the minimum number of operations required to turn M into T. If there is no solution, return -1. Problem Example Given matrices M and T ? Matrix M: 00 10 11 Target T: 01 10 11 ...

Read More

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

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

When we have a two-dimensional matrix where each cell contains a value representing its color, we need to find the minimum number of operations to make all cells the same color. Adjacent cells (top, bottom, left, right) with the same color form connected groups, and in each operation, we can change all cells in one group to any color. The key insight is that we should keep the color group with the most connected components unchanged, and convert all other groups to that color. Problem Understanding Given the matrix: 2 2 2 2 ...

Read More

Program to find largest sum of any path of a binary tree in Python

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

Suppose we have a binary tree, we have to find the largest sum of any path that goes from the root node to the leaf node. So, if the input is like ? 5 1 9 7 10 6 8 ...

Read More

Program to find circular greater element to the right in Python

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

The circular next greater element problem requires finding the next greater element to the right of each element in an array, with the array treated as circular. If no greater element exists, we assign -1. For example, given [4, 5, 1, 3], the result is [5, -1, 3, 4] because: 4's next greater element is 5 5 has no greater element, so -1 1's next greater element is 3 3's next greater element is 4 (wrapping around) Algorithm Steps We use a stack-based approach with double iteration to handle the circular nature: Initialize ...

Read More

Program to arrange cards so that they can be revealed in ascending order in Python

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

This problem involves arranging cards in a specific order so that when revealed using a particular pattern, they appear in ascending order. The revelation pattern is: remove the top card, then move the next card to the back, and repeat until no cards remain. Understanding the Problem Given cards [1, 2, 3, 4, 5, 6, 7, 8], we need to arrange them as [1, 5, 2, 7, 3, 6, 4, 8] so that the revelation process yields ascending order ? The revelation process works as follows: Remove top card (reveal it) Move the next top card ...

Read More

Program to partition two strings such that each partition forms anagram in Python

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

Suppose we have two non-empty strings s and t that are of the same length. We have to partition them into substrings such that each pair of s and t substring is the same size and they are anagrams of each other. Now find the cut indexes such that it results in the maximum number of cuts of s and t. If no result is found, then return empty list. So, if the input is like s = "bowcattiger" t = "owbactietgr", then the output will be [0, 3, 5, 6, 10], as we can partition the string into ...

Read More

Program to find sum of the sum of all contiguous sublists in Python

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

Given a list of numbers, we need to find the sum of all possible contiguous subarrays and return the result modulo 109 + 7. This problem can be solved efficiently by calculating how many times each element appears in all subarrays. For the input nums = [3, 4, 6], we have these contiguous subarrays: [3], [4], [6], [3, 4], [4, 6], [3, 4, 6]. Their sums are 3, 4, 6, 7, 10, 13 respectively, totaling 43. Understanding the Pattern Instead of generating all subarrays, we can calculate how many times each element contributes to the final sum. ...

Read More

Program to check we can reach end of list by starting from k in Python

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

Suppose we have a list of numbers called nums and another number k. If we start at index k and at any index i, we can go either left or right by exactly nums[i] number of steps. We have to check whether we can reach the end of the list or not. So, if the input is like nums = [0, 0, 2, 1, 3, 3, 1, 1] and k = 2, then the output will be True, as if we start at index 2, then jump to index 4 and then jump to the last index 7. ...

Read More
Showing 6331–6340 of 8,546 articles
« Prev 1 632 633 634 635 636 855 Next »
Advertisements