Found 10784 Articles for Python

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

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

147 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

477 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 10-Oct-2020 12:42:34

136 Views

Suppose we have a binary tree, we have to find the longest path consisting of even values between any two nodes in the tree.So, if the input is likethen the output will be 5 as the longest path is [10, 2, 4, 8, 6].To solve this, we will follow these steps −ans := 0Define a function find() . This will take nodeif node is null, thenreturn (-1, -1)leftCnt := maximum of returned value of find(left of node) + 1rightCnt := maximum of returned value of find(right of node) + 1if value of node is even, thenans := maximum of ans ... Read More

Program to find longest equivalent sublist after K increments in Python

Arnab Chakraborty
Updated on 10-Oct-2020 12:33:03

105 Views

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 sublist containing equal elements.So, if the input is like nums = [3, 5, 9, 6, 10, 7] k = 6, then the output will be 3, as we can increment 9 once and 6 four times to get the sublist [10, 10, 10].To solve this, we will follow these steps −if nums is empty, thenreturn 0wMax := a double ended queue of ... Read More

Program to find length of longest distinct sublist in Python

Arnab Chakraborty
Updated on 10-Oct-2020 12:25:47

303 Views

Suppose we have a list of numbers called nums, and we have to find the length of the longest contiguous sublist where all its elements are unique.So, if the input is like nums = [6, 2, 4, 6, 3, 4, 5, 2], then the output will be 5, as the longest list of unique elements is [6, 3, 4, 5, 2].To solve this, we will follow these steps −head := 0, dct := a new mapmax_dist := 0for each index i and element num in nums, doif num is in dct and dct[num] >= head, thenhead := dct[num] + 1dct[num] ... Read More

Program to find length of longest consecutive sequence in Python

Arnab Chakraborty
Updated on 10-Oct-2020 12:20:22

653 Views

Suppose we have an unsorted array of numbers, we have to find the length of the longest sequence of consecutive elements.So, if the input is like nums = [70, 7, 50, 4, 6, 5], then the output will be 4, as the longest sequence of consecutive elements is [4, 5, 6, 7]. so we return its length: 4.To solve this, we will follow these steps −nums := all unique elements of numsmax_cnt := 0for each num in nums, doif num - 1 not in nums, thencnt := 0while num is present in nums, donum := num + 1cnt := cnt ... Read More

Program to find length of longest anagram subsequence in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:50:40

249 Views

Suppose we have two lowercase strings S and T, we have to find the length of the longest anagram subsequence.So, if the input is like S = "helloworld", T = "hellorld", then the output will be 8To solve this, we will follow these steps −c := a new map, d := a new mapfor i in range 0 to size of a, doif a[i] in c, thenc[a[i]] := c[a[i]] + 1otherwise, c[a[i]] := 1for i in range 0 to size of b, doif b[i] in d, thend[b[i]] := d[b[i]] + 1otherwise, d[b[i]] := 1res := 0for each ch in c, ... Read More

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

Arnab Chakraborty
Updated on 10-Oct-2020 11:44:43

292 Views

Suppose we have a list of numbers called nums, we will create a new list where each element in the new list is the number of smaller elements to the right hand side of that element in the original input list.So, if the input is like nums = [4, 5, 9, 7, 2], then the output will be [1, 1, 2, 1, 0], as there is 1 smaller element to the right of 4, there is 1 smaller element to the right of 5, there are 2 smaller elements to the right of 9, there is 1 smaller element to ... Read More

Program to find which element occurs exactly once in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:41:12

342 Views

Suppose we have a list of numbers called nums where each value occurs exactly three times except one value that occurs once. We have to find the unique value. We have to solve it in constant space.So, if the input is like nums = [3, 3, 3, 8, 4, 4, 4], then the output will be 8To solve this, we will follow these steps −m := a map with different values and their frequenciesreturn the value with minimum frequencyLet us see the following implementation to get better understanding −Example Live Demofrom collections import Counter class Solution:    def solve(self, nums):   ... Read More

Program to partition color list in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:37:39

520 Views

Suppose we have a a list of color strings, these may contain "red", "green" and "blue", we have to partition the list so that the red come before green, and green come before blue.So, if the input is like colors = ["blue", "green", "blue", "red", "red"], then the output will be ['red', 'red', 'green', 'blue', 'blue']To solve this, we will follow these steps −green := 0, blue := 0, red := 0for each string in strs, doif string is same as "red", thenstrs[blue] := "blue"blue := blue + 1strs[green] := "green"green := green + 1strs[red] := "red"red := red + ... Read More

Advertisements