Programming Articles - Page 1976 of 3363

Rectangle Area in C++

Arnab Chakraborty
Updated on 02-May-2020 07:14:23

410 Views

Suppose we want to find the total area covered by two rectilinear rectangles in a 2D plane. Here each rectangle is defined by its bottom left corner and top right corner as shown in the figure.To solve this, we will follow these steps −if C = E or A >= G or B >= H or D = H || D

Contains Duplicate III in C++

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

358 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

920 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

435 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

How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?

raja
Updated on 01-May-2020 17:24:58

234 Views

JShell is a command-line prompt tool introduced in Java 9, and it is also called a REPL tool to evaluate simple statements, executes it, and print the output immediately.A Map interface specifies a contract to implement collections of elements in the form of key/value pairs. Java collection classes that implement the Map interface are HashMap, LinkedHashMap, and TreeMap.In the below code snippet, the elements of HashMap are not guaranteed to store either in an insertion order or in the sorted order of keys.Snippet-1jshell> HashMap hashMap = new HashMap(); hashMap ==> {} jshell> hashMap.put("Adithya", 101); $2 ==> null jshell> hashMap.put("Jai", 102); $3 ==> null ... Read More

Differences between CompletableFuture and Future in Java 9?

raja
Updated on 01-May-2020 11:45:14

6K+ Views

CompletableFuture class implements Future interface in Java. CompletableFuture can be used as a Future that has explicitly completed. The Future interface doesn’t provide a lot of features, we need to get the result of asynchronous computation using the get() method, which is blocked, so there is no scope to run multiple dependent tasks in a non-blocking fashion whereas CompleteFuture class can provide the functionality to chain multiple dependent tasks that run asynchronously, so we can create a chain of tasks where the next task is triggered when the result of the current task is available.Syntaxpublic class CompletableFuture extends Object implements Future, CompletionStageExampleimport java.util.function.Supplier; import java.util.concurrent.CompletableFuture; ... Read More

How to get a snapshot of information about Process API in Java 9?

raja
Updated on 01-May-2020 08:33:51

245 Views

Java 9 has improved Process API by including new methods and introduced new interfaces ProcessHandle and ProcessHandle.Info to get all the details regarding the process and its information.ProcessHandle interface can identify and provide control of native processes. Each individual process can be monitored for liveness, listed its children, get information about the process, or destroys it. ProcessHandle.Info interface gives information snapshots about a process.SyntaxProcessHandle.Info info()Examplepublic class ProcessSnapShotTest {    public static void main(String[] args) {       ProcessHandle currentProcessHandleImpl = ProcessHandle.current();             // Process snapshot of the current running process with ProcessHandle.Info:       ProcessHandle.Info processInfo = ... Read More

Advertisements