Python Articles

Page 655 of 855

Find the lexicographically largest palindromic Subsequence of a String in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 380 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 266 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 855 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 213 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 565 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 153 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

Find the count of sub-strings whose characters can be rearranged to form the given word in Python

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

Given a string S (all letters are lowercase), we need to find the count of all substrings of length four whose characters can be rearranged to form the word "bird". For example, if the input is "birdb", the output will be 2 because there are two 4-character substrings that contain exactly one 'b', one 'i', one 'r', and one 'd'. Approach To solve this problem, we follow these steps − Initialize a counter to 0 Iterate through all possible 4-character substrings For each substring, count occurrences of 'b', 'i', 'r', 'd' If each character appears ...

Read More

Find the cordinates of the fourth vertex of a rectangle with given 3 vertices in Python

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

A rectangle has four vertices, and if we know three of them, we can find the fourth by using the property that opposite vertices share the same coordinates. In a grid representation with asterisks ('*') marking three vertices, we need to find the missing fourth vertex coordinates. Problem Understanding Given a grid where exactly three asterisks ('*') represent three vertices of a rectangle, we need to find the coordinates of the fourth vertex. The key insight is that in a rectangle, each row and column should contain exactly two vertices, except for the missing vertex's row and column ...

Read More

Find the character in first string that is present at minimum index in second string in Python

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

Suppose we have a string str and another string patt, we need to find the character in patt that is present at the minimum index in str. If no character from patt is present in str, then return -1. For example, if str = "helloworld" and patt = "wor", the output will be 'o' because 'o' appears at index 4, which is the smallest index among all characters from patt that exist in str. Algorithm Steps To solve this problem, we follow these steps − Initialize minimum_index to a large value ...

Read More

Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python

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

We have a 2D matrix and a set of cell indices represented as (i, j) where i is the row and j is the column. For each given cell index, we need to find the sum of all matrix elements excluding the elements in the ith row and jth column. For example, if we have the matrix: 2 2 3 4 5 7 6 4 3 And cell indices = [(0, 0), (1, 1), (0, 1)], the output will be [19, 14, 20]. Algorithm To ...

Read More
Showing 6541–6550 of 8,549 articles
« Prev 1 653 654 655 656 657 855 Next »
Advertisements