Programming Articles - Page 2221 of 3363

How to use a return statement in lambda expression in Java?

raja
Updated on 11-Jul-2020 07:31:48

12K+ Views

A return statement is not an expression in a lambda expression. We must enclose statements in braces ({}). However, we do not have to enclose a void method invocation in braces. The return type of a method in which lambda expression used in a return statement must be a functional interface.Example 1public class LambdaReturnTest1 {    interface Addition {       int add(int a, int b);    }    public static Addition getAddition() {       return (a, b) -> a + b; // lambda expression return statement    }    public static void main(String args[]) {       System.out.println("The ... Read More

Find smallest number K such that K % p = 0 and q % K = 0 in C++

Arnab Chakraborty
Updated on 18-Dec-2019 13:10:47

132 Views

Suppose we have two integers P and Q. We have to find smallest number K, such that K mod P = 0 and Q mod K = 0. Otherwise print -1. So if the P and Q are 2 and 8, then K will be 2. As 2 mod 2 = 0, and 8 mode 2 = 0.In order for K to be possible, Q must be divisible by P. So if P mod Q = 0 then print P otherwise print -1.Example Live Demo#include using namespace std; int getMinK(int p, int q) {    if (q % p == 0)    return p;    return -1; } int main() {    int p = 24, q = 48;    cout

Find Selling Price from given Profit Percentage and Cost in C++

Arnab Chakraborty
Updated on 18-Dec-2019 13:09:23

304 Views

Consider we have the selling price, and percentage of profit or loss is given. We have to find the cost price of the product. The formula is like below −$$Cost Price=\frac{Sell Price∗100}{100+percentage profit}$$ $$Cost Price=\frac{Sell Price∗100}{100+percentage loss}$$Example Live Demo#include using namespace std; float priceWhenProfit(int sellPrice, int profit) {    return (sellPrice * 100.0) / (100 + profit); } float priceWhenLoss(int sellPrice, int loss) {    return (sellPrice * 100.0) / (100 - loss); } int main() {    int SP, profit, loss;    SP = 1020;    profit = 20;    cout

Find safe cells in a matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 13:07:14

190 Views

Suppose we have a matrix mat[][]. It has character Z and P. The Z is the zombie and P is the plant. And another character * is a bare land. A zombie can attack the plant, when plant is adjacent to zombie. We have to find number of plants, that are safe from zombie. Suppose the matrix is like below −So there are only two plants that are safe.We will traverse the matrix element by element, then when the current element is a plant, then check that the plant is surrounded by zombie or not, if not then increase the ... Read More

Find repeated character present first in a string in C++

Arnab Chakraborty
Updated on 18-Dec-2019 13:03:35

445 Views

Suppose we have a string; we have to find first character that is repeated. So is the string is “Hello Friends”, the first repeated character will be l. As there are two l’s one after another.To solve this, we will use the hashing technique. Create one hash table, scan each character one by one, if the character is not present, then insert into hash table, if it is already present, then return that character.Example Live Demo#include #include using namespace std; char getFirstRepeatingChar(string &s) {    unordered_set hash;    for (int i=0; i

Find product of prime numbers between 1 to n in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:59:05

379 Views

Suppose we have a number n. We have to find the product of prime numbers between 1 to n. So if n = 7, then output will be 210, as 2 * 3 * 5 * 7 = 210.We will use the Sieve of Eratosthenes method to find all primes. Then calculate the product of them.Example Live Demo#include using namespace std; long PrimeProds(int n) {    bool prime[n + 1];    for(int i = 0; i

Find prime number K in an array such that (A[i] % K) is maximum in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:56:56

148 Views

Suppose we have an array A with n integers. We have to find one element K such that K is prime, and A[i] mod K is maximum for all valid i among all possible values of K. If no such numbers are found, then return -1. For example, if A = [2, 10, 15, 7, 6, 8, 13], then the output will be 13. There are three prime numbers 2, 7, and 13. The maximum possible values of A[i] mod 2 is 1, (15 mod 2), for 7, it will be 6 mod 7 = 6, and for 13, it ... Read More

Find permutation of n which is divisible by 3 but not divisible by 6 in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:53:38

128 Views

Suppose we have a number n, and we have to find the permutation of this number, that is divisible by 3, but not divisible by 6. If no such value can be made, then return -1. For example, if n is 336, then the output can be 363.As we know a number is divisible by 6 means it is divisible by 3 and 2. So each even number that is divisible by 3, will be divisible by 6. If we interchange the digits of a number which is divisible by 3 and also even, to make it odd, it will ... Read More

Find permutation of first N natural numbers that satisfies the given condition in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:52:46

258 Views

Suppose we have two integers N and K, and we have to find the permutation P of first N natural numbers such that there are exactly K elements which satisfies the condition GCD(P[i], i) > 1 for all 1

Find the multiple of x which is closest to a^b in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:54:03

159 Views

Suppose we have three integers, a, b and x. The task is to get the multiple of x, which is closest to ab. So if a = 5, b = 4 and x = 3, then output will be 624. As 54 = 625, and 624 is the multiple of 3, which is closest to 625.The task is simple. we have to follow these steps to solve this problem −calculate num := abThen find f := floor of (num/x)Now the closest element at the left will be cl = x * f, and at right will be cr = x ... Read More

Advertisements