Programming Articles - Page 2222 of 3366

Find consecutive 1s of length >= n in binary representation of a number in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:33:51

259 Views

Suppose, we have two integers x and n, our task is to search for the first consecutive stream of 1s (32-bit binary) which is greater than or equal to the value of n in length and return its position. If no such string exists, then return -1. For example, if x = 35, and n = 2, then result will be 31. The binary representation of 35 in a 32-bit integer is like −00000000000000000000000000100011. So two consecutive 1s are present at index 31, so the answer is 31.To solve this problem, we have to find the number of leading zeros, ... Read More

Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:32:10

198 Views

Suppose, we have a number n. Our task is to find the number of integers from 1 to n, which contains digits 0s and 1s only. So if n = 15, then output will be. As the numbers are 1, 10, 11To solve this, we will create integers using 0s and 1s using recursive function. Following code will help us to understand this better.Example Live Demo#include using namespace std; int numberOfValues(int p, int n) {    if (p > n)       return 0;    return 1 + numberOfValues(p * 10, n) + numberOfValues(p * 10 + 1, n); } int main() {    int n = 120;    cout

Find common elements in three sorted arrays in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:27:05

732 Views

Suppose we have three arrays with some elements. We have to find all the common elements that are present in these three arrays. Suppose these elements are [10, 12, 15, 20, 25], [10, 12, 13, 15] and [10, 12, 15, 24, 25, 26], then the common elements in these three arrays are 10, 12 and 15.Suppose current element traversed in array A1 be x, A2 be y and A3 be z. We can have the following cases for them −If x, y, and z are same, then we will print any of them, and increase each array elements by 1When ... Read More

Find the number of binary strings of length N with at least 3 consecutive 1s in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:24:56

386 Views

Suppose, we have an integer N, We have to find the number of all possible distinct binary strings of the length N, which have at least three consecutive 1s. So if n = 4, then the numbers will be 0111, 1110, 1111, so output will be 3.To solve this, we can use the Dynamic programming approach. So DP(i, x) indicates number of strings of length i with x consecutive 1s in position i + 1 to i + x. Then the recurrence relation will be like −DP(i, x) = DP(i – 1, 0) + DP(i – 1, x + 1).The ... Read More

Find common elements in three linked lists in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:23:38

237 Views

Suppose we have three linked lists. We have to find all the common elements that are present in these three linked lists. Suppose these lists are [10, 12, 15, 20, 25], [10, 12, 13, 15] and [10, 12, 15, 24, 25, 26], then the common elements in these three lists are 10, 12 and 15.We will use the hashing technique to solve this problem. To solve this, we have to follow these steps −Create an empty hash table, and go through each element in the first table, and insert the elements, and mark the frequency as 1Iterate through the second ... Read More

Find the number closest to n and divisible by m in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:21:17

2K+ Views

Suppose we have two integers n and m. We have to find the number closest to n and divide by m. If there are more than one such number, then show the number which has maximum absolute value. If n is completely divisible by m, then return n. So if n = 13, m = 4, then output is 12.To solve this, we can follow this steps −let q := n/m, and n1 := m*qif n * m > 0, then n2 := m * (q + 1), otherwise n2 := m * (q - 1)if |n – n1| < ... Read More

Find closest smaller value for every element in array in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:12:12

205 Views

Here we will see how to find the closest value for every element in an array. If an element x has the next element that is larger than it, and also present in the array, then that will be the greater value of that element. If the element is not present, then return -1. Suppose the array elements are [10, 5, 11, 6, 20, 12], then the greater elements are [11, 6, 12, 10, -1, 20]. As 20 has not greater value in the array, then print -1.To solve this, we will use the settings in C++ STL. The set ... Read More

How to initialize an array using lambda expression in Java?

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

2K+ Views

An array is a fixed size element of the same type. The lambda expressions can also be used to initialize arrays in Java. But generic array initializes cannot be used.Example-1interface Algebra {    int operate(int a, int b); } public class LambdaWithArray1 {    public static void main(String[] args) {       // Initialization of an array in Lambda Expression       Algebra alg[] = new Algebra[] {                                         (a, b) -> a+b,             ... Read More

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

122 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

Advertisements