Programming Articles - Page 1977 of 3366

Bitwise AND of Numbers Range in C++

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

902 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

415 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

216 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

229 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

Importance of destroyForcibly() method in Java 9?

raja
Updated on 30-Apr-2020 17:21:25

441 Views

The destroyForcibly() method can be used to kill a process. It will be needed if the process has finished or has frozen. For instance, the isAlive() method returns true after destroyForcibly() is called. The destroyForcibly() method returns true if the termination successfully requested, otherwise returns false.Syntaxboolean destroyForcibly()In the below example, we will able to launch a notepad application, and it will be terminated after the destroyForcibly() method called.Exampleimport java.io.IOException; import java.lang.ProcessBuilder; public class DestroyForciblyTest {    public static void main(String args[]) throws IOException, InterruptedException {       ProcessBuilder pBuilder = new ProcessBuilder();       pBuilder.command("notepad.exe");       ... Read More

Remove Sub-Folders from the Filesystem in C++

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

285 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

Advertisements