Python Articles

Page 120 of 855

Program to find kth missing positive number in an array in Python

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

Suppose we have an array called nums with positive sorted strictly increasing values, and also have an integer k. We have to find the kth positive integer that is missing from this array. So, if the input is like nums = [1, 2, 4, 8, 12], k = 6, then the output will be 10 because the missing numbers are [3, 5, 6, 7, 9, 10, 11], here the 6th term is 10. Approach To solve this, we will follow these steps − Convert the array to a set for O(1) ...

Read More

Program to shuffle string with given indices in Python

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

Suppose we have a string s and a list of indices ind, they are of same length. The string s will be shuffled such that the character at position i moves to indices[i] in the final string. We have to find the final string. So, if the input is like s = "ktoalak" and ind = [0, 5, 1, 6, 2, 4, 3], then the output will be "kolkata". Original String: Index: Char: 0 ...

Read More

Program to count odd numbers in an interval range using Python

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

Suppose we have two non-negative numbers left and right. We have to find the number of odd numbers between left and right (inclusive). So, if the input is like left = 3, right = 15, then the output will be 7 because there are 7 odd numbers in range, these are [3, 5, 7, 9, 11, 13, 15], there are 7 elements. Algorithm To solve this, we will follow these steps: If left is odd or right is odd, then ...

Read More

Program to find maximum how many water bottles we can drink in Python

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

Suppose there are n number of full water bottles, and we can exchange m empty water bottles for one full water bottle. Drinking a full water bottle makes it an empty bottle. We have to find the maximum number of water bottles we can drink. So, if the input is like n = 9, m = 3, then the output will be 13 because initially we have 9 bottles. After drinking all bottles, we get 9/3 = 3 full bottles from the empty ones. After drinking those 3, we have 3 empty bottles which gives us 1 more full ...

Read More

Program to reformat date in YYYY-MM-DD format using Python

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

Converting date strings from human-readable format like "23rd Jan 2021" to standardized "YYYY-MM-DD" format is a common task in data processing. Python provides several approaches to handle this conversion efficiently. Understanding the Problem We need to parse a date string where: Day includes ordinal suffix (1st, 2nd, 3rd, 4th, etc.) Month is abbreviated (Jan, Feb, Mar, etc.) Year is a four-digit number (1900-2100) The target format is ISO 8601 standard: "YYYY-MM-DD". Method 1: Using String Manipulation This approach splits the date string and manually converts each component ? def solve(date): ...

Read More

Program to check we can make arithmetic progression from sequence in Python

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

Suppose we have a list of numbers. We need to check whether these numbers can form an Arithmetic Progression (AP) when arranged in proper order. In an AP series, the common difference between any two consecutive elements is the same. So, if the input is like nums = [9, 1, 17, 5, 13], then the output will be True because if we sort them, it will be [1, 5, 9, 13, 17] and here common difference is 4 for each pair of elements. Algorithm To solve this, we will follow these steps: ...

Read More

Program to find average salary excluding the minimum and maximum salary in Python

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

Suppose we have an array with distinct elements called salary where salary[i] is the salary of ith employee. We have to find the average salary of employees excluding the minimum and maximum salary. So, if the input is like salary = [8000, 6000, 2000, 8500, 2500, 4000], then the output will be 5125.0, as the minimum and maximum salary values are 2000 and 8500. Excluding them, the remaining salary values are [8000, 6000, 2500, 4000], so the average is (8000 + 6000 + 2500 + 4000)/4 = 5125. Algorithm To solve this problem, we will follow these ...

Read More

Program to perform XOR operation in an array using Python

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

Suppose we have an integer n and another integer start. We need to create an array called nums where nums[i] = start + 2*i (where i starts from 0) and n is the size of nums. Then find the bitwise XOR of all elements of nums. So, if the input is like n = 6, start = 2, then the output will be 14 because the array will be like [2+2*0, 2+2*1, ... 2+2*5] = [2, 4, 6, 8, 10, 12], then XOR of each element present in the array is 14. Understanding the Problem Let's first ...

Read More

Program to find running sum of 1d array in Python

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

A running sum array calculates the cumulative sum at each position. For a given array, the running sum at index i equals the sum of all elements from index 0 to i. For example, if nums = [8, 3, 6, 2, 1, 4, 5], then the running sum will be [8, 11, 17, 19, 20, 24, 29] because: rs[0] = nums[0] = 8 rs[1] = sum of nums[0..1] = 8 + 3 = 11 rs[2] = sum of nums[0..2] = 8 + 3 + 6 = 17 and so on Method 1: Using ...

Read More

Program to find Final Prices With a Special Discount in a Shop in Python

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

Given an array of prices, we need to find the final prices after applying a special discount. For each item at index i, we get a discount equal to the price of the first item at index j where j > i and prices[j] ≤ prices[i]. Problem Understanding The discount rule works as follows ? For item at index i, look for the first item at index j where j > i If prices[j] ≤ prices[i], then discount = prices[j] Final price = prices[i] − prices[j] If no such j exists, no discount is applied ...

Read More
Showing 1191–1200 of 8,549 articles
« Prev 1 118 119 120 121 122 855 Next »
Advertisements