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
Articles by Arnab Chakraborty
Page 125 of 377
Inverse Factorial in Python
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 MoreFind Intersecting Intervals in Python
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 MoreString Interleaving in Python
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 MoreIn-place Move Zeros to End of List in Python
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 MoreIndex into an Infinite String in Python
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 MoreCount Frequency of Highest Frequent Elements in Python
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 MoreInsert 5 to Make Number Largest in Python
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 MoreGuess Nearest Square Root in Python
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 MoreGroup Integers in Python
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 MoreGreatest common divisors in Python
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