Found 7197 Articles for C++

Number of anomalies in an array in C++

Hafeezul Kareem
Updated on 26-Oct-2021 06:03:10

235 Views

In this tutorial, we are going to write a program that finds the number of anomalies in the given array.A number is an anomaly in the given array if the absolute difference between the number and all other numbers is greater than the given number k. Let's see an example.Inputarr = [3, 1, 5, 7] k = 1Output4The absolute difference between all numbers with others is greater than k.AlgorithmInitialise the array.Iterate over the array.Take the element and iterate over the array.Find the absolute difference between the two numbers.If no absolute difference is less than or equal to k than increment ... Read More

Number is divisible by 29 or not in C++

Hafeezul Kareem
Updated on 26-Oct-2021 06:00:45

131 Views

It's a straightforward problem. We can use the modulo (%) operator to check whether the given number is divisible by 29 or not. Let's see some examples.Input29 254Output1 0AlgorithmImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; bool isDivisibleBy29(long long n) {    return n % 29 == 0; } int main() {       cout

Multiply two polynomials in C++

Hafeezul Kareem
Updated on 26-Oct-2021 05:56:45

4K+ Views

The coefficients of each term of the polynomial are given in an array. We need to multiply the two polynomials. Let's see an example.InputA = [1, 2, 3, 4] B = [4, 3, 2, 1]Output4x6 + 11x5 + 20x4 + 30x3 + 20x2 + 11x1 + 4AlgorithmInitialise two polynomials.Create a new array with a length of two polynomials.Iterate over the two polynomials.Take one term from the first polynomial and multiply it with all the terms in the second polynomial.Store the result in the resultant polynomial.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int *multiplyTwoPolynomials(int ... Read More

Multiply two numbers represented by Linked Lists in C++

Hafeezul Kareem
Updated on 25-Oct-2021 16:28:34

1K+ Views

Given two linked lists with digits in it. We need to multiply two numbers formed by the linked list. It can be done easily by forming the numbers from the two linked lists. Let's see an example.Input1 -> 2 -> NULL 2 -> 3 -> NULLOutput2 -> 7 -> 6 -> NULLAlgorithmInitialise the two linked lists.Initialise two variables with 0 to store the two numbers.Iterate over the two linked lists.Add each digit to the respective number variable at the end.Multiply the resultant numbers and store the result in a variable.Create a new list with the result.Print the new list.ImplementationFollowing is ... Read More

Multiply two numbers represented as linked lists into a third list in C++

Hafeezul Kareem
Updated on 25-Oct-2021 17:04:14

276 Views

Given two linked lists with digits in it. We need to multiply two numbers formed by the linked list. It can be done easily by forming the numbers from the two linked lists. Let's see an example.Input1 -> 2 -> NULL 2 -> 3 -> NULLOutput2 -> 7 -> 6 -> NULLAlgorithmInitialise the two linked lists.Initialise two variables with 0 to store the two numbers.Iterate over the two linked lists.Add each digit to the respective number variable at the end.Multiply the resultant numbers and store the result in a variable.Create a new list with the result.Print the new list.ImplementationFollowing is ... Read More

Multiply Large Numbers represented as Strings in C++

Hafeezul Kareem
Updated on 25-Oct-2021 07:10:44

4K+ Views

Given two numbers in the string formats. We need to multiply them. The idea to solve the problem is to maintain a previous digit multiplication answer and carry. We can use the previous digits multiplication answer and carry to get the next set digits multiplication.Let's see an example.Input15 2Output30AlgorithmInitialise the numbers in string.Initialise a string of length number_one_length + number_two_length.Iterate over the first number from the end.Iterate over the second number from the end.Multiply two digits and add the corresponding previous row digit.Update the previous row digit.Store the carry in the previous index of the result string.Convert the char to ... Read More

Multiply any Number with using Bitwise Operator in C++

Hafeezul Kareem
Updated on 25-Oct-2021 06:31:30

11K+ Views

In this tutorial, we are going write a program that multiplies the given two numbers using bitwise operators.The left shift () is used for the division.The multiplication of two numbers x, y can be written as x * y = (x * 2) * (y / 2) if y is even else it's equal to x * y = (x * y) * (y / 2) + x.So whenever the second number becomes odd, add the first number to the result. Let's see the steps to solve the problem.AlgorithmInitialise two numbers.Write a loop that iterates till the second number becomes ... Read More

Multiply a given Integer with 3.5 in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:48:06

398 Views

To get the result of n * 3.5 we need to calculate (n * 2) + n + (n / 2). Moving the bits to left by 1 will give you n * 2 and moving the bits to right by will you n / 2. Add those to get the result.n * 3.5 = (n * 2) + n + (n / 2)You can submit different values of n to verify the above equation. Let's see some examples.Input2 7 10Output7 24 35AlgorithmInitialise the number n.Find the n * 2 using left shift bitwise operatorFind the n / 2 using ... Read More

Multiples of 3 or 7 in C++

Hafeezul Kareem
Updated on 25-Oct-2021 05:40:28

1K+ Views

Given a number n, we need to find the count of multiples of 3 or 7 till n. Let's see an example.Input100Output43There are total of 43 multiples of 3 or 7 till 100.AlgorithmInitialise the number n.Initialise the count to 0.Write a loop that iterates from 3 to n.Increment the count if the current number is divisible by 3 or 7.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getMultiplesCount(int n) {    int count = 0;    for (int i = 3; i

Advertisements