Found 7347 Articles for C++

Find all possible outcomes of a given expression in C++

Arnab Chakraborty
Updated on 18-Dec-2019 06:53:17

199 Views

Suppose we have an arithmetic expression without parentheses. Our task is to find all possible outcomes of that expression. Suppose the expression is like 1+2*3-4, this can be interpreted like below −1+(2*(3-4)) = 1 + (2* -1) = -1(1+2)*(3-4) = 3 * -1 = -31+((2*3)-4) = 1 + (6 - 4) = 3((1+2)*3)-4 = (3 * 3) - 4 = 51+(2*3)-4 = 1 + 6 – 4 = 3To solve this problem, we have to follow these steps −Initially set res as emptyFor every operator x, do the following −Recursively evaluate all possible values on left of x, let the ... Read More

Find a range that covers all the elements of given N ranges in C++

Arnab Chakraborty
Updated on 18-Dec-2019 06:50:45

103 Views

Suppose we have a n ranges containing L and R. We have to check or find the index of 0 – based of the range which covers all the other given n – 1 ranges. If there is no such range, display -1. For example, if L = [2, 4, 3, 1], and R = [4, 6, 7, 9], then the output is 3. So it means the range at 3rd index (1 to 9) covers all the elements of other n – 1 ranges.Since all L and R points are distinct, find the range of smallest L and largest ... Read More

Find a range of composite numbers of given length in C++

Arnab Chakraborty
Updated on 18-Dec-2019 06:48:45

219 Views

Suppose we have a number n. We have to find the range of positive integers, where all the numbers in the range is composite, and the length of the range is n. If there are more than one range, then print any one range. The composite number is a number where it has at least one divisor other than 1 and itself.As the length of the range is n, then if the first number is a, then the other numbers are a + 1, a + 2, …, a + n – 1, all should be composite. If we see ... Read More

Find a permutation of 2N numbers such that the result of given expression is exactly 2K in C++

Arnab Chakraborty
Updated on 18-Dec-2019 06:45:42

49 Views

Suppose we have two integers N and K. We have to find first permutation of 2N number of natural numbers, such that the following equation is satisfied.$$\displaystyle\sum\limits_{i=1}^N\lvert A_{2i-1}-A_{2i}\rvert+\lvert \displaystyle\sum\limits_{i=1}^N A_{2i-1}-A_{2i} \rvert=2K$$The value of K should be less than or equal to N. For example, if N = 4 and K = 1, then output will be 2 1 3 4. The result of the given expression will be (|2 – 1| + |3 – 4|) – (|2 – 1 + 3 – 4|) = 2.The idea is simple, consider we have a sorted sequence like 1, 2, 3, 4, 5, ... Read More

Find a local minima in an array in C++

Arnab Chakraborty
Updated on 18-Dec-2019 06:43:41

325 Views

Suppose we have an array A with n elements. We have to find the local minima of the array. In array A, the element A[x] is said to be local minima if it is less than or equal to both of its neighbors. For corner elements only one neighbor will be considered. And if there are more than one local minima available, then return only one. For example, if the array is like [9, 6, 3, 14, 5, 7, 4], then the local minima can be 3, 5 and 4, so this algorithm can return only one of them.To solve ... Read More

Comparing String objects using Relational Operators in C++

Arnab Chakraborty
Updated on 17-Dec-2019 13:31:15

367 Views

Here we will see how to compare two strings in C++. The C++ has string class. It also has the compare() function in the standard library to compare strings. But here we will use the conditional operators like ==, !=, , =. These operators check the strings character by characters. Let us see the code to get better idea.Example Live Demo#include using namespace std; void compareStrings(string s1, string s2) {    if (s1 != s2)       cout

Code valid in both C and C++ but produce different output

Arnab Chakraborty
Updated on 17-Dec-2019 13:26:14

63 Views

Here we will see some program that will return different results if they are compiled in C or C++ compilers. We can find many such programs, but here we are discussing about some of them.In C and C++, the character literals are treated as different manner. In C, they are treated as int but in C++, they are treated as characters. So if we check the size using sizeof() operator, it will return 4 in C, and 1 in C++.Example Live Demo#include int main() {    printf("The character: %c, size(%d)", 'a', sizeof('a')); }OutputThe character: a, size(4)Example#include int main() {    printf("The ... Read More

Chrono in C++

Arnab Chakraborty
Updated on 17-Dec-2019 13:23:17

327 Views

In this section we will see what is the Chrono library in C++. This Chrono library is used for date and time. Timers and clocks are different in different systems. So if we want to improve time over precision we can use this library.In this library, it provides precision-neutral concept, by separating the durations and point of time.The duration objects are used to express time span by means of a count like minute, two hours or ten minutes. For example, 30 seconds is represented by a duration consisting of 30 ticks of unit of 1 seconds.Example Live Demo#include #include ... Read More

How to use getline() in C++ when there are blank lines in input?

Arnab Chakraborty
Updated on 17-Dec-2019 13:20:55

784 Views

In C++, we use the getline() function to read lines from stream. It takes input until the enter button is pressed, or user given delimiter is given. Here we will see how to take the new line character as input using the getline() function. Let us see the following implementation to get the idea.Example Live Demo#include using namespace std; int main() {    string str;    int term = 4;    while (term--) {       getline(cin, str);       while (str.length()==0 )       getline(cin, str);       cout

How to traverse a C++ set in reverse direction?

Arnab Chakraborty
Updated on 17-Dec-2019 13:19:02

1K+ Views

Suppose we have a set, then we have to traverse the set in reverse direction. So if the set is like S = [10, 15, 26, 30, 35, 40, 48, 87, 98], then the output will be: 98 87 48 40 35 30 26 15 10.To traverse in reverse order, we can use the reverse_iterator. Here we will use the rbegin() and rend() function to get the begin and end of the reverse iterator.Example Live Demo#include #include using namespace std; int main() {    int arr[] = {10, 15, 26, 30, 35, 40, 48, 87, 98};    set my_set(arr, arr + sizeof(arr) / sizeof(arr[0]));    set::iterator it;    cout

Advertisements