Python Articles

Page 628 of 855

Program to find lowest possible integer that is missing in the array in Python

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

Finding the first missing positive integer is a common programming problem. Given an array that may contain duplicates and negative numbers, we need to find the lowest positive number that is not present in the array. So, if the input is like nums = [0, 3, 1], then the output will be 2 because 1 and 3 are present, but 2 is missing. Algorithm Approach To solve this problem, we follow these steps: Create a set containing only positive numbers from the input array If the set is empty (no positive numbers), return 1 Iterate ...

Read More

Program to find minimum amount needed to be paid all good performers in Python

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

Suppose we have a list of performance ratings for coders. The manager wants to pay each coder at least Rs 1000, but if two coders are adjacent, the better performing coder must receive at least Rs 1000 more than the worse performing one. We need to find the minimum total amount required following these constraints. So, if the input is like ratings = [1, 2, 5, 1], then the output will be 7000, as the minimum payment for each coder is [1000, 2000, 3000, 1000]. Algorithm To solve this problem, we use a two-pass approach ? ...

Read More

Program to check a number can be written as a sum of distinct factorial numbers or not in Python

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

Suppose we have a positive number n, we have to check whether n can be written as the sum of unique positive factorial numbers or not. So, if the input is like n = 144, then the output will be True, as 4! + 5! = 24 + 120 = 144 Algorithm To solve this, we will follow these steps − Generate all factorial numbers up to n Use a greedy approach: start from the largest factorial and subtract it if possible Continue until we either reach 0 (success) or can't subtract any more factorials ...

Read More

Program to check we can reach leftmost or rightmost position or not in Python

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

Suppose we have a string containing letters of three types: R, B, and dot(.). Here R stands for our current position, B stands for a blocked position, and dot(.) stands for an empty position. In one step, we can move to any adjacent position to our current position, as long as it is valid (empty). We have to check whether we can reach either the leftmost position or the rightmost position. So, if the input is like s = "...........R.....BBBB.....", then the output will be True, as R can reach the leftmost position because there is no block between ...

Read More

Program to find how many years it will take to reach t amount in Python

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

Suppose we have some parameters P, O, E, T. If we have P dollars in principal that we want to invest in the stock market. The stock market alternates between first returning E and then O percent interest per year, we have to check how many years it would take to reach at least T dollars. Problem Understanding The investment follows an alternating pattern: Year 1, 3, 5... − Apply E percent interest Year 2, 4, 6... − Apply O percent interest We need to calculate how many years it takes for the principal ...

Read More

Program to count k length substring that occurs more than once in the given string in Python

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

Sometimes we need to count how many k-length substrings appear more than once in a given string. This is useful for pattern analysis and string processing tasks. So, if the input is like s = "xxxyyy", k = 2, then the output will be 2 because substrings "xx" and "yy" each occur more than once. Algorithm Steps To solve this, we will follow these steps − Create a list to store all k-length substrings For i in range 0 to size of s - k, do Extract substring of s [from index i to ...

Read More

Program to find sum of digits until it is one digit number in Python

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

Sometimes we need to keep adding the digits of a number repeatedly until we get a single digit. This process is called finding the digital root of a number. So, if the input is like 9625, then the output will be 4 because: 9 + 6 + 2 + 5 = 22 2 + 2 = 4 Using Recursive Approach We can solve this by recursively summing the digits until we get a single digit ? import math class Solution: def solve(self, n): ...

Read More

Program to find duplicate elements and delete last occurrence of them in Python

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

When working with lists in Python, sometimes we need to find duplicate elements and remove their last occurrences. This problem requires tracking the frequency of each element and identifying when we encounter the final duplicate. So, if the input is like [10, 30, 40, 10, 30, 50], then the output will be [10, 30, 40, 50]. Algorithm Steps To solve this problem, we follow these steps: Count the frequency of each element in the list Track how many times we've seen each element while iterating When the seen count equals the total frequency and it's ...

Read More

Program to count number of elements present in a set of elements with recursive indexing in Python

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

Recursive indexing involves creating a chain of elements by repeatedly using values as indices. Given a list A and starting index k, we build a set {A[k], A[A[k]], A[A[A[k]]], ...} until we reach an out-of-bounds index or detect a cycle. If there's a cycle (we encounter the same value twice), we return -1. Otherwise, we return the size of the unique elements collected. Example Walkthrough For A = [1, 2, 3, 4, 5, 6, 7] and k = 1: A[1] = 2, add 2 to set A[2] = 3, add 3 to set A[3] = ...

Read More

Program to find the index of first Recurring Character in the given string in Python

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

In string processing, we often need to find the first recurring character — the first character that appears again in the string. This problem requires us to track which characters we've seen and return the index where we encounter a character for the second time. So, if the input is like "abcade", then the output will be 3, as 'a' appears first at index 0 and recurs at index 3. Approach To solve this, we will follow these steps ? Create a set to store characters we've already seen ...

Read More
Showing 6271–6280 of 8,546 articles
« Prev 1 626 627 628 629 630 855 Next »
Advertisements