Programming Articles - Page 1165 of 3363

Number of elements greater than modified mean in matrix in C++

Hafeezul Kareem
Updated on 26-Oct-2021 07:20:06

175 Views

The modified mean of the matrix is defined as follows...(sum(row-wise min) + sum(column-wise max)) / (row_size + column_size)Let's see an example.1 2 3 4 5 6 7 8 9mean = (sum(1 + 4 + 7) + sum(7 + 8 + 9)) / (3 + 3)We have to find the mean first and then count the number of elements that are greater than mean.If we take the above example, then we will get 3 as the count. There are 3 elements that are greater than the mean which is 6.AlgorithmInitialise the matrix.Find the row-wise minimum elements sum.Find the column-wise maximum elements ... Read More

Number of digits to be removed to make a number divisible by 3 in C++

Hafeezul Kareem
Updated on 26-Oct-2021 07:11:56

403 Views

You are given a number in string. You need to find how many digits need to be removed to make it divisible by 3.We make a number divisible by removing at most 2 digits. So, the maximum number of digits to be removed to make it divisible by 3 is 2.Let's see some examples.Input92Output1We can remove 2 to make it divisible by 3.Input999Output0The given number itself is divisible by 3.AlgorithmInitialise the number in string.Find the sum of the number.If the sum is divisible by 3, then return 0.If the sum is not divisible by 3 and the length of the ... Read More

Number of digits in the nth number made of given four digits in C++

Hafeezul Kareem
Updated on 26-Oct-2021 07:00:47

225 Views

We need to find the number of digits in the nth number made of given four digits 1, 2, 3, and 4.The series with the above four digits is as follows1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24...We need to find the number of digits of the nth number from the above series. If you carefully observe the pattern, you will find the following points.There are 4 numbers with digits 1.There are 16 numbers with digits 2.The pattern continues as the powers of 4.Let's see an exampleInput7Output2 The 7th number in the series is 13 and ... Read More

Number of Digits in a^b in C++

Hafeezul Kareem
Updated on 26-Oct-2021 06:53:53

201 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 a raised to power b. And the number of digits in the result. Let's see some examples.Inputa = 5 b = 2Output2 Inputa = 7 b = 6Output6 AlgorithmInitialise the number a and b.Find the value of ab.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 a, int b) ... Read More

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

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

158 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
Updated on 26-Oct-2021 06:33:42

414 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

266 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

154 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

Advertisements