Found 10476 Articles for Python

Program to find length of longest set of 1s by flipping k bits in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:22:23

226 Views

Suppose we have a binary list, so here only 1s and 0s are available and we also have another number k. We can set at most k 0s to 1s, we have to find the length of the longest sublist containing all 1s.So, if the input is like nums = [0, 1, 1, 0, 0, 1, 1] k = 2, then the output will be 6, as we can set the two middle 0s to 1s and then the list becomes [0, 1, 1, 1, 1, 1, 1].To solve this, we will follow these steps −zeros := 0, ans := ... Read More

Program to find length of longest strictly increasing then decreasing sublist in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:20:36

467 Views

Suppose we have a list of numbers called nums. We have to find the length of the longest sublist such that (minimum length 3) its values are strictly increasing and then decreasing.So, if the input is like nums = [7, 1, 3, 5, 2, 0], then the output will be 5, as the sublist is [2, 4, 6, 3, 1] is strictly increasing then decreasing.To solve this, we will follow these steps −i := 0, n := size of a, res := -infinitywhile i < n - 2, dost := ilinc := 0, ldec := 0while i < n - ... Read More

Program to find length of longest sign alternating subsequence from a list of numbers in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:18:32

209 Views

Suppose we have a list of numbers called nums, we have to find the length of longest subsequence that flips sign on each consecutive number.So, if the input is like nums = [1, 3, -6, 4, -3], then the output will be 4, as we can pick [1, -6, 4, -3].To solve this, we will follow these steps −pos := 0, neg := 0for each n in nums, doif n < 0, thenneg := pos + 1otherwise, pos := neg + 1return maximum of pos and negLet us see the following implementation to get better understanding −Example Live Democlass Solution:   ... Read More

Program to find length of longest interval from a list of intervals in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:16:22

513 Views

Suppose we have a list of intervals where each interval is in form [start, end]. We have to find the longest interval that we can make by merging any number of overlapping intervals.So, if the input is like [[1, 6],[4, 9],[5, 6],[11, 14],[16, 20]], then the output will be 9, as after merging, we have the interval [1, 9] of a length 9.To solve this, we will follow these steps −sort the list intervalsunion := first interval from the intervals listbest := union[end] - union[start] + 1for each start time s and end time e in intervals except the first one, doif s

Program to find length of longest Fibonacci subsequence from a given list in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:14:18

368 Views

Suppose we have a list of strictly increasing positive numbers called nums. We have to find the length of the longest subsequence A (of length minimum 3) such that A[i] = A[i - 1] + A[i - 2] for all i > 1.So, if the input is like nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], then the output will be 6, as we can pick [1, 2, 3, 5, 8, 13].To solve this, we will follow these steps −A := numsn := size of AmaxLen := 0S := a new set ... Read More

Program to find longest number of 1s after swapping one pair of bits in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:12:39

541 Views

Suppose we have a binary string s. If we can swap at most one pair of characters in the string, we have to find the resulting length of the longest contiguous substring of 1s.So, if the input is like s = "1111011111", then the output will be 9, as we can swap s[4] and s[9] to get 9 consecutive 1s.To solve this, we will follow these steps −l := 0, cnt := 0, ans := 0for r in range 0 to size of s, docnt := cnt + (1 when s[r] is same as "0" otherwise 0)if cnt > 1, ... Read More

Program to find union of two given linked lists in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:08:44

445 Views

Suppose we have two sorted linked lists L1 and L2, we have to return a new sorted linked list that is the union of the two given lists.So, if the input is like L1 = [10, 20, 30, 40, 50, 60, 70] L2 = [10, 30, 50, 80, 90], then the output will be [10, 20, 30, 40, 50, 60, 70, 80, 90, ]To solve this, we will follow these steps −Define a function solve() . This will take L1, L2if L1 is empty, thenreturn L2if L2 is empty, thenreturn L1if value of L1 < value of L2, thenres := ... Read More

Program to convert linked list to zig-zag binary tree in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:06:45

230 Views

Suppose we have a singly linked list, we have to convert it to a binary tree path using following rules −The head of the linked list is the root.Each subsequent node is the left child of the parent when its value is less, otherwise it will be the right child.So, if the input is like [2, 1, 3, 4, 0, 5], then the output will beTo solve this, we will follow these steps −Define a function solve() . This will take nodeif node is null, thenreturn nullroot := create a tree node with value same as value of nodeif next ... Read More

Program to arrange linked list nodes based on the value k in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:04:47

154 Views

Suppose we have a singly linked list and another value k. We have to arrange the nodes so that all nodes whose values are less than k come first, and all nodes whose values are equal to k next, and finally other nodes at last. The constraint is that the relative ordering of the nodes should remain the same.So, if the input is like L = [4, 3, 6, 6, 6, 10, 8] k = 6, then the output will be [4, 3, 6, 6, 6, 10, 8, ]To solve this, we will follow these steps −less_head := create a ... Read More

Program to find linked list intersection from two linked list in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:02:50

228 Views

Suppose we have two sorted linked lists L1 and L2, we have to make a new sorted linked list which contains the intersection of these two lists.So, if the input is like L1 = [2, 4, 8] L2 = [3, 4, 8, 10], then the output will be [4, 8, ]To solve this, we will follow these steps −head := a new node with value 0cur := headwhile l1 and l2 are not empty, doif value of l1 < value of l2, thenl1 := next of l1otherwise when value of l2 < value of l1, thenl2 := next of l2otherwise, ... Read More

Advertisements