Found 33676 Articles for Programming

Number of flips to make binary string alternate - Set 1 in C++

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

627 Views

Let's say you have given a binary string "10011". To make an alternate binary string, we need to flip a minimum of 2 characters as "10101".There are two possibilities for the alternate string. It will start with 0 or 1. We will check for two alternates and count the number of flips required for both.And then return the minimum of both. Let's see an example.Inputbinary = "10011"Output2 If we start the string with 0, then we have to flip 3 times. And if we start the string with 1, then we have to flip 2 times. The minimum is 2.AlgorithmInitialise ... Read More

Number of even substrings in a string of digits in C++

Hafeezul Kareem
Updated on 26-Oct-2021 10:33:29

359 Views

Given a string of digits, we need to find the count of even substrings in it. Let's see an example.Inputnum = "1234"Output6The even substrings that can be formed from the given string are2 12 4 34 234 1234AlgorithmInitialise the string with digits.Initialise the count to 0.Iterate over the string.Get the current digit by subtracting the char 0 from the current char digit.Check whether the digit is even or not.If the current digit is even, then add it's index plus 1 to the count.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getEvenSubstringsCount(char str[]) ... Read More

Number of elements smaller than root using preorder traversal of a BST in C++

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

112 Views

You are given the result of the preorder traversal. You need to find the number of elements that are smaller than the root.The first element in the preorder traversal is the root of the BST. Let's see an example.Inputpreorder_result = [5, 4, 2, 1, 7, 6, 8, 9]Output3 There are three elements that are less than the root. The root is 5.AlgorithmInitialise the preorder result in an array.Store the first element i.e.., root of the BST in a variable.Write a loop that iterates from the 2nd element of the preorder result.Compare every element with the root.If the current element is ... Read More

Number of elements less than or equal to a given number in a given subarray in C++

Hafeezul Kareem
Updated on 26-Oct-2021 07:52:26

180 Views

You are given a number and subarray lower and upper bound indexes. You need to count a number of elements that are less than or equal to the given number. Let's see an example.Inputarr = [1, 2, 3, 4, 5, 6, 7, 8] k = 4 lower = 0 upper = 5Output4There are 4 elements between the index 0 and 5 that are less than or equal to 4.AlgorithmInitialise the array, number, and subarray indexes.Initialise the count to 0.Write a loop that iterates from the lower index of the subarray to the upper index of the subarray.If the current element ... Read More

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

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

152 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

381 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

202 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

176 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

141 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

386 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

Advertisements