Programming Articles - Page 2530 of 3366

C++ Program for GCD 0.of more than two (or array) numbers?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

200 Views

Here we will see how we can get the gcd of more than two numbers. Finding gcd of two numbers are easy. When we want to find gcd of more than two numbers, we have to follow the associativity rule of gcd. For example, if we want to find gcd of {w, x, y, z}, then it will be {gcd(w, x), y, z}, then {gcd(gcd(w, x), y), z}, and finally {gcd(gcd(gcd(w, x), y), z)}. Using array it can be done very easily.Algorithmgcd(a, b)begin    if a is 0, then       return b    end if    return gcd(b ... Read More

C++ Program for Common Divisors of Two Numbers?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

329 Views

Here we will see how we can get the number of common divisors of two numbers. We are not going to find all common divisors, but we will count how many common divisors are there. If two numbers are like 12 and 24, then common divisors are 1, 2, 3, 4, 6, 12. So there are 6 common divisors, so the answer will be 6.AlgorithmcountCommonDivisor(a, b)begin    count := 0    gcd := gcd of a and b    for i := 1 to square root of gcd, do       if gcd is divisible by 0, then   ... Read More

Add n binary strings in C++?

Nishu Kumari
Updated on 29-Jul-2025 18:52:05

299 Views

We are given n binary strings, and we need to add them together and return the result (as a binary string). A binary string is a string that consists only of the characters '0' and '1', which represent binary digits. We will add them bit by bit using binary addition rules. Addition of Binary Numbers Here's how binary addition with binary numbers works: 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 1 + 1 ... Read More

Add minimum number to an array so that the sum becomes even in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

149 Views

Suppose there is an array with some numbers. We have to tell minimum how many numbers will be added with it to make the sum of the elements even. The number must be greater than 0. So if the sum of the elements is odd, we will add 1, but if the sum is already even, then we will add 2 with it to make it even.AlgorithmaddMinNumber(arr)begin    s := 0    for each element e from arr, do       s := e + s    done    if s is even, then return 2, otherwise 1 endExample Live ... Read More

Adam Number in C++

Ravi Ranjan
Updated on 11-Jul-2025 18:16:36

2K+ Views

The Adam number is a number such that the square of the given number 'n' is the reverse of the square of the reverse of that number 'n'. In this article, our task is to write a program that can check whether the given number is an Adam number or not. Here is an example to check whether 12 is an Adam number or not: Input: num = 12 Output: 12 is an Adam number Explanation: num = 12 , square of num = ... Read More

Activity Selection Problem (Greedy Algo-1) in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

2K+ Views

There are n different activities are given with their starting time and ending time. Select maximum number of activities to solve by a single person.We will use the greedy approach to find the next activity whose finish time is minimum among rest activities, and the start time is more than or equal with the finish time of the last selected activity.The complexity of this problem is O(n log n) when the list is not sorted. When the sorted list is provided the complexity will be O(n).InputA list of different activities with starting and ending times.{(5, 9), (1, 2), (3, 4), ... Read More

Absolute Difference of even and odd indexed elements in an Array (C++)?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

584 Views

Here we will see how we can get the absolute differences of odd and even indexed elements in an array. The absolute difference indicates that if the difference of one pair is negative, the absolute value will be taken. For an example, let the numbers are {1, 2, 3, 4, 5, 6, 7, 8, 9}. So the even position elements are 1, 3, 5, 7, 9 (starting from 0), and odd place elements are 2, 4, 6, 8. So the difference for even placed data are |1 - 3| = 2, then |2 - 5| = 3, |3 - 7| ... Read More

Absolute Difference of all pairwise consecutive elements in an array (C++)?

Revathi Satya Kondra
Updated on 01-Aug-2025 18:06:10

686 Views

An array in C++ is a data structure used to store multiple values of the same type in a contiguous block of memory. To know that how values shift from one index to the next, a common and practical technique is to compute the absolute difference between each pair of consecutive elements. Absolute Difference The absolute difference is the positive distance between two numbers, regardless of which one is larger. It is computed using the abs() function from the library. Absolute difference between a and b is denoted as |a - b|. For example, |7 - 3| = ... Read More

A Sum Array Puzzle in C++?

Ravi Ranjan
Updated on 11-Jun-2025 18:41:02

264 Views

In this article, we will solve a sum array puzzle, where we have an array with n elements. We have to create another array of n elements such that the 'ith' position of the second array will hold the sum of all elements of the first array except the 'ith' element. We have one constraint, we cannot use the subtraction operator in this problem. Example Here is an example of calculating the sum of array elements except for the ith element: Input: arr[] = {5, 6, 7, 8} Output: res[] = ... Read More

A Product Array Puzzle in C++?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

295 Views

Here we will see one interesting problem related to array. There is an array with n elements. We have to create another array of n elements. But the i-th position of second array will hold the product of all elements of the first array except the i-th element. And one constraint is that we cannot use the division operator in this problem.If we can use the division, operation, we can easily solve this problem, by getting the product of all elements, then divide i-th element of first array and store it into i-th place of the second array.Here we are ... Read More

Advertisements