Programming Articles - Page 2220 of 3366

Find Intersection of all Intervals in C++

Arnab Chakraborty
Updated on 19-Dec-2019 09:50:23

864 Views

Suppose, we have N intervals in the form {L, R}, the L is the starting time, and R is the ending time. We have to find an intersection of all intervals. An intersection is an interval that lies within all of the given intervals. If no such found, return -1. For example, if the intervals are like [{1, 6}, {2, 8}, {3, 10}, {5, 8}, The output interval is {5, 6}To solve this problem, we will follow these steps −Consider the first interval is the final intervalStarting from the second interval, try searching for the intersection. Two cases can be ... Read More

What is a casting expression in Java?

raja
Updated on 11-Jul-2020 07:40:37

863 Views

A cast expression provides a mechanism to explicitly provide a lambda expression's type if none can be conveniently inferred from context. It is also useful to resolve ambiguity when a method declaration is overloaded with unrelated functional interface types.SyntaxObject o = () -> { System.out.println("TutorialsPoint"); }; // Illegal: Object o = (Runnable) () -> { System.out.println("TutorialsPoint"); }; // LegalExampleinterface Algebra1 {    int operate(int a, int b); } interface Algebra2 {    int operate(int a, int b); } public class LambdaCastingTest {    public static void main(String[] args) {       printResult((Algebra1)(a, b) -> a + b);  // Cast Expression ... Read More

Find indices of all occurrence of one string in other in C++

Arnab Chakraborty
Updated on 19-Dec-2019 09:42:14

3K+ Views

Suppose we have string str, and another substring sub_str, we have to find the indices for all occurrences of the sub_str in str. Suppose the str is “aabbababaabbbabbaaabba”, and sub_str is “abb”, then the indices will be 1 9 13 18.To solve this problem, we can use the substr() function in C++ STL. This function takes the initial position from where it will start checking, and the length of the substring, if that is the same as the sub_str, then returns the position.Example Live Demo#include using namespace std; void substrPosition(string str, string sub_str) {    bool flag = false;    for ... Read More

Find if the given number is present in the infinite sequence or not in C++

Arnab Chakraborty
Updated on 19-Dec-2019 08:15:27

264 Views

Suppose we have three integers a, b and c. Suppose in an infinite sequence, a is the first term, and c is a common difference. We have to check whether b is present in the sequence or not. Suppose the values are like a = 1, b = 7 and c = 3, Then the sequence will be 1, 4, 7, 10, …, so 7 is present in the sequence, so the output will be ‘yes’.To solve this problem, we have to follow these two steps −When c = 0, and a = b, then print yes, and if a ... Read More

Find if an expression has duplicate parenthesis or not in C++

Arnab Chakraborty
Updated on 19-Dec-2019 08:08:01

358 Views

Consider we have an expression exp, and we have to check whether the exp has a duplicate set of parentheses around it or not. An expression will have duplicate parentheses if one sub-expression will be surrounded by more than one parentheses set. For example, if the expression is like −(5+((7−3)))Here the sub-expression (7 – 3) is surrounded by two parentheses pair, so these are duplicate parentheses.To solve this problem, we will use stacks. We will iterate through each character in the exp, and if the character is opening parentheses ‘(’, or any of the operator or operand, then push it ... Read More

Find if an array contains a string with one mismatch in C++

Arnab Chakraborty
Updated on 19-Dec-2019 08:05:55

199 Views

Suppose we have a string s, and another array of strings A. We have to find whether the array is containing a string with the one-character difference from the current string of different lengths. Suppose the string is like “banana”, and the array looks like [“bana”, “orange”, “banaba”, “banapy”], the result will be true, as there is one string banaba, here only one character is different than a banana.To solve this problem, we will follow some steps −Traverse through given string s, and check for every string in the array, then follow these steps for every string in arr −Check ... Read More

Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9 in C++

Arnab Chakraborty
Updated on 19-Dec-2019 08:01:30

87 Views

We have to find the GCD of two numbers of which one number can be as big as (109 ^ 109), which cannot be stored in some data types like long or any other. So if the numbers are a = 10248585, n = 1000000, b = 12564, then result of GCD(a^n, b) will be 9.As the numbers are very long, we cannot use the Euclidean algorithm. We have to use the modular exponentiation with O(log n) complexity.Example Live Demo#include #include using namespace std; long long power(long long a, long long n, long long b) {    long long res = ... Read More

Find frequency of each element in a limited range array in less than O(n) time in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:58:53

340 Views

Suppose we have an array of integers. The array is A, and the size is n. Our task is to find the frequency of all elements in the array less than O(n) time. The size of the elements must be less than one value say M. Here we will use the binary search approach. Here we will recursively divide the array into two parts if the end elements are different, if both its end elements are the same, it means all elements in the array are the same as the array is already sorted.Example Live Demo#include #include using namespace std; void ... Read More

Find four elements a, b, c and d in an array such that a+b = c+d in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:55:06

831 Views

Suppose we have a list of integers. Our task is to find four distinct integers as two pairs like (a, b) and (c, d), such that a+b = c+d. If there are multiple answers, then print only one. Suppose the array elements are like: A = [7, 5, 9, 3, 6, 4, 2], then pairs can be (7, 3) and (6, 4)Here we will use the hashing technique. We use the sum as key as pair as the value in the hash table. We have to follow these steps to solve this problem.For i in range 0 to n – ... Read More

Find three closest elements from given three sorted arrays in C++

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

410 Views

Suppose we have three sorted arrays A, B and C, and three elements i, j and k from A, B and C respectively such that max(|A[i] – B[i]|, |B[j] – C[k]|, |C[k] – A[i]|) is minimized. So if A = [1, 4, 10], B = [2, 15, 20], and C = [10, 12], then output elements are 10, 15, 10, these three from A, B and C.Suppose the size of A, B and C are p, q and r respectively. Now follow these steps to solve this −i := 0, j := 0 and k := 0Now do the following ... Read More

Advertisements