Python Articles

Page 635 of 855

Program to find how many ways we can climb stairs (maximum steps at most k times) in Python

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

Suppose we have a staircase with n steps and we also have another number k. Initially we are at stair 0, and we can climb up either 1, 2 or 3 steps at a time, but we can only climb 3 stairs at most k times. We need to find the number of ways we can climb the staircase. So, if the input is like n = 5, k = 2, then the output will be 13, as there are different ways we can climb the stairs − [1, 1, 1, 1, 1] [2, 1, 1, 1] ...

Read More

Program to find how many ways we can climb stairs in Python

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

Suppose we have a staircase with n steps, and we can climb up either 1 or 2 steps at a time. We need to find the number of unique ways we can climb the staircase. The order of the steps matters, so each different sequence counts as a unique way. If the answer is very large, we return the result modulo 10^9 + 7. For example, if n = 5, there are 8 unique ways ? 1, 1, 1, 1, 1 2, 1, 1, 1 1, 2, 1, 1 1, 1, 2, 1 1, 1, 1, ...

Read More

Program to find minimum number of busses are required to pass through all stops in Python

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

Suppose we have a list of numbers called nums representing bus stops on a line where nums[i] shows the time a bus must arrive at station i. Since buses can only move forward in time, we need to find the minimum number of buses required to pass through all stops. So, if the input is like nums = [1, 2, 7, 9, 3, 4], then the output will be 2, as one bus can take stops [1, 2, 3, 4] and another can handle [7, 9]. Algorithm To solve this, we will follow these steps ? ...

Read More

Program to find maximum amount we can get by taking different items within the capacity in Python

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

The 0/1 Knapsack problem is a classic optimization problem where we have items with weights and values, and we need to find the maximum value we can obtain within a given weight capacity. Each item can be taken at most once. Given two lists weights and values of the same length and a capacity k, where weights[i] and values[i] represent the weight and value of the i-th item, we need to find the maximum value achievable without exceeding the capacity limit. Problem Example If we have weights = [2, 3, 4], values = [2, 6, 4], and ...

Read More

Program to find the sum of elements that forms a Z shape on matrix in Python

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

Suppose we have one n x n matrix M, we have to find the sum of all elements that form a Z shape in the matrix. A Z-shape consists of three parts: the first row (top horizontal line), the diagonal from top-right to bottom-left (middle diagonal), and the last row (bottom horizontal line). Example Matrix So, if the input is like ? 4 3 2 9 1 8 2 5 6 Then the output will be 23, as elements are [4+3+2+1+2+5+6] = 23. Algorithm To solve this, we will follow these steps ? n := row count of matrix if n

Read More

Program to check we can spell out the target by a list of words or not in Python

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

Given a list of numbers and a starting index, we need to determine if we can reach the end of the list by jumping. At any index i, we can move left or right by exactly nums[i] steps. For example, with nums = [0, 0, 2, 1, 3, 3, 1, 1] and starting index k = 2, we can reach the end by jumping from index 2 to index 4, then to the last index 7. Algorithm We'll use a breadth-first search (BFS) approach to explore all possible positions: Initialize a visited array to track ...

Read More

Program to find the maximum profit we can get by buying on stock market multiple times in Python

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

Suppose we have a list of prices representing the stock prices of a company in chronological sequence, we have to find the maximum profit we could have made from buying and selling that stock any number of times. We have to keep in mind that we must buy before we can sell it. So, if the input is like prices = [10, 50, 30, 40, 60], then the output will be 70, as we can buy at 10, sell at 50, buy at 30, and sell at 60. Algorithm To solve this, we will follow these steps: ...

Read More

Program to check whether given graph is bipartite or not in Python

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

A bipartite graph is an undirected graph where vertices can be divided into two disjoint sets such that no two vertices within the same set are adjacent. In other words, every edge connects vertices from different sets. For example, if we have the following graph: 0 1 2 3 4 ...

Read More

Program to find the maximum profit we can get by buying on stock market once in Python

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

Finding the maximum profit from buying and selling stock once is a classic programming problem. Given a list of stock prices in chronological order, we need to find the maximum profit possible by buying once and selling once, where we must buy before selling. For example, if the input is prices = [10, 12, 9, 6, 8, 12], the output will be 6, as we can buy at price 6 and sell at price 12. Algorithm To solve this efficiently, we use a single pass approach ? Initialize max_profit to 0 Initialize min_price to infinity ...

Read More

Program to sort all vowels at beginning then the consonants, are in sorted order in Python

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

Suppose we have a lowercase alphabet string s, we have to find a string with all the vowels of s in sorted sequence followed by all consonants of s in sorted sequence. So, if the input is like "helloworld", then the output will be "eoodhlllrw", as vowels are "eooo" in sorted order and consonants are "dhlllrw" in sorted order. Algorithm To solve this, we will follow these steps − vowel_str := blank string, consonant_str := blank string for each character c in s, do if c is a vowel, then vowel_str := vowel_str ...

Read More
Showing 6341–6350 of 8,546 articles
« Prev 1 633 634 635 636 637 855 Next »
Advertisements