Found 26504 Articles for Server Side Programming

Contains Duplicate III in C++

Arnab Chakraborty
Updated on 02-May-2020 07:11:25

330 Views

Suppose we have an array of integers, we have to check whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t. And the absolute difference between i and j is at most k. So if input is like [1, 2, 3, 1], then if k = 3 and t = 0, then return true.To solve this, we will follow these steps −Make a set s, n := size of nums arrayfor i in range 0 to n – 1x is index of set element starting ... Read More

Bitwise AND of Numbers Range in C++

Arnab Chakraborty
Updated on 02-May-2020 07:06:53

892 Views

Suppose we have a range [m, n] where 0 >= 1;          i++;       }       return m

Repeated DNA Sequences in C++

Arnab Chakraborty
Updated on 02-May-2020 06:59:41

1K+ Views

Suppose we have a DNA sequence. As we know, all DNA is composed of a series of nucleotides abbreviated such as A, C, G, and T, for example: "ACGAATTCCG". When we are studying DNA, it is sometimes useful to identify repeated sequences within the DNA.We have to write one method to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.So if the input is like “AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT”, then the output will be ["AAAAACCCCC", "CCCCCAAAAA"].To solve this, we will follow these steps −Define an array ret, n := size of s, create two sets called visited ... Read More

Compare Version Numbers in Python

Arnab Chakraborty
Updated on 02-May-2020 06:51:39

3K+ Views

Suppose we have to compare two version numbers version1 and version2. If the version1 > version2 then return 1; otherwise when version1 < version2 return -1; otherwise return 0. We can assume that the version strings are non-empty and contain only digits and the dot (.) characters. The dot character does not represent a decimal point and is used to separate number sequences. So for example, 2.5 is not "two and a half" or "halfway to version three", it is the fifth second-level revision of the second first-level revision.We can assume the default revision number for each level of a ... Read More

Lexicographical Numbers in C++

Arnab Chakraborty
Updated on 02-May-2020 06:48:59

3K+ Views

Suppose we have an integer n. We have to return 1 to n in lexicographic order. So for example when 13 is given, then the output will be [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9].To solve this, we will follow these steps −define one array ret of size ncurr := 1for i in range 0 to n – 1ret[i] := currif curr * 10 = n, then curr := curr / 10increase curr by 1while curr is divisible by 10, then curr := curr / 10return retExample(C++)Let us see the following implementation to get better understanding − Live Demo#include using namespace std; void print_vector(vector v){    cout

Range Sum Query 2D - Immutable in C++

Arnab Chakraborty
Updated on 02-May-2020 06:35:50

408 Views

Suppose we have a 2D matrix called matrix, we have to find the sum of the elements inside the rectangle defined by its upper left corner using (row1, col1) and lower right corner using (row2, col2).So if the matrix is like −3014256321120154101710305The above rectangle with the blue color defined by (2, 1) and (4, 3), this contains sum 8.So if we perform some query like sumRegion(2, 1, 4, 3), sumRegion(1, 1, 2, 2), sumRegion(1, 2, 2, 4), these will return 8, 11, 12 respectively.To solve this, we will follow these steps −Define a matrix called dp.Initialize the task as followsn ... Read More

Remove Sub-Folders from the Filesystem in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:10:36

278 Views

Suppose we have a list of folders, we have to remove all sub-folders in those folders and return in any order the folders after removing. Here if a folder[i] is located within another folder[j], it is denoted as subfolder of it. The paths will be like folder1/subfolder2/… etc.Suppose the input is like["/myfolder", "/myfolder/secondfolder", "/another/document", "/another/document/extrafolder", "/another/final"], then the output will be: ["/myfolder", "/another/final", "/another/document"]To solve this, we will follow these steps −sort the folder array based on the length of the pathscreate one map m, and another array ansfor i in range 0 to size of path array – 1s ... Read More

Toss Strange Coins in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:05:59

669 Views

Suppose we have some coins. The i-th coin has a probability prob[i] of facing heads when tossed. We have to show the probability that the number of coins facing heads equals target if you toss every coin exactly once. So if the prob array is like [0.5, 0.5, 0.5, 0.5, 0.5] and target is 0, then the output will be 0.03125.To solve this, we will follow these steps −n := size of prob arraycreate one 2d array of size n x (target + 5)set dp[0, 0] = 1 – prob[0] and dp[0, 1] := prob[0]for i in range 1 to ... Read More

Meeting Scheduler in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:03:11

1K+ Views

Suppose we have the availability time slots lists slots1 and slots2 of two people and a meeting duration d, we have to find the earliest time slot that works for both of them and is of duration d. If there is no common time slot that satisfies the requirements, then show an empty array. Here the format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end. we can assume that no two availability slots of the same person intersect with each other. That is, for any two time ... Read More

Dice Roll Simulation in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:58:42

675 Views

Suppose a die simulator generates a random number from 1 to 6 for each roll. We want to introduced a constraint to the generator such that it cannot roll the number i more than rollMax[i] (1-indexed) consecutive times. Consider we have an array of integers rollMax and an integer n, we have to return the number of distinct sequences that can be obtained with exact n rolls. The two sequences are considered different if at least one element differs from each other. So if n is 2, then rollMax = [1, 1, 2, 2, 2, 3], then the output will ... Read More

Advertisements