Hafeezul Kareem

Hafeezul Kareem

259 Articles Published

Articles by Hafeezul Kareem

Page 19 of 26

Number of digits in 2 raised to power n in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 26-Oct-2021 186 Views

The power of a number can be computed using the iterative multiplication or function that the language provides. It's a straightforward thing.Here, we have to find the 2 raised to power n. And the number of digits in the result. Let's see some examples.Input5Output2Input10Output4 AlgorithmInitialise the number n.Find the value of 2n.The ceil of log10(n) will give you number of digits in the number n.Find it and return it.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getDigitsCount(int n) {    return ceil(log10(pow(2, n))); } int main() {    int n = 8;   ...

Read More

Number of arrays of size N whose elements are positive integers and sum is K in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 26-Oct-2021 441 Views

We are given two numbers n and k. We need to find the count of arrays that can be formed using the n numbers whose sum is k.The number of arrays of size N with sum K is $\dbinom{k - 1}{n - 1}$.This a straightforward formula to find the number arrays that can be formed using n elements whose sum k. Let's see an example.Inputn = 1 k = 2Output1 The only array that can be formed is [2]Inputn = 2 k = 4Output3 The arrays that can be formed are [1, 3], [2, 2], [3, 1].AlgorithmInitialise the numbers n ...

Read More

Number of anomalies in an array in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 26-Oct-2021 284 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
Hafeezul Kareem
Updated on 26-Oct-2021 170 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

Read More

Multiply two polynomials in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 26-Oct-2021 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 as linked lists into a third list in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Oct-2021 340 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 any Number with using Bitwise Operator in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Oct-2021 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
Hafeezul Kareem
Updated on 25-Oct-2021 456 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
Hafeezul Kareem
Updated on 25-Oct-2021 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

Read More
Showing 181–190 of 259 articles
« Prev 1 17 18 19 20 21 26 Next »
Advertisements