Found 7347 Articles for C++

Number of pairs with Bitwise OR as Odd number in C++

Hafeezul Kareem
Updated on 26-Oct-2021 19:18:54

160 Views

Given an array, we have to find the number of pairs whose Bitwise OR is an odd number. Let's see the example.Inputarr = [1, 2]Output1 There is only one pair whose Bitwise OR is an odd number. And the pair is (1, 2).AlgorithmInitialise the array with random numbers.Initialise the count to 0.Write two loops to get the pairs of the array.Compute the bitwise OR between every pair.Increment the count if the result is an odd number.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getOddPairsCount(int arr[], int n) {    int count ... Read More

Number of pairs whose sum is a power of 2 in C++

Hafeezul Kareem
Updated on 26-Oct-2021 19:09:44

797 Views

Given an array, we have to find the number of pairs whose sum is a power of 2. Let's see the example.Inputarr = [1, 2, 3]Output1 There is only one pair whose sum is a power of 2. And the pair is (1, 3).AlgorithmInitialise array with random numbers.Initialise the count to 0.Write two loops to get all the pairs of the array.Compute the sum of every pair.Check whether the sum is a power of 2 or not using bitwise AND.Increment the count if the count is a power of 2.Return the count.ImplementationFollowing is the implementation of the above algorithm in ... Read More

Number of pairs from the first N natural numbers whose sum is divisible by K in C++

Hafeezul Kareem
Updated on 26-Oct-2021 18:56:50

132 Views

Given numbers N and K, we have to count the number of pairs whose sum is divisible by K. Let's see an example.InputN = 3 K = 2Output1 There is only one pair whose sum is divisible by K. And the pair is (1, 3).AlgorithmInitialise the N and K.Generate the natural numbers till N and store them in an array.Initialise the count to 0.Write two loops to get all pairs from the array.Compute the sum of every pair.If the pair sum is divisible by K, then increment the count.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include ... Read More

Number of ordered points pair satisfying line equation in C++

Hafeezul Kareem
Updated on 26-Oct-2021 18:42:03

115 Views

The line equation that should be satisfied is y = mx + c. Given an array, m, and c, we have to find the number of order points satisfying the line equation. Let's see an example.Inputarr = [1, 2, 3] m = 1 c = 1Output2The pairs satisfying the line equation are2 1 3 2AlgorithmInitialise the array, m, and c.Write two loops to get all pairs from the array.Check whether the pair is satisfying the line equation or not.We can check the whether the equation is satisfied or not by substituting the values into the line equation.If the pair satisfies ... Read More

Number of non-negative integral solutions of sum equation in C++

Hafeezul Kareem
Updated on 26-Oct-2021 18:32:38

157 Views

In this tutorial, we are going to write a program that finds the number non-negative integral solution of sum equation.The sum equation is x + y + z = n. You are given the number n, you need to find the number of solutions for the equation. Let's see an example.Input2Output6Solutions are0 0 2 0 1 1 0 2 0 1 0 1 1 1 0 2 0 0AlgorithmInitialise the number m.Initialise the count to 0.Write three nested loops to get all the combinations of three numbers.Check the validation of the equation.If the current numbers satisfies the equation, then increment ... Read More

Number of nodes greater than a given value in n-ary tree in C++

Hafeezul Kareem
Updated on 26-Oct-2021 18:25:17

291 Views

Given an n-ary tree and a number, we have to count the number of nodes greater than the given number. Let's see an example.Inputtree = [[4], [1, 2], [3, 5]] n = 2Output3There are 3 nodes with values that are greater than n.AlgorithmInitialise the n-ary tree.Initialise the count to 0.Increment the count by 1 when you find a node whose value is greater than n.Get the children of the current node.Iterate over all the children and recursively call the same function to count nodes.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; struct ... Read More

Number of NGEs to the right in C++

Hafeezul Kareem
Updated on 26-Oct-2021 15:27:07

223 Views

You are given an array and the index of the target element. We have to count the number of elements greater than the given element to its right. Let's see an example.Inputarr = [2, 3, 5, 1, 4, 2, 6] index = 3Output3The target index element is 1. There are three elements i.e..., 4, 2, 6 that are greater than 1 on its right side.AlgorithmInitialise the array and index of target element.If the index is greater than or equal to the length of the array, then return -1.Write a loop that iterates from the next element of the given index.Increment ... Read More

Number of n digit stepping numbers in C++

Hafeezul Kareem
Updated on 26-Oct-2021 15:21:46

124 Views

The stepping number is a number where the difference between the consecutive digits is 1. You are given a number n representing the number of digits. You need to count the total number of stepping numbers with n digits.Let's see an example.Input2Output17The lowest number of 2 digits is 10 and highest number of 2 digits is 99. There are 17 stepping numbers in between them.AlgorithmInitialise the number n.Initialise the count to 0.Find the lowest number of n digits i.e.., pow(10, n - 1).Find the highest number of n digits i.e.., pow(10, n) - 1.Write a loop that iterates from the ... Read More

Number of moves required to guess a permutation in C++

Hafeezul Kareem
Updated on 26-Oct-2021 15:14:20

94 Views

Given a number N, we need to find the moves required to guess the permutation in the worst-case scenario. The number of moves required to guess the permutation will be n!. Let's take an example.Input5Output129When we have 5 elements, then we have 5 ways to guess, and 4 ways when we have 4 elements and it continuous till 1.AlgorithmInitialise the number n.Initialise count to 1.Write a loop that iterates from 1 to n.Multiply count with the current number.Return the count.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNumberMoves(int n) {    int count ... Read More

Number of leaf nodes in the subtree of every node of an n-ary tree in C++

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

329 Views

In this tutorial, we are going to write a program that finds the number of leaf nodes for every node in the n-ary tree.Given a n-ary tree, we have to find the number of leaf nodes for every subtree. Let's see an example.InputN = 8 tree = [[2, 3], [], [4, 5, 6], [7, 8], [], [], [], []]Output1->5 2->1 3->4 4->2 5->1 6->1 7->1 8->1AlgorithmInitialise the n-ary tree with tree you like.Use the DFS to traverse through the tree.Maintain an array to store the count of each node leaf nodes count.Increment the count of leaf node after the recursive ... Read More

Advertisements