Find Longest Number of 1s After Swapping One Pair of Bits in Python

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

567 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

Convert Linked List to Binary Search Tree in C++

Arnab Chakraborty
Updated on 19-Nov-2020 07:11:15

611 Views

Suppose we have a singly linked list where elements are arranged in non-decreasing order, we have to convert it to a height balanced binary search tree. So if the list is like [-10, -3, 0, 5, 9], The possible tree will be like −To solve this, we will follow these steps −If the list is empty, thenreturn nullDefine a recursive method called sortedListToBST() this will take list start nodex := address of the previous node of mid node from list amid := exact mid nodecreate a new node with value by taking from value of midnextStart := next of mid ... Read More

Find Union of Two Given Linked Lists in Python

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

469 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

Convert Linked List to Zig Zag Binary Tree in Python

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

252 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

Arrange Linked List Nodes Based on Value K in Python

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

175 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

Find Linked List Intersection in Python

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

264 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

Find Folded List from a Given Linked List in Python

Arnab Chakraborty
Updated on 19-Nov-2020 07:00:53

310 Views

Suppose we have a linked list. We have to take the first half of the linked list and fold over the second half then merge the intersecting nodes by taking their sum. Finally, we have to return the resulting head of the linked list.So, if the input is like [5, 8, 1, 2, 4, 7, 5], then the output will be [2, 5, 15, 10, ]To solve this, we will follow these steps −temp := 0ptr := nodewhile ptr is not null, dotemp := temp + 1ptr := next of ptrt := quotient of temp / 2m := nodestk := ... Read More

Remove Last Occurrence of Target from Linked List in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:57:56

423 Views

Suppose we have a singly linked list, and another value called target, we have to remove the last occurrence of target in the given list.So, if the input is like [5, 4, 2, 6, 5, 2, 3, 2, 4, 5, 4, 7], target = 5, then the output will be [5, 4, 2, 6, 5, 2, 3, 2, 4, 4, 7, ]To solve this, we will follow these steps −head := nodek := null, prev := nullfound := Falsewhile node is not null, doif value of node is same as target, thenfound := Trueprev := kk := nodenode := next ... Read More

Check If List of Points Form a Straight Line in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:55:47

3K+ Views

Suppose we have a list coordinates in a Cartesian plane, we have to check whether the coordinates form a straight line segment or not.So, if the input is like coordinates = [(5, 5), (8, 8), (9, 9)], then the output will be True, as these points are forming a line segment with a slope 1.To solve this, we will follow these steps −(x0, y0) := coordinates[0](x1, y1) := coordinates[1]for i in range 2 to size of coordinates list - 1, do(x, y) := coordinates[i]if (x0 - x1) * (y1 - y) is not same as (x1 - x) * (y0 ... Read More

Find Lexicographically Smallest Non-Palindromic String in Python

Arnab Chakraborty
Updated on 19-Nov-2020 06:54:38

381 Views

Suppose we have a string s that is a palindrome. We have to change one character such that s is no longer a palindrome and it is lexicographically smallest.So, if the input is like s = "level", then the output will be "aevel", as we can change the first "l" to "a" to get the lexicographically smallest string that is not palindrome.To solve this, we will follow these steps −for i in range 0 to integer part of(size of s / 2), doif s[i] is not same as "a", thens := a new list from all characters in ss[i] := ... Read More

Advertisements