Programming Articles

Page 533 of 2547

Knights' Attack in Python

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

The knight attack problem checks if any two knights on a chessboard can attack each other. A knight moves in an L-shape: two squares in one direction and one square perpendicular, or vice versa. Problem Understanding Given a binary matrix where 0 represents an empty cell and 1 represents a knight, we need to determine if any two knights are attacking each other. For the input matrix: 00000 01000 00010 The output is True because the knight at position (1, 1) can attack the knight at position (2, 3). Knight ...

Read More

K and -K in Python

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

In Python, finding the largest number k where both k and -k exist in a list is a common programming problem. This means we need to find pairs of numbers that are additive inverses of each other. So, if the input is like [-5, 2, 9, -6, 5, -9], then the output will be 9 because both 9 and -9 exist in the list. Algorithm Approach To solve this problem, we follow these steps: Create L1: a list of non-negative elements (≥ 0) from nums Create L2: a list of non-positive elements (≤ 0) from ...

Read More

Remove One to Make Average k in Python

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

Suppose we have a list of numbers called nums and an integer k, we have to check whether we can remove exactly one element from the list to make the average equal to exactly k. Now we have to keep in mind that, there are some constraints − 2 ≤ n ≤ 1, 000 where n is number of elements of nums list nums[i], k ≤ 1, 000, 000 So, if the input is like [5, 3, 2, 4, 6, 10], k = 4, then the output will ...

Read More

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 535 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 591 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
Showing 5321–5330 of 25,466 articles
« Prev 1 531 532 533 534 535 2547 Next »
Advertisements