Python Articles

Page 339 of 855

Program to detect cycles in 2D grid in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 341 Views

Cycle detection in a 2D grid is a common graph problem where we need to find if there exists a path that starts and ends at the same cell. A cycle must have at least 4 cells and we can only move to adjacent cells with the same character value. Problem Understanding Given a 2D grid of characters, we need to detect if there's a cycle. The conditions are ? A cycle has length 4 or more We can move in four directions (up, down, left, right) Adjacent cells must have the same character Cannot revisit ...

Read More

Program to find minimum number of days to eat N oranges in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 350 Views

Suppose we have n oranges in the kitchen. We can eat some oranges every day following these rules: Eat 1 orange If n is even, eat n/2 oranges If n is divisible by 3, eat 2*(n/3) oranges We can select only one option each day. We need to find the minimum number of days to eat all n oranges. Example Walkthrough If the input is n = 10, the output will be 4 days ? Day 1: Eat 1 orange, 10 − 1 = 9 Day 2: Eat 6 oranges (2*(9/3)), 9 ...

Read More

Program to find minimum cost to cut a stick in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 859 Views

Given a wooden stick of length n and an array of cut positions, we need to find the minimum cost to make all cuts. The cost of each cut equals the length of the current stick segment being cut. This is a classic dynamic programming problem that can be solved optimally by choosing the right order of cuts. Problem Understanding Consider a stick of length 7 with cuts at positions [5, 1, 4, 3]. The total cost depends on the order we make the cuts ? Each cut costs the length of the current stick segment ...

Read More

Program to find longest awesome substring in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 603 Views

An awesome substring is a non-empty substring where we can rearrange characters through swaps to form a palindrome. To create a palindrome, at most one character can have an odd frequency. We need to find the length of the longest awesome substring in a numeric string. For example, in string "4353526", the substring "35352" (length 5) is awesome because we can rearrange it to form the palindrome "35253". Algorithm Overview We use a bitmask approach where each bit represents whether a digit (0-9) has appeared an odd number of times. Two positions with the same bitmask or ...

Read More

Program to find the maximum score from all possible valid paths in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 665 Views

Suppose we have two arrays nums1 and nums2. A valid path is defined as follows − Select nums1 or nums2 to traverse (from index-0). Traverse the array from left to right. Now, if we are moving through any value that is present in nums1 and nums2 we can change the path to the other array. Here the score is the sum of unique values in a valid path. We have to find the maximum score we can get of all possible valid paths. If the answer is too large then ...

Read More

Program to find min length of run-length encoding after removing at most k characters in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 311 Views

Run-length encoding is a string compression method that replaces consecutive identical characters with the character followed by its count. For example, "xxyzzz" becomes "x2yz3". In this problem, we need to find the minimum length of the run-length encoded string after removing at most k characters. Problem Understanding Given a string s and integer k, we can delete at most k characters to minimize the run-length encoded length. The key insight is that we want to create longer consecutive sequences by strategically removing characters. Example For s = "xxxyzzzw" and k = 2: Original: "xxxyzzzw" → ...

Read More

Program to find maximum score from performing multiplication operations in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 402 Views

Suppose we have two arrays nums and multipliers of size n and m respectively (n >= m). We want to perform exactly m operations to achieve the maximum score. Our initial score is 0. In each operation, we can: Select one value x from either the start or the end of the nums array Add multipliers[i] * x to the score Remove x from the array nums We need to find the maximum score after performing m operations. Example If the input is nums = [5, 10, ...

Read More

Program to find minimum number of increments on subarrays to form a target array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 399 Views

Suppose we have an array called target with positive values. Now consider an array initial of same size with all zeros. We have to find the minimum number of operations required to generate a target array from the initial if we do this operation: Select any subarray from initial and increment each value by one. So, if the input is like target = [2, 3, 4, 3, 2], then the output will be 4. How It Works The key insight is that we only need to increment when moving to a higher value. When the array decreases, ...

Read More

Program to find minimum number of operations to move all balls to each box in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 826 Views

Suppose we have a binary string called boxes, where boxes[i] is '0' indicates the ith box is empty, and '1' indicates it contains one ball. In one operation, we can move one ball from a box to an adjacent box. We need to find an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box. Problem Understanding If the input is boxes = "1101", then the output will be [4, 3, 4, 5] ? To put all balls in first box: take from ...

Read More

Program to find maximum number of non-overlapping substrings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 710 Views

Given a string with lowercase letters, we need to find the maximum number of non-overlapping substrings where each substring contains all occurrences of every character it includes. Problem Understanding The algorithm must satisfy two conditions ? Substrings are non-overlapping A substring containing character 'ch' must include all occurrences of 'ch' in the string For example, if we have "pqstpqqprrr", the valid substrings are ["pqstpqqprrr", "pqstpqqp", "st", "s", "t", "rrr"]. We want the maximum count, so we choose ["s", "t", "rrr"]. Algorithm Approach The solution uses a greedy approach ? Find ...

Read More
Showing 3381–3390 of 8,546 articles
« Prev 1 337 338 339 340 341 855 Next »
Advertisements