Programming Articles - Page 1977 of 3363

Importance of destroyForcibly() method in Java 9?

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

453 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

300 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

691 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

715 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

Path with Maximum Gold in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:55:21

559 Views

Suppose in a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 means that is empty. We have to find the maximum amount of gold that you can collect under the conditions −Every time we are pointing a cell we will collect all the gold in that cell.From our position we can walk one step to the left, right, up or down.We cannot visit the same cell more than once.Never visit a cell with 0 gold.So if the input is like [[0, 6, 0], ... Read More

Stepping Numbers in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:50:13

396 Views

Suppose we have two integers low and high, we have to find and show a sorted list of all the Stepping Numbers in the range [low, high] inclusive. A Stepping Number is an integer means all of its adjacent digits have an absolute difference of exactly 1. For example, 321 is a Stepping Number but 421 is not. So if the input is like low := 0 and high := 21, then the result will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21]To solve this, we will follow these steps −create one array tempmake ... Read More

Remove All Adjacent Duplicates in String II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:47:11

721 Views

Suppose a string s is given, a k duplicate removal consists of choosing k adjacent and equal letters from string s and removing them causing the left and the right side of the deleted substring to concatenate together. We will repeatedly make k duplicate removals on the given string s until we cannot change any remaining. We have to find the final string after all such duplicate removals have been made. So if the input is like s = “deeedbbcccbdaa”, and k = 3, then the output will be “aa”, at first delete the “eee” and “ccc” and we will ... Read More

Stone Game II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:44:15

487 Views

Suppose there are two persons Alice and Bob, they are continuing their games with piles of stones. There are a number of piles placed in a row, and each pile has a positive integer number of stones in an array piles[i]. Our objective of the game is to end with the most stones. Alice and Bob take the turns, with Alice starting first. Initially, M = 1. On each player's turn, that player can take all the stones in the first X remaining piles, here 1

Longest Well-Performing Interval in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:40:28

280 Views

Suppose we have hours list, this is a list of the number of hours worked per day for a given employee. Here a day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8. One well-performing interval is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days. We have to find the length of the longest well-performing interval. So if the input is like [9, 9, 6, 0, 6, 6, 9], so then then the output will be ... Read More

Advertisements