Python Articles

Page 625 of 855

Program to find length of longest anagram subsequence in Python

Niharikaa Aitam
Niharikaa Aitam
Updated on 25-Mar-2026 430 Views

In Python, a string is an immutable data structure in which sequence of characters are enclosed in double("") or single quotes(''). In some cases, we need to find the length of the longest subsequence of characters that can be rearranged to form a palindrome. This type of subsequence is referred to as a palindromic anagram subsequence. An anagram is a word or phrase formed by rearranging the letters of another word or phrase by using all the original letters exactly once. In other words, two strings are said to be anagrams of each other if they contain the same ...

Read More

Program to return number of smaller elements at right of the given list in Python

Niharikaa Aitam
Niharikaa Aitam
Updated on 25-Mar-2026 488 Views

In Python, a list is an ordered and mutable data structure where elements are enclosed in square brackets []. In some scenarios, we may need to count how many elements to the right of each element in the list are smaller than it. For example, given the list [5, 2, 6, 1], for element 5 at index 0, there are 2 smaller elements (2 and 1) to its right. For element 2 at index 1, there is 1 smaller element (1) to its right. Using Binary Search with bisect_left() Function The bisect_left() function from the bisect module ...

Read More

Program to find which element occurs exactly once in Python

Niharikaa Aitam
Niharikaa Aitam
Updated on 25-Mar-2026 607 Views

In Python, data structures such as strings, lists, etc., may contain duplicate values. In some scenarios, we may need to find which element appears only once while all other elements appear multiple times. Let's explore different methods to find which element occurs exactly once in Python. Using Bitwise XOR The XOR (Exclusive OR) is a bitwise operator that returns 1 if the corresponding bits of two operands are different, otherwise it returns 0 if they are the same. In Python, we use the ^ operator to perform XOR operations. The XOR approach works because of these properties: ...

Read More

Program to partition color list in Python

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

Suppose we have a list of color strings containing "red", "green" and "blue". We need to partition the list so that red comes before green, and green comes before blue. So, if the input is like colors = ["blue", "green", "blue", "red", "red"], then the output will be ['red', 'red', 'green', 'blue', 'blue']. Algorithm To solve this, we will follow these steps − Initialize three pointers: red = 0, green = 0, blue = 0 For each string in the list: ...

Read More

Program to create linked list to binary search tree in Python

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

A Binary Search Tree (BST) is a tree data structure where each node has at most two children, and for every node, the left subtree contains values smaller than the node, and the right subtree contains values greater than the node. Converting a sorted linked list to a BST involves finding the middle element as the root and recursively building left and right subtrees. Algorithm The key insight is to use the two-pointer technique to find the middle node of the linked list ? Find the middle node using slow and fast pointers Make the middle ...

Read More

Program to convert level order binary tree traversal to linked list in Python

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

Converting a binary tree to a singly linked list using level-order traversal means visiting nodes level by level from left to right. We use a queue to perform breadth-first traversal and create linked list nodes in the same order. Problem Understanding Given a binary tree like this: 5 4 10 2 7 ...

Read More

Program to traverse binary tree level wise in alternating way in Python

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

Suppose we have a binary tree, we need to traverse it level by level in an alternating manner − first level left−to−right, second level right−to−left, third level left−to−right, and so on. So, if the input is like: 5 4 -10 ...

Read More

Program to find length of longest balanced subsequence in Python

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

When working with bracket sequences, finding the longest balanced subsequence is a common problem. A balanced bracket subsequence contains equal numbers of opening "(" and closing ")" brackets, where each closing bracket has a matching opening bracket before it. So, if the input is like s = "())(()(" , then the output will be 4, as we can take the subsequence like "()()". Algorithm Approach The key insight is to traverse the string from right to left and use a greedy approach ? Initialize result counter and closing bracket counter Traverse string from right to ...

Read More

Program to find leftmost deepest node of a tree in Python

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

In a binary tree, we often need to find the leftmost deepest node. This means finding the node at the maximum depth, and if multiple nodes exist at that depth, we return the leftmost one. So, if the input is like 13 12 14 16 22 ...

Read More

Program to check whether we can fill square where each row and column will hold distinct elements in Python

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

Suppose we have one n × n matrix containing values from 0 to n. Here 0 represents an unfilled square, we have to check whether we can fill empty squares such that in each row and each column every number from 1 to n appears exactly once. This is essentially a Latin square completion problem where we need to verify if a partially filled grid can be completed following sudoku-like rules. Example Input and Output So, if the input is like ? 002 201 123 Then the output will be True, as we ...

Read More
Showing 6241–6250 of 8,546 articles
« Prev 1 623 624 625 626 627 855 Next »
Advertisements