Number of Digits in 2 Raised to Power n in C++

Hafeezul Kareem
Updated on 26-Oct-2021 06:38:43

145 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 with Positive Integers Sum k in C++

Hafeezul Kareem
Updated on 26-Oct-2021 06:33:42

399 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
Updated on 26-Oct-2021 06:03:10

251 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

Check If a Number is Divisible by 29 in C++

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

145 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 as Linked Lists in C++

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

293 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 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

Find Coefficients of Linear Equations with One Solution in Python

Arnab Chakraborty
Updated on 25-Oct-2021 08:11:57

516 Views

Suppose we have a value n, we have to find the number of pairs (a, b) [a < b], that exist such that the equation a*x + b*y = n, has at least one solution.So, if the input is like n = 4, then the output will be 2 because the valid pairs are (1, 2) and (1, 3).To solve this, we will follow these steps −Define a function divisors_gen() . This will take ndivs := a list of lists of size n+1. And each inner list is holding 1divs[0] := a list with only one element 0for i in ... Read More

Find Number M with N Zeros at End in Python

Arnab Chakraborty
Updated on 25-Oct-2021 08:08:24

134 Views

Suppose we have a number n. We have to find smallest number m, such that factorial of m has at least n number of 0s.So, if the input is like n = 2, then the output will be 10 because 10! = 3628800 and 9! = 362880, minimum number with 2 zeros is 10.To solve this, we will follow these steps −Define a function count_fives() . This will take ncnt := 0while n > 0, don := floor of (n / 5)cnt := cnt + nreturn cntFrom the main method, do the following −left := 1right := 5^24while right - ... Read More

Find How Many Children Will Get Candies in Python

Arnab Chakraborty
Updated on 25-Oct-2021 08:05:08

329 Views

Suppose we have k number of candies. We have to distribute them among children. Now there are some rulesith child will get i^2 number of candiesany children at index i will not get any candy until all children from index 1 to i-i are servedIf ith children does not get i^2 number of candies, then that is not a valid serve.So, if the input is like k = 20, then the output will be 3 because, first one will get 1, second one will get 2^2 = 4, third one will get 3^2 = 9, but fourth one needs 4^2 ... Read More

Advertisements