Found 26504 Articles for Server Side Programming

Find Equal (or Middle) Point in a sorted array with duplicates in C++

Arnab Chakraborty
Updated on 18-Dec-2019 09:14:58

156 Views

Suppose we have one sorted array with n elements. The array is sorted. We have to find whether an element exists in an array from where the number of smaller element is same as the number of larger elements. If the equal point appears multiple times in the array, return the index of first occurrence. If no such point is present, then return -1. Suppose the elements are like A = [1, 1, 2, 3, 3, 3, 3, 3], then the equal point is at index 2, the element is A[2] = 2. As it has only one smaller element ... Read More

Find cost price from given selling price and profit or loss percentage in C++

Arnab Chakraborty
Updated on 18-Dec-2019 07:10:28

290 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 all possible outcomes of a given expression in C++

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

255 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

161 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

299 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

106 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

484 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

466 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

123 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

400 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

Advertisements