Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 124 of 855
Program to find minimum deletions to make string balanced in Python
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 MoreProgram to find maximum profit by selling diminishing-valued colored balls in Python
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 MoreProgram to find out the farthest building a parkour artist can reach in Python
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 MoreProgram to check subarrays can be rearranged from arithmetic sequence or not in Python
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 MoreProgram to split two strings to make palindrome using Python
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 MoreProgram to find the most competitive subsequence in Python
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 MoreProgram to maximize the minimum force between balls in a bucket using Python
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 MoreProgram to find array of doubled pairs using Python
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 MoreProgram to merge in between linked lists in Python
Suppose we have two linked lists L1 and L2 of length m and n respectively, we also have two positions a and b. We have to remove nodes from L1 from a-th node to b-th node and merge L2 in between. So, if the input is like L1 = [1, 5, 6, 7, 1, 6, 3, 9, 12] L2 = [5, 7, 1, 6] a = 3 b = 6, then the output will be [1, 5, 6, 5, 7, 1, 6, 9, 12] Algorithm To solve this, we will follow these steps − ...
Read MoreProgram to find out the number of people who get a food packet using Python
Suppose in a conference, there are two types of people. The first type prefers vegetarian lunch (denoted by 0), and the other type prefers non-vegetarian lunch (denoted by 1). There are limited food packets, and if vegetarians receive a non-vegetarian packet or vice versa, they will not take that packet and wait until they get their preferred one. We are given two arrays: one containing food packets and another containing the queue of people with their preferences. If a person does not receive their preferred packet, they re-enter the queue at the end. We need to find the number ...
Read More