Found 10476 Articles for Python

Program to find minimum length of lossy Run-Length Encoding in Python

Arnab Chakraborty
Updated on 10-Oct-2020 15:03:03

290 Views

Suppose we have a lowercase string s and another value k. Now consider an operation where we perform a run-length encoding on a string by putting repeated successive characters as a count and character. So if the string is like "aaabbc" would be encoded as "3a2bc". Here we do not put "1c" for "c" since it only appears once successively. So we can first remove any k consecutive characters in s, then find the minimum length possible of the resulting run-length encoding.So, if the input is like s = "xxxxxyyxxxxxzzxxx", k = 2, then the output will be 6, as ... Read More

Program to find sum of longest sum path from root to leaf of a binary tree in Python

Arnab Chakraborty
Updated on 10-Oct-2020 14:19:52

183 Views

Suppose we have a binary tree, we have to find the sum of the longest path from the root to a leaf node. If there are two same long paths, return the path with larger sum.So, if the input is likethen the output will be 20.To solve this, we will follow these steps −Define a function rec() . This will take currif curr is null, thenreturn(0, 0)bigger := maximum of rec(left of curr) , rec(right of curr)return a pair (bigger[0] + 1, bigger[1] + value of curr)From the main method do the following −ret := rec(root)return the 1th index of ... Read More

Program to find the length of longest substring which has two distinct elements in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:57:29

560 Views

Suppose we have a string s, we have to find the length of the longest substring that contains at most 2 distinct characters.So, if the input is like s = "xyzzy", then the output will be 4, as "yzzy" is the longest substring with at most 2 unique characters.To solve this, we will follow these steps−start := 0c := a mapans := 0for end in range 0 to size of s, doc[s[end]] := c[s[end]] + 1while size of c > 2, doc[s[start]] := c[s[start]] - 1if c[s[start]] is 0, thendelete c[s[start]]start := start + 1ans := maximum of ans and ... Read More

Program to find length of longest sublist with given condition in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:51:11

310 Views

Suppose we have a list of numbers called nums, we have to find the length of the longest sublist where 2 * minimum of sublist > maximum of sublist.So, if the input is like nums = [10, 2, 6, 6, 4, 4], then the output will be 4, as the sublist [6, 6, 4, 4] is the longest sublist that holds the criteria as 2 * 4 > 6.To solve this, we will follow these steps−ret := 0define two double ended queues minq and maxql := 0, r := 0while r < size of nums, don := nums[r]while minq and ... Read More

Program to find length of longest palindromic substring in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:41:59

640 Views

Suppose we have a string S. We have to find the length of longest palindromic substring in S. We are assuming that the length of the string S is 1000. So if the string is “BABAC”, then the longest palindromic substring is “BAB” and length is 3.To solve this, we will follow these steps −Define one square matrix of order same as the length of string, and fill it with FalseSet the major diagonal elements as true, so DP[i, i] = True for all i from 0 to order – 1start := 0for l in range 2 to length of ... Read More

Program to find length of longest alternating inequality elements sublist in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:36:00

226 Views

Suppose we have a list of mumbers called nums, and find the length of the longest sublist in nums such that the equality relation between every consecutive numbers changes alternatively between less-than and greater-than operation. The first two numbers' inequality may be either less-than or greater-than.So, if the input is like nums = [1, 2, 6, 4, 5], then the output will be 4, as the longest inequality alternating sublist is [2, 6, 4, 5] as 2 < 6 > 4 < 5.To solve this, we will follow these steps −Define a function get_direction(). This will take a, breturn 0 ... Read More

Program to find length of longest increasing subsequence in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:10:29

578 Views

Suppose we have a list of numbers. We have to find the length of longest increasing subsequence. So if the input is like [6, 1, 7, 2, 8, 3, 4, 5], then the output will be 5, as the longest increasing subsequence is [2, 3, 4, 5, 6].To solve this, we will follow these steps −Make an array called tails whose size is same as nums, and fill this with 0.size := 0for each element x in nums array −i := 0, j := sizewhile i is not same as j, thenmid := i + (j – i)/2if tails[mid] < ... Read More

Program to find longest even value path of a binary tree in Python

Arnab Chakraborty
Updated on 30-Jun-2025 15:49:58

242 Views

A Binary tree is one of the data structures in Python in which each element is known as a Node. Each node should have a minimum of two child nodes. These children nodes are called as left child and the right child. The top node of the binary tree is known as the Root Node, and the nodes with no children are called Leaf Nodes. Following is the representation of the Binary Tree in Python - 10 / \ 5 ... Read More

Program to find longest equivalent sublist after K increments in Python

Niharikaa Aitam
Updated on 30-Jun-2025 16:52:55

215 Views

The longest equivalent sublist after K increments is known as the longest contiguous portion of a list where we can make all the elements equal by performing at most K increment operations. Suppose we have a list of numbers called nums and k. Now, consider an operation where we can increment any one element once. If we can perform operations at most k times, we have to find the longest sub-list containing equal elements. Input Output Scenario Let's consider the following scenario, as we can increment 9 once and 6 four times to get the sublist [10, 10, 10], ... Read More

Program to find length of longest distinct sublist in Python

Niharikaa Aitam
Updated on 01-Sep-2025 12:00:56

416 Views

A distinct sublist is a continuous portion of a list in which all the elements are different from each other. The goal is to find the maximum length of such a sublist within a given list of elements. In Python, a list is an ordered and mutable data structure in which sequence of elements are enclosed in square brackets []. The elements in a list can be of any datatype such as integer, float, string etc., and each element in a list has a unique index position which starts from 0. Using set() function The set() is a built-in function ... Read More

Advertisements