Snapshot Array in Python

Arnab Chakraborty
Updated on 30-Apr-2020 05:56:46

603 Views

Suppose we have to Implement a SnapshotArray that supports the following interfaces −SnapshotArray(int length) this will initialize the array-like data structure with the given length. Initially, each element equals 0.set(index, val) this will sets the element at the given index to be equal to val.snap() will take a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1.get(index, snap_id) this will return the value at the given index, at the time we took the snapshot with the given snap_idSo if the array size is 2, it is set using [0, 5], after ... Read More

Kth Smallest Element in a Sorted Matrix in Python

Arnab Chakraborty
Updated on 30-Apr-2020 05:54:09

460 Views

Suppose we have a n x n matrix where each of the rows and columns are sorted in increasing order, we have to find the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth unique element. So if the input is like [[1, 5, 9], [10, 11, 13], [12, 13, 15]], if k = 8, then the output will be 13.To solve this, we will follow these steps −define one method called checkVal() and the arguments are matrix and valuei := 0, j := length of matrix[0] – ... Read More

Ugly Number III in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:38:57

246 Views

Suppose we have to write a program to find the n-th ugly number. The Ugly numbers are positive integers which are divisible by a or b or c. So for example, if n = 3 and a = 2, b = 3 and c = 5, then the output will be 4, as the ugly numbers are [2, 3, 4, 5, 6, 8, 9, 10], the third one is 4.To solve this, we will follow these steps −make a method called ok(), this will take x, a, b, c, this will act like below −return (x/a) + (x/b) + (x/c) ... Read More

Find Smallest Common Element in All Rows in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:35:19

221 Views

Suppose we have a matrix mat where every row is sorted in non-decreasing order, we have to find the smallest common element in all rows. If there is no common element, then return -1. So if the matrix is like −1234524581035791113579The output will be 5To solve this, we will follow these steps −Define a map m, n := row count of matrix, if n is not 0, then x = column size, otherwise 0for i in range 0 to n – 1for j in range 0 to x – 1if m[mat[i, j]] + 1 = i + 1, then increase ... Read More

Minimum Knight Moves in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:32:23

890 Views

Suppose we have an infinite chessboard with coordinates from -infinity to +infinity, and we have a knight at square [0, 0]. A knight has 8 possible moves it can make, as shown below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.We have to find the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.So if the input is like x = 5 and y = 5, then the output will be 4. This will be like [0, 0] → [2, 1] ... Read More

Reverse Substrings Between Each Pair of Parentheses in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:28:04

387 Views

Suppose we have a string s that consists of lower case letters and brackets. We have to reverse the strings in each pair of matching parentheses, starting from the innermost one. And the result should not contain any brackets. So if the input is like "(hel(lowo)rld)", then the output will be "dlrlowoleh", so from the beginning, it is changed like: "(hel(lowo)rld)" → "(helowolrld)" → “dlrowoleh”.To solve this, we will follow these steps −n := size of string, make an array called par whose length is n, define a stack stfor i in range 0 to n – 1if s[i] is ... Read More

Shortest Distance to Target Color in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:23:16

263 Views

Suppose we have an array color, in which there are three colors: 1, 2 and 3. We have given some queries. Each query consists of two integers i and c, we have to find the shortest distance between the given index i and the target color c. If there is no solution, then return -1. So if the colors array is like [1, 1, 2, 1, 3, 2, 2, 3, 3], and the queries array is like [[1, 3], [2, 2], [6, 1]], the output will be [3, 0, 3]. This is because the nearest 3 from index 1 is ... Read More

Before and After Puzzle in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:18:40

168 Views

Suppose we have a list of phrases, generate a list of Before and After puzzles. Here a phrase is a string that consists of lowercase letters and spaces only. No space will be there at the starting and ending positions. There are no consecutive spaces in a phrase.The before and after puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase. We have to find the Before and After puzzles that can be formed by every two phrases phrases[i] and phrases[j] ... Read More

Convex Polygon in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:12:32

2K+ Views

Suppose we have a list of points that form a polygon when joined sequentially, we have to find if this polygon is convex (Convex polygon definition). We have to keep in mind that there are at least 3 and at most 10, 000 points. And the coordinates are in the range -10, 000 to 10, 000.We can assume the polygon formed by given points is always a simple polygon, in other words, we ensure that exactly two edges intersect at each vertex and that edges otherwise don't intersect each other. So if the input is like: [[0, 0], [0, 1], ... Read More

Validate IP Address in Python

Arnab Chakraborty
Updated on 29-Apr-2020 16:08:17

2K+ Views

Suppose we have a string; we have to check whether the given input is a valid IPv4 address or IPv6 address or neither.The IPv4 addresses are canonically represented in dotted-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), For example, 192.168.254.1; Besides, leading zeros in the IPv4 address is invalid. For example, the address 192.168.254.01 is invalid.The IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, suppose the address is 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid address. ... Read More

Advertisements