Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 125 of 377

Inverse Factorial in Python

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

An inverse factorial problem asks us to find which number n produces a given factorial value. Given a number a, we need to find n such that n! = a. If no such integer exists, we return -1. For example, if a = 120, we need to find n where n! = 120. Since 5! = 5 × 4 × 3 × 2 × 1 = 120, the answer is 5. Algorithm To solve this problem, we follow these steps ? Initialize i = 0 and num = 1 Create an empty list L to ...

Read More

Find Intersecting Intervals in Python

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

When working with intervals in Python, you often need to find the intersection of multiple intervals. An interval intersection is the overlapping portion that exists within all given intervals. Each interval is represented as [start, end] where both endpoints are inclusive. For example, given intervals [[10, 110], [20, 60], [25, 75]], the intersection is [25, 60] because this range is common to all three intervals. Algorithm Overview To find the intersection of intervals, we need to ? Find the maximum start time among all intervals Find the minimum end time among all intervals The intersection ...

Read More

String Interleaving in Python

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

String interleaving combines two strings by alternating characters from each string, starting with the first string. If one string is longer, the remaining characters are appended to the end. For example, if we have s = "abcd" and t = "pqrstu", the interleaved result will be "apbqcrdstu". Algorithm Steps To solve this problem, we follow these steps − Initialize an empty result string Find the minimum length between both strings Iterate up to the minimum length, alternating characters from each string Append any remaining characters from the longer string Using a Class Method ...

Read More

In-place Move Zeros to End of List in Python

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

Moving zeros to the end of a list while maintaining the relative order of non-zero elements is a common array manipulation problem. This can be solved efficiently using a two-pointer technique that modifies the list in-place with O(1) extra space. The algorithm works by using one pointer to track the position for non-zero elements and another to iterate through the entire list. Algorithm Steps The approach follows these steps: Use a pointer k to track the position for the next non-zero element Iterate through the list and move all non-zero elements to the front Fill ...

Read More

Index into an Infinite String in Python

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

Sometimes we need to extract a substring from an infinite repetition of a given string. Python's modulo operator makes this straightforward by cycling through the original string's indices. Given a string s and two integers i and j where i < j, we need to find the substring from an infinite string formed by repeating s forever, using indices [i, j). Example Problem If s = "programmer", i = 4, and j = 8, the infinite string looks like: "programmerprogrammerprogrammer..." 0123456789012345678901234567890... We need the substring from index 4 to 7 (excluding 8), ...

Read More

Count Frequency of Highest Frequent Elements in Python

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

Finding the frequency of the most common element in a list is a common programming task. Python provides several approaches, from basic nested loops to efficient built-in methods using collections.Counter. So, if the input is like [1, 5, 8, 5, 6, 3, 2, 45, 7, 5, 8, 7, 1, 4, 6, 8, 9, 10], then the output will be 3 as the number 5 occurs three times and is the most frequent element. Using Nested Loops (Basic Approach) This approach compares each element with all other elements to count occurrences ? def count_max_frequency(nums): ...

Read More

Insert 5 to Make Number Largest in Python

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

Given a number n, we need to find the maximum number we can create by inserting the digit '5' at any position in the number. This problem involves string manipulation and finding the optimal position to maximize the resulting number. So, if the input is like n = 826, then the output will be 8526 (inserting '5' after the first digit creates the largest possible number). Algorithm Steps To solve this problem, we follow these steps ? Convert the number n to a string for easy manipulation ...

Read More

Guess Nearest Square Root in Python

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

Finding the square root of a number without using built-in functions is a classic programming problem. We need to find the largest integer r such that r * r ≤ n, effectively rounding down to the nearest integer. So, if the input is 1025, then the output will be 32 because 32² = 1024 ≤ 1025, but 33² = 1089 > 1025. Algorithm Steps We'll use binary search to efficiently find the square root ? If n ≤ 1, return n (base case) Set start = 1, end = n While start < end: ...

Read More

Group Integers in Python

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

In Python, you can group integers into equal-sized groups where all numbers in each group are identical. This involves checking if a list can be split into groups where each group has the same size (≥2) and contains identical elements. The solution uses the Greatest Common Divisor (GCD) to find if all frequencies share a common factor greater than 1. Algorithm Steps The approach follows these steps ? Count frequency of each distinct element Find GCD of all frequencies If GCD > 1, ...

Read More

Greatest common divisors in Python

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

The greatest common divisor (GCD) is the largest positive number that divides all numbers in a given list. Python's math.gcd() function makes it easy to find the GCD of multiple numbers by applying it iteratively. Problem Statement Given a list of positive numbers, find the largest positive number that divides each number in the list. For example, if the input is [14, 28, 70, 56], the output should be 14 since 14 divides all these numbers. Algorithm To solve this problem, we follow these steps ? Initialize result with the first element of ...

Read More
Showing 1241–1250 of 3,768 articles
« Prev 1 123 124 125 126 127 377 Next »
Advertisements