Articles on Trending Technologies

Technical articles with clear explanations and examples

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 722 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 573 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 506 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

Find the lexicographically largest palindromic Subsequence of a String in Python

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

Sometimes we need to find the lexicographically largest palindromic subsequence of a string. A palindromic subsequence reads the same forwards and backwards, and the lexicographically largest one uses the highest character in the string. The key insight is that the lexicographically largest palindromic subsequence consists of all occurrences of the maximum character in the string, since identical characters always form a palindrome. So, if the input is like "tutorialspointtutorial", then the output will be "uu". Algorithm Steps To solve this, we will follow these steps − Initialize ans as empty ...

Read More

Find the largest rectangle of 1's with swapping of columns allowed in Python

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

Given a binary matrix, we need to find the largest rectangle of all 1's that can be formed by swapping any columns. This problem combines the classic "largest rectangle in histogram" algorithm with column optimization. Problem Understanding Consider this matrix ? 1 0 0 1 0 1 0 0 1 1 1 1 0 1 0 By swapping columns 1 and 3, we get ? 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 ...

Read More

Find the largest Perfect Subtree in a given Binary Tree in Python

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

A Perfect Binary Tree is a binary tree where all internal nodes have exactly two children and all leaves are at the same level. This article explains how to find the size of the largest perfect subtree within a given binary tree using Python. 2 3 4 5 6 7 ...

Read More

Find the index which is the last to be reduced to zero after performing a given operation in Python

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

Given an array and a value K, we need to find which index will be the last element to reach zero after repeatedly performing a subtraction operation. In each operation, we subtract K from each non-zero element, and if any element becomes less than K, it's set to zero. Problem Understanding The operation works as follows ? Start from A[0] to A[N − 1], update each element as A[i] = A[i] − K If A[i] < K, then set A[i] = 0 Once an element becomes 0, no further operations are performed on it Repeat until ...

Read More

Find the element before which all the elements are smaller than it, and after which all are greater in Python

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

In array processing, we sometimes need to find a special element that acts as a pivot point — an element where all elements to its left are smaller, and all elements to its right are greater. This problem requires finding such an element and returning its index, or -1 if no such element exists. Problem Understanding Given an array, we need to find an element where: All elements before it are smaller than the element All elements after it are greater than the element For the array [6, 2, 5, 4, 7, 9, 11, 8, ...

Read More

Find the distance covered to collect items at equal distances in Python

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

In this problem, we need to calculate the total distance covered by a participant who collects stones placed at equal intervals and returns each stone to a starting bucket. The setup is as follows: A bucket is placed at the starting point The first stone is 6 units away from the bucket Each subsequent stone is 4 units apart from the previous one The participant must collect stones one by one, returning to the bucket after each collection Bucket ...

Read More
Showing 1–10 of 61,304 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements