Found 7347 Articles for C++

Number of leading zeros in binary representation of a given number in C++

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

466 Views

Given a number, we have to find the number of leading zeroes in the binary representation of it. Assuming total bits to be 32. Let's see an example.Input5Output25The binary representation of 5 is 00000...00101. The number of leading zeroes are 29.AlgorithmInitialise the number n.Find the binary representation of n.Subtract the length of binary representation of n from the total number of bits i.e.., 32.Return the result.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getLeadingZeroesCount(unsigned int n) {    int totalBits = sizeof(n) * 8;    string binary = "";    while (n) { ... Read More

Number of Larger Elements on right side in a string in C++

Hafeezul Kareem
Updated on 26-Oct-2021 14:45:24

121 Views

Given a string, we have to count the number of larger elements on right side of each character. Let's see an example.Inputstring = "abc"Output2 1 0There are 2 larger elements than a on its right side.There is 1 larger element than b on its right side.There are 0 larger elements than c on its right side.AlgorithmInitialise the string.Initialise an array to keep track of the count.Write two loops to iterate over the string.Take one char at a time and compare it with all the character after it.Increment the corresponding character count in the count array if the current element is ... Read More

Number of integral solutions of the equation x1 + x2 +…. + xN = k in C++

Hafeezul Kareem
Updated on 26-Oct-2021 14:35:40

302 Views

The solutions for the equation areThe number of non-negative integral solutions of the equation are $\left(\begin{array}{c}n-k+1\ k\end{array}\right)$The number of positive integral solutions of the equation are $\left(\begin{array}{c}k-1\ n-1\end{array}\right)$Add both to get the required answer. Let's see an example.Inputn = 4 k = 7Output140 AlgorithmInitialise the numbers n and k.Find the integral solutions of not-negative and positive numbers.Add both of them.Return the answer.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int factorial(int n) {    int product = 1;    for (int i = 2; i Read More

Number of Integral Points between Two Points in C++

Hafeezul Kareem
Updated on 26-Oct-2021 13:40:00

425 Views

In this tutorial, we are going to write a program the finds the number of integral points between the given two points.The number of points between two given points will be gcd(abs(x2), abs(y1-y2)) - 1.If the line joining is parallel to x-axis, then the number of integral points will be abs(y1 - y2) - 1.If the line joining is parallel to y-axis, then the number of integral points will be abs(x1 - x2) - 1If the x points of both points are equal, then they are parallel to the x-axis. If the y points of both points are equal, then ... Read More

Number of integers with odd number of set bits in C++

Hafeezul Kareem
Updated on 26-Oct-2021 13:29:41

300 Views

Given a number n, we have to find the number of integers with an odd number of set bits in their binary form. Let's see an example.Inputn = 10Output5There are 5 integers from 1 to 10 with odd number of set bits in their binary form.AlgorithmInitialise the number N.Write a function to count the number of set bits in binary form.Initialise the count to 0.Write a loop that iterates from 1 to N.Count the set bits of each integer.Increment the count if the set bits count is odd.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include ... Read More

Number of indexes with equal elements in given range in C++

Hafeezul Kareem
Updated on 26-Oct-2021 13:19:43

131 Views

You are given an array, and indexes range. You need to count the total number of adjacent elements that are equal in the given range.Let's see an example.Inputarr = [1, 2, 2, 2, 3, 3, 4] lower = 1 upper = 5Output3AlgorithmInitialise the array and indexes range.Write a loop that iterates from the lower index of the range to upper index of the range.Compare the element the previous or next element.Increment the count if they are equal.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getEqualElementsCount(int arr[], int n, int lower, int ... Read More

Number of Groups of Sizes Two Or Three Divisible By 3 in C++

Hafeezul Kareem
Updated on 26-Oct-2021 12:25:13

257 Views

Given an array of numbers, we need to find the number of groups of size 2 and 3 that are divisible by 3. We can get the sums of two and three combination numbers and check whether they are divisible by 3 or not.Let's see an example.Inputarr = [1, 2, 3, 4]Output4 There are 4 combinations that are divisible by 3. The combinations are...[1, 2] [2, 4] [1, 2, 3] [2, 3, 4]AlgorithmInitialise the array.Write two loops to get all combinations of size two.Compute the sum of each group.If the sum is divisible by 3, then increment the count.Write three ... Read More

Number of groups of magnets formed from N magnets in C++

Hafeezul Kareem
Updated on 26-Oct-2021 12:12:09

663 Views

The digit 1 represent positive pole whereas 0 represents negative pole.The magnet will have both poles as 10 or 01. A group can be formed with the magnets that attracts each other. The magnets with different pole facing each other will be in the same group.Here, you are given N number of magnets. You need to find out number of groups can be formed with them.Whenever there are two different magnets side by side, there forms a new group. In that case increment the count of the groups.Let's see an example.Inputmagnets = ["10", "01", "01", "01", "10", "01"]Output4 There are ... Read More

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

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

564 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

293 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

Advertisements