Found 33676 Articles for Programming

How a thread can interrupt another thread in Java?

AmitDiwan
Updated on 13-Jul-2020 11:57:50

961 Views

The ‘interrupt’ function can be used in Java to interrupt the execution of a thread with the help of the exception InterruptedException.The below example shows how the currently executing thread stops execution (because of a new exception raised in the catch block) once it is interrupted −Example Live Demopublic class Demo extends Thread {    public void run()    {       try       {          Thread.sleep(150);          System.out.println("In the 'run' function inside try block");       }       catch (InterruptedException e)       {       ... Read More

Sum of list with stream filter in Java

AmitDiwan
Updated on 13-Jul-2020 11:53:38

1K+ Views

To get sum of list with stream filter in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args)    {       List my_list = new ArrayList();       my_list.add(11);       my_list.add(35);       my_list.add(56);       my_list.add(78);       my_list.add(91);       System.out.println(sum(my_list));    }    public static int sum(List my_list)    {       System.out.println("In the main function, the sum of list with filter is ");       return my_list.stream().filter(i -> i > 5).mapToInt(i -> i).sum();    } ... Read More

Structure and Members of the Java Program

AmitDiwan
Updated on 13-Jul-2020 11:52:46

318 Views

While writing any piece of code in Java, there is a certain set of rules and regulations that need to be followed, that is considered as a standard. For example − A class contains variables, and functions. The functions can be used to work with the variables. Classes can be extended, and improvised too.Basic structureList of packages that are imported; public class {    Constructor (can be user defined or implicitly created)    {       Operations that the constructor should perform;    }    Data elements/class data members;    User-defined functions/methods;    public static void main (String ... Read More

Handshakes That Don't Cross in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:48:50

328 Views

Suppose we have an even number of people n that stand around a circle and each person shakes hands with someone else, so that there will be n / 2 handshakes total. We have to find the number of ways these handshakes could occur such that none of the handshakes cross. The answers may be very large so return the answer mod 10^9 + 7.So, if the input is like n = 2, then the output will be 1To solve this, we will follow these steps −m := 10^9 + 7Define an array dp of size (n+1)dp[0] := 1for initialize i := 0, when i

Palindrome Removal on C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:46:04

341 Views

Suppose we have an integer array called arr, now in one move we can select a palindromic subarray from index i to j where i

Divide Chocolate in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:43:46

388 Views

Suppose we have one chocolate bar that consists of some chunks. In each chunk it has its own sweetness given by a list called sweetness. If we want to share the chocolate among K friends so we start cutting the chocolate bar into K+1 piece using K cuts, now each piece consists of some consecutive chunks. If we take out the piece with the minimum total sweetness and give the other pieces to our friends. We have to find the maximum total sweetness of the piece we can get by cutting the chocolate bar optimally.So, if the input is like ... Read More

Valid Palindrome III in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:40:52

480 Views

Suppose we have a string s and another number k; we have to check whether the given string is a K-Palindrome or not.A string is said to be K-Palindrome if it can be transformed into a palindrome by removing at most k characters from it.So, if the input is like s = "abcdeca", k = 2, then the output will be true as by removing 'b' and 'e' characters, it will be palindrome.To solve this, we will follow these steps −Define a function lcs(), this will take s, t, n := size of sadd one blank space before sadd one ... Read More

Minimum Time to Build Blocks in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:38:21

196 Views

Suppose we have a list of blocks, if we have blocks[i] = t, this means that the i-th block needs t units of time to be built. A block can only be built by exactly one worker. Single worker can either split into two workers or build a block then go home. These two decisions take some time. The time cost of spliting one worker into two workers is given as a number called split.So, if the input is like blocks = [1, 2], and split = 5, then the output will be 7 as we can split the worker ... Read More

Maximum Number of Ones in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:36:39

145 Views

Suppose we have a matrix M with dimensions w x h, such that every cell has value 0 or 1, and any square sub-matrix of M of size l x l has at most maxOnes number of ones. We have to find the maximum possible number of ones that the matrix M can have.So, if the input is like w = 3, h = 3, l = 2, maxOnes = 1, then the output will be 4 as in a 3*3 matrix, no 2*2 sub-matrix can have more than 1 one. The best solution that has 4 ones is −101000101To ... Read More

Optimize Water Distribution in a Village in Python

Arnab Chakraborty
Updated on 11-Jul-2020 12:34:17

714 Views

Suppose there are n houses in a village. We have to supply water for all the houses by building wells and laying pipes. For each house i, we can either build a well inside it, the building cost will be wells[i], or pipe in water from another well to it. The costs to lay pipes between houses are given by the array pipes, where each pipes[i] is [house1, house2, cost] represents the cost to connect house1 and house2 together using a pipe. These connections are bidirectional. We have to find the minimum total cost to supply water to all houses.So, ... Read More

Advertisements