Partition Array into Disjoint Intervals in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:18:32

297 Views

Suppose we have an array nums, we have to partition it into two different subarrays called left and right such that −Each element in left subarray is less than or equal to each element in right subarray.left and right subarrays are non-empty.left subarray has the smallest possible size.We have to find the length of left after such a partitioning.So, if the input is like nums = [5, 0, 3, 8, 6], then the output will be 3 because left array will be [5, 0, 3] and right subarray will be [8, 6].To solve this, we will follow these steps −mx ... Read More

Find Maximum Number of Consecutive Values in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:14:35

483 Views

Suppose we have an array called coins with n elements, and it is representing the coins that we own. The value of the ith coin is denoted as coins[i]. We can make some value x if we can select some of our n coins such that their values sum up to x. We have to find the maximum number of consecutive values that we can get with the coins starting from and including 0.So, if the input is like coins = [1, 1, 3, 4], then the output will be 10, because0 = []1 = [1]2 = [1, 1]3 = ... Read More

Find Number of Pairs of Equal Substrings in Python

Arnab Chakraborty
Updated on 07-Oct-2021 12:04:37

376 Views

Suppose we are given two strings, both made of lowercase alphabets. We have to find out the number of quadruples (p, q, r, s) satisfying the given conditions −0

Sort Phrases Based on Their Appearances in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:59:00

153 Views

Suppose, we are given two lists; 'phrases' that contain a few selected phrases and 'sentences' that contain several sentences that may or may not contain the phrases from the other list. We have to find out if the various phrases in the first list appear in the second list and sort the first list phrases based on their appearances in the second list. We return the sorted list 'phrases' as output.So, if the input is like phrases = ['strong', 'durable', 'efficient'], sentences = ['the product is durable and efficient', 'strong and durable', 'it is efficient', 'like it because it is ... Read More

Find Buildings with a Better View in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:52:59

358 Views

Suppose, we are provided with an array that contains the heights of different buildings. The buildings are located on a line, and a building has a better view if it is not obstructed by another taller building. So provided the array containing heights, we have to find out the buildings that do not have other taller buildings to obstruct the view from them. The indices are returned from the array that satisfies the criteria.So, if the input is like height = [5, 6, 8, 7], then the output will be [2, 3]. The buildings in array index 0 and 1 ... Read More

Design a Queue that Moves the Most Recently Used Element to the End in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:49:46

485 Views

Suppose, we are asked to design a queue that moves the most recently used element to the end of it. The queue will be initialized with integer numbers 1 to n. Now we have to build a function so that whenever it is called, it moves the value from the position given as its input to the end of the queue. We will call the function multiple times, and the function will return the value currently at the end of the queue while performing its moving task.So, if the queue is initialized with a value n = 5, or it ... Read More

Sum of Maximum Subarray After an Operation in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:44:21

217 Views

Suppose, we are given an array containing integer numbers. We can perform an operation where we can replace the value of array[i] with its squared value; or array[i] * array[i]. Only one operation of this kind is permitted and we have to return the sum of the maximum possible subarray after the operation. The subarray cannot be empty.So, if the input is like array = [4, 1, -2, -1], then the output will be 17.If we replace the value in array[0] with its squared value, the array becomes [16, 1, -2, -1]. The maximum subarray that is possible from this ... Read More

Restore Array from Adjacent Pairs in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:39:28

282 Views

Suppose we have a 2D array called adPair of size n-1 where each adPair[i] has two elements [ui, vi] represents that the elements ui and vi are adjacent in an array called nums, in nums there are n unique elements. We have to find the array nums. If there are multiple solutions, return any of them.So, if the input is like adPair = [[3, 2], [4, 5], [4, 3]], then the output will be [2, 3, 4, 5]To solve this, we will follow these steps −my_map := an empty map to store list for different keysfor each pair (a, b) ... Read More

Find Distance Between Two Nodes in a Binary Tree using Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:33:05

819 Views

Suppose, we are given a binary tree and are asked to find the distance between two nodes in the binary tree. We find out the edges between the two nodes like in a graph and return the number of edges or the distance between them. A node of a tree has the structure as below −data : right : left : So, if the input is likeand the nodes between which we have to find the distance between are 2 and 8; then the output will be 4.The edges between the two nodes 2 and 8 are: (2, ... Read More

Find K-th Largest XOR Coordinate Value in Python

Arnab Chakraborty
Updated on 07-Oct-2021 11:19:54

205 Views

Suppose we have one m x n matrix. and another value k. Here the value of coordinate (a, b) of the matrix is the XOR of all matrix[i, j] where i in range (0 to a) and j in range (0 to b). We have to find the kth largest value (1-indexed) of all the coordinates of matrix.So, if the input is like5216And k = 1, then the output will be 7 because the value of coordinate (0, 1) is 5 XOR 2 = 7, and this is the largest oneTo solve this, we will follow these steps −m := ... Read More

Advertisements