Server Side Programming Articles - Page 2149 of 2650

Check if a given array is pairwise sorted or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 09:04:02

181 Views

We have an array A, with n elements. We have to check whether the array is pairwise sorted or not. Suppose the array is like {8, 10, 18, 20, 5, 15}. This is pairwise sorted as (8, 10), (18, 20), (5, 15) are sorted. If the array has an odd number of elements, then the last one will be ignored.The approach is too simple, by taking I from 0 to n-1, we will see if the ith element is less than the i+1th element or not, if not, then return false, otherwise increase I by 2.Example Live Demo#include #include ... Read More

Check if a circle lies inside another circle or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 09:01:17

623 Views

Suppose we have two circles (center points, and the radius values), we have to check one circle is fit inside another circle or not. There are three possible causes.The smaller circle lies completely inside the bigger one, without touching each other. In this case, the sum of the distance between the centers, and the smaller radius, is lesser than a bigger radius. So the smaller one will be inside the bigger one.The second case is the smaller circle is inside the bigger ones, but also touches the circumference of the bigger circle.The third case is, some part of the smaller ... Read More

Check if a binary string contains consecutive same or not in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:58:08

323 Views

Suppose we have a binary string. Our task is to check whether the string has consecutive same characters or not. If there are consecutive same characters, then that is invalid, otherwise valid. Then the string “101010” is valid, but “10111010” is invalid.To solve this problem, we will traverse from left to right, if two consecutive characters are the same, then return false, otherwise true.Example Live Demo#include #include using namespace std; bool isConsecutiveSame(string str){    int len = str.length();    for(int i = 0; i

Check if a + b = c is valid after removing all zeroes from a, b and c in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:53:27

117 Views

Suppose we have three numbers a, b, c, we have to check whether a + b = c, after removing all 0s from the numbers or not. Suppose the numbers are a = 102, b = 130, c = 2005, then after removing 0s, the numbers will be a + b = c : (12 + 13 = 25) this is trueWe will remove all 0s from a number, then we will check after removing 0s, a + b = c or not.Example Live Demo#include #include using namespace std; int deleteZeros(int n) {    int res = 0;   ... Read More

Check if a number is sandwiched between primes in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:49:00

102 Views

Here we will see whether a number is sandwiched between primes or not. A number is said to be sandwiched between primes when the number just after it, and just below it is prime numbers. To solve this, check whether n-1 and n+1 are prime or not.Example Live Demo#include #include #define N 100005 using namespace std; bool isPrime(int n) {    if (n == 0 || n == 1)       return false;    for (int i=2;i

Check if a number is magic (Recursive sum of digits is 1) in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:46:25

2K+ Views

Here we will see one program, that can check whether a number is magic number or not. A number is said to be magic number, when the recursive sum of the digits is 1. Suppose a number is like 50311 = 5 + 0 + 3 + 1 + 1 = 10 = 1 + 0 = 1, this is magic number.To check whether a number is magic or not, we have to add the digits until a single-digit number is reached.Example Live Demo#include using namespace std; int isMagicNumber(int n) {    int digit_sum = 0;    while (n > ... Read More

Check for integer overflow on multiplication in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:25:08

2K+ Views

Suppose we want to find the result after multiplying two numbers A and B. We have to check whether the multiplied value will exceed the 64-bit integer or not. If we multiply 100, and 200, it will not exceed, if we multiply 10000000000 and -10000000000, it will overflow.To check this, we have to follow some steps. These are like below −Steps −If anyone of the numbers is 0, then it will not exceedOtherwise, if the product of two divided by one equals to the other, then it will not exceedFor some other cases, it will exceed.Example Live Demo#include #include ... Read More

Find two numbers whose sum and GCD are given in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:21:57

230 Views

We have the sum and gcd of two numbers a and b. We have to find both numbers a and b. If that is not possible, return -1. Suppose the sum is 6 and gcd is 2, then the numbers are 4 and 2.The approach is like, as the GCD is given, then it is known that the numbers will be multiples of it. Now there following stepsIf we choose the first number as GCD, then the second one will be sum − GCDIf the sum of the numbers is chosen in the previous step is the same as the ... Read More

Find the resulting Colour Combination in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:16:57

339 Views

We have a string with three colors (G, B, Y). We have to find the resulting color based on these relations −B * G = YY * B = GG * Y = BSuppose the string is “GBYGB” is B. If the string is “BYB”, then it will be Y.The approach is simple; we will take the string. Compare each alphabet with adjacent characters, using the given condition, find the color.Example Live Demo#include using namespace std; char combination(string s) {    char color = s[0];    for (int i = 1; i < s.length(); i++) {       ... Read More

Find the other number when LCM and HCF given in C++

Arnab Chakraborty
Updated on 21-Oct-2019 08:09:36

117 Views

Suppose we have a number A, and LCM and GCD values, we have to find another number B. If A = 5, LCM is 25, HCF = 4, then another number will be 4. We know that −$$𝐴∗𝐵=𝐿𝐶𝑀∗𝐻𝐶𝐹$$$$𝐵= \frac{LCM*HCF}{A}$$Example Live Demo#include using namespace std; int anotherNumber(int A, int LCM, int GCD) {    return (LCM * GCD) / A; } int main() {    int A = 5, LCM = 25, GCD = 4;    cout

Advertisements