Python Articles

Page 654 of 855

Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted in Python

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

When working with unsorted arrays, we often need to find the minimum length subarray that, when sorted, makes the entire array sorted. For example, in the array [2, 6, 4, 8, 10, 9, 15], sorting the subarray [6, 4, 8, 10, 9] at positions 1-5 would make the entire array sorted. Algorithm Approach The solution compares the original array with its sorted version to identify mismatched positions ? Create a sorted copy of the input array Compare each element with its sorted counterpart Track all positions where elements differ Return the distance between first and last ...

Read More

Find the minimum difference between Shifted tables of two numbers in Python

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

When we have two numbers p and q, we can create their multiplication tables and then shift them by values r and s respectively. The problem asks us to find the minimum difference between any terms in these shifted tables. A shifted table means we add the shift value to each element in the multiplication table. For example, if we have the table of 7: [7, 14, 21, 28, ...] and shift it by 6, we get [13, 20, 27, 34, ...]. Understanding the Problem Let's understand with an example where p = 7, q = 17, ...

Read More

Find the minimum and maximum amount to buy all N candies in Python

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

Suppose there is a candy store where N different types of candies are available and the prices of all N different types of candies are given. The store also provides an attractive offer: when you buy a single candy, you can get a maximum of K different types of other candies for free. We need to find both the minimum and maximum amount of money required to buy all N different types of candies while utilizing the offer optimally. The key insight is: For minimum cost: Buy the cheapest candies and get expensive ones for free For ...

Read More

Find the maximum repeating number in O(n) time and O(1) extra space in Python

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

Finding the maximum repeating number in an array with constraints of O(n) time and O(1) extra space requires a clever approach. Given an array of size n with elements in range 0 to k-1 (where k ≤ n), we can modify the original array to store frequency information. The key insight is to use the array indices as counters by adding k to the element at index A[i] % k for each element A[i]. Algorithm Steps For each element in the array, increment the value at index A[i] % k by k After processing, the element ...

Read More

Find the maximum number of composite summands of a number in Python

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

Given a number N (where 1 ≤ N ≤ 10^9), we need to represent N as a sum of the maximum possible number of composite numbers. A composite number is a positive integer greater than 1 that has at least one positive divisor other than 1 and itself (like 4, 6, 8, 9, 10, etc.). For example, if N = 16, we can write it as 4 + 4 + 4 + 4 (4 summands) or 8 + 8 (2 summands). The maximum number of summands is 4. Approach We use dynamic programming with the smallest composite ...

Read More

Find the maximum distance covered using n bikes in Python

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

Suppose there are n bikes and each can cover 100 km when they are fully fueled. We have to find the maximum amount of distance we can go using these n bikes. Here we can assume that all bikes are similar and a bike consumes 1 litre of fuel to cover 1 km distance. The key insight is that instead of running all bikes in parallel (which only covers 100 km), we can transfer fuel strategically. When bikes run serially and transfer fuel at optimal points, we can cover much more distance without overflowing fuel tanks. Problem Analysis ...

Read More

Find the longest substring with k unique characters in a given string in Python

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

Finding the longest substring with exactly k unique characters is a classic sliding window problem. We use two pointers to maintain a window and expand or shrink it based on the number of unique characters. Problem Understanding Given a string and a number k, we need to find the longest substring that contains exactly k unique characters. For example, in "ppqprqtqtqt" with k=3, the answer is "rqtqtqt" which has length 7 and contains exactly 3 unique characters: 'r', 'q', and 't'. Algorithm Steps The solution uses a sliding window approach ? First, check if ...

Read More

Find the longest subsequence of an array having LCM at most K in Python

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

Given an array of numbers and a positive integer K, we need to find the longest subsequence where the Least Common Multiple (LCM) is at most K. The function returns the LCM value, length of the subsequence, and indices of elements in the optimal subsequence. Problem Understanding For example, if we have array A = [3, 4, 5, 6] and K = 20, we need to find which combination of elements gives us the longest subsequence with LCM ≤ 20. The LCM of [3, 4, 6] is 12, which is ≤ 20, and this gives us the longest ...

Read More

Find the longest sub-string which is prefix, suffix and also present inside the string in Python

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

Given a string, we need to find the longest substring that appears as a prefix, suffix, and also somewhere inside the string. This problem uses the concept of the Longest Prefix Suffix (LPS) array from the KMP algorithm. For example, in the string "languagepythonlanguageinterestinglanguage", the substring "language" appears at the beginning, end, and middle of the string. Algorithm Overview We use a two-step approach: Build the LPS (Longest Prefix Suffix) array using the KMP preprocessing algorithm Find the longest substring that satisfies our conditions using the LPS array Building the LPS Array ...

Read More

Find the lexicographically smallest string which satisfies the given condition in Python

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

Given an array A of n numbers where A[i] indicates the number of distinct characters in the prefix of length (i + 1) of a string s, we need to find the lexicographically smallest string that satisfies the given prefix array. All characters will be lowercase English alphabets [a-z]. If no such string exists, return -1. For example, if A = [1, 1, 2, 3, 4], the output will be "aabcd" because prefix[0] has 1 distinct character, prefix[1] has 1 distinct character, prefix[2] has 2 distinct characters, prefix[3] has 3 distinct characters, and prefix[4] has 4 distinct characters, and ...

Read More
Showing 6531–6540 of 8,549 articles
« Prev 1 652 653 654 655 656 855 Next »
Advertisements