Programming Articles

Page 661 of 2544

Find the resulting Colour Combination in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 374 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#include using namespace std; char combination(string s) {    char color = s[0];    for (int i = 1; i < s.length(); i++) {       if (color ...

Read More

C Program for Basic Euclidean algorithms?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 336 Views

Here we will see the Euclidean algorithm to find the GCD of two numbers. The GCD (Greatest Common Divisor) can easily be found using Euclidean algorithm. There are two different approach. One is iterative, another one is recursive. Here we are going to use the recursive Euclidean algorithm.AlgorithmEuclideanAlgorithm(a, b)begin    if a is 0, then       return b    end if    return gcd(b mod a, a) endExample#include using namespace std; int euclideanAlgorithm(int a, int b) {    if (a == 0)       return b;    return euclideanAlgorithm(b%a, a); } main() {    int a, b;    cout > a >> b;    cout

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 257 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

Check for integer overflow on multiplication in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 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#include #include using ...

Read More

C Program for Egg Dropping Puzzle - DP-11?

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 407 Views

This is a famous puzzle problem. Suppose there is a building with n floors, if we have m eggs, then how can we find the minimum number of drops needed to find a floor from which it is safe to drop an egg without breaking it.There some important points to remember −When an egg does not break from a given floor, then it will not break for any lower floor also.If an egg breaks from a given floor, then it will break for all upper floors.When an egg breaks, it must be discarded, otherwise we can use it again.Input - The ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 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#include using namespace std; int isMagicNumber(int n) {    int digit_sum = 0;    while (n > 0 ...

Read More

C++ program for hashing with chaining

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 2K+ Views

Hashing is the method by which we can map any length data element to a fixed size key. hashing works as key-value pairs.Hashing function is the function that does the mapping in a hash map. the data elements that are given as input to the Hash Function may get same hash key. In this case the elements may overlap. To avoid overlapping of elements which have the same hash key the concept of chaining was introduced.Creating a hashmapIn order to create a hashmap we need hashing function that will define the index value of the data element.We have a hash ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 121 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#include #include #define N 100005 using namespace std; bool isPrime(int n) {    if (n == 0 || n == 1)       return false;    for (int i=2;i

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 132 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#include #include using namespace std; int deleteZeros(int n) {    int res = 0;    int ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 362 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#include #include using namespace std; bool isConsecutiveSame(string str){    int len = str.length();    for(int i = 0; i

Read More
Showing 6601–6610 of 25,433 articles
« Prev 1 659 660 661 662 663 2544 Next »
Advertisements