Programming Articles

Page 506 of 2547

Program to find maximum number of courses we can take based on interval time in Python?

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

Suppose we have a list of intervals in the form [start, end], representing the start and end time of courses. We need to find the maximum number of courses we can take, assuming we can only take one course at a time and the start of a course must be after the end of the previous course. So, if the input is like times = [[3, 6], [6, 9], [7, 8], [9, 11]], then the output will be 3, as we can take courses [[3, 6], [7, 8], [9, 11]]. Algorithm To solve this, we will follow ...

Read More

Program to find number of boxes that form longest chain in Python?

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

Suppose we have a list of boxes, where each entry has two values [start, end] (start < end). We can join two boxes if the end of one is equal to the start of another. We have to find the length of the longest chain of boxes. So, if the input is like boxes = [[4, 5], [5, 6], [4, 8], [1, 2], [2, 4]], then the output will be 4, as we can form the chain: [1, 2] → [2, 4] → [4, 5] → [5, 6] Algorithm To solve this, we will follow these steps ...

Read More

Program to check whether first player win in candy remove game or not in Python?

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

Suppose we have a list of numbers called candies and someone is playing a game against their friend. In each round, a player can remove any two consecutive candies with the same value. Whoever cannot pick up any candies loses. If player1 starts first, we have to check whether player1 will win or not. So, if the input is like candies = [2, 2, 5], then the output will be True, as if player1 picks the consecutive 2s, then the other player cannot pick any candies and loses. Algorithm To solve this, we will follow these steps ...

Read More

Program to evaluate Boolean expression from a string in Python?

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

Boolean expressions in strings can be evaluated by parsing the tokens and using a stack to handle operator precedence and parentheses. This approach splits the expression into components and processes them systematically. Problem Statement Given a string containing a boolean expression with operators "and" and "or", we need to evaluate it and return the result. The expression may contain parentheses that should be evaluated first. For example, if the input is s = "T and (F or T)", the output should be True. Algorithm Approach The solution uses a stack-based approach to handle the expression ...

Read More

Program to find minimum bus fare for travelling all days in Python?

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

When traveling by bus on specific days, we need to find the minimum cost using three ticket types: 1-day pass ($2), 7-day pass ($7), and 30-day pass ($25). This is a classic dynamic programming problem where we optimize the cost for each day. Problem Understanding Given a list of travel days, we need to choose the best combination of tickets. For example, if we travel on days [1, 3, 5, 6, 28], buying a 7-day pass covers days 1-7, and a 1-day pass for day 28 gives us a total cost of $9. Algorithm Steps ...

Read More

Program to count number of swaps required to change one list to another in Python?

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

Suppose we have two lists of numbers L1 and L2, the length of each list is n and each value is unique to its list, and values are in range 1 to n. We need to find the minimum number of adjacent swaps required to transform L1 to L2. So, if the input is like L1 = [0, 1, 2, 3] and L2 = [2, 0, 1, 3], then the output will be 2. We can swap 1 and 2 to get [0, 2, 1, 3], then swap 0 and 2 to get [2, 0, 1, 3], which matches ...

Read More

Program to find maximum profit we can get by buying and selling stocks with a fee in Python?

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

When trading stocks with transaction fees, we need to find the maximum profit possible by buying and selling stocks any number of times. Each sell transaction incurs a fee that reduces our profit. The problem can be solved using dynamic programming with recursion. We track two states: whether we currently hold stock (flag=1) or not (flag=0). Example Let's say we have stock prices [2, 10, 4, 8] and a transaction fee of 3. The optimal strategy is: Buy at price 2, sell at price 10: profit = 10 - 2 - 3 = 5 Buy ...

Read More

Program to find number of places are safe when bomb explodes in Python?

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

Suppose we have a 2D binary matrix where 1 represents a bomb and 0 represents an empty cell. When a bomb explodes, all spaces along the same row and column are damaged. We need to find the number of safe positions where we can stand without getting damaged. So, if the input is like ? 1 1 0 0 0 0 0 0 0 Then the output will be 2, as the bottom-right cell (2, 2) and the middle-right cell (1, 2) are safe positions. Approach To ...

Read More

Program to find minimum possible sum by changing 0s to 1s k times from a list of numbers in Python?

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

Given a list of numbers and a value k, we need to perform k operations where each operation flips a 0 bit to 1 in any number's binary representation. The goal is to minimize the final sum of all numbers. For example, if nums = [4, 7, 3] and k = 2, the binary representations are: 4 → 100 (binary) 7 → 111 (binary) 3 → 011 (binary) We can flip two 0 bits in 4 (positions 0 and 1) to make it 111 (7). Final sum: 7 + 7 + 3 = 17. ...

Read More

Program to find starting index of the child who receives last balloon in Python?

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

Suppose we have n children standing in a circle, waiting to receive balloons. The distribution starts with the kth child (indexing from 0), and after giving them a balloon, they leave the circle. The process continues clockwise, with every kth child receiving a balloon until only one child remains. We need to find the starting index of the child who receives the last balloon. This is a variation of the famous Josephus problem, where we eliminate every kth person from a circle. Problem Example If the input is n = 3 and k = 2, then the ...

Read More
Showing 5051–5060 of 25,466 articles
« Prev 1 504 505 506 507 508 2547 Next »
Advertisements