Python Articles

Page 342 of 855

Program to find minimum jumps to reach home in Python

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

Suppose there is an array called forbidden, where forbidden[i] indicates that the bug cannot jump to the position forbidden[i], and we also have three values a, b, and x. A bug's home is at position x on the number line. It is at position 0 initially. It can jump by following rules − Bug can jump exactly a positions to the right. Bug can jump exactly b positions to the left. Bug cannot jump backward twice in a row. Bug cannot jump ...

Read More

Program to find out the minimum rotations needed to maximize the profit from a Ferris wheel in Python

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

Suppose there is a Ferris wheel with four cabins and each cabin can contain four passengers. The wheel rotates counter-clockwise, and for each rotation, it costs run amount of money. We have an array cust that contains n items where each item i signifies the number of people waiting to get into the Ferris wheel before the i-th rotation. To board the wheel, each customer pays an amount board for one anti-clockwise rotation. The people waiting in line should not wait if there is any vacant seat available in any cabin. ...

Read More

Program to find minimum deletions to make string balanced in Python

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

Suppose we have a string s with only two characters 's' and 't'. We can delete any number of characters of s to make the string balanced. We can say, s is balanced when there is no pair of indices (i, j) such that i < j and s[i] = 't' and s[j]= 's'. We have to find the minimum number of deletions needed to make s balanced. So, if the input is like s = "sststtst", then the output will be 2 because we can either remove the characters at indices 2 and 6 ("sststtst" to "sssttt"), or ...

Read More

Program to find maximum profit by selling diminishing-valued colored balls in Python

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

Suppose we have an array called inventory, where inventory[i] represents the number of balls of the ith color we have initially. We also have a value called orders, which represents the total number of balls that the customer wants. We can sell the balls in any order, and customers want balls of any color. The values of the balls are special in nature − each colored ball's value equals the current number of balls of that color in our inventory. So if we currently have 6 blue balls, the customer pays 6 for the first blue ball. After selling ...

Read More

Program to find out the farthest building a parkour artist can reach in Python

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

A parkour artist wants to traverse buildings of different heights using a limited number of bricks and ladders. Bricks are used for small height differences (each brick covers 1 unit), while ladders can cover any height difference. We need to find the farthest building the artist can reach. Problem Understanding Given an array of building heights, the artist starts at building 0 and moves sequentially. When moving to a taller building, they must use bricks or ladders. The goal is to reach the farthest possible building using the available resources optimally. Example Walkthrough For heights = ...

Read More

Program to check subarrays can be rearranged from arithmetic sequence or not in Python

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

Given a sequence of numbers and range queries, we need to check if subarrays can be rearranged to form arithmetic sequences. An arithmetic sequence has a constant difference between consecutive elements. A sequence is arithmetic if it has at least two elements and the difference between consecutive elements remains the same. Examples include [2, 4, 6, 8, 10], [5, 5, 5, 5], and [4, -2, -8, -14]. Problem Example For nums = [6, 8, 7, 11, 5, 9], l = [0, 0, 2], r = [2, 3, 5] ? Query [0, 2]: subarray [6, 8, ...

Read More

Program to split two strings to make palindrome using Python

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

Given two strings a and b of equal length, we need to find if we can split both strings at the same index to create a palindrome by combining parts from different strings. We can split string a into a_pref + a_suff and string b into b_pref + b_suff, then check if a_pref + b_suff or b_pref + a_suff forms a palindrome. Example If a = "pqrst" and b = "turqp", we can split: a → ["pq", "rst"] b → ["tu", "rqp"] Then a_pref + b_suff = "pq" + "rqp" = "pqrqp", which is ...

Read More

Program to find the most competitive subsequence in Python

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

A competitive subsequence is a subsequence where elements appear in their original order from the array. Given an array and a target size k, we need to find the most competitive subsequence of size k. A subsequence s1 is more competitive than s2 if at the first differing position, s1 has a smaller number than s2. For example, given nums = [4, 6, 3, 7] and k = 2, among all possible subsequences of size 2: [4, 6], [4, 3], [4, 7], [6, 3], [6, 7], [3, 7], the subsequence [3, 7] is most competitive. Algorithm We ...

Read More

Program to maximize the minimum force between balls in a bucket using Python

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

Suppose we are given several buckets and x number of balls. If the balls are put into the bucket, a special force acts within them and we have to find out a way to maximize the minimum force between two balls. The force between two balls in a bucket of position p and q is |p - q|. The input given to us is the array containing the bucket positions and the number of balls x. We have to find out the minimum force between them. So, if the input is like pos = [2, 4, 6, 8, 10, ...

Read More

Program to find array of doubled pairs using Python

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

Given an array of integers with even length, we need to check if it's possible to reorder the array such that nums[2*i + 1] = 2*nums[2*i] for every index 0 cnt[2 * x]: return False cnt[2 * x] -= cnt[x] return True # Test case 1 nums1 = [4, -2, 2, -4] print("Array:", nums1) print("Can form doubled pairs:", solve(nums1)) # Test case 2 nums2 = [2, ...

Read More
Showing 3411–3420 of 8,546 articles
« Prev 1 340 341 342 343 344 855 Next »
Advertisements