Found 7197 Articles for C++

Find the Nth Even Length Palindrome using C++

prateek jangid
Updated on 27-Sep-2021 10:58:29

792 Views

If you ever used C + +, then you must have heard about Palindrome numbers. So in this guide, we will explain everything about "Nth even-length Palindrome" using appropriate examples. Palindrome numbers are numbers that stay the same after reversing them. Not only numbers but a word whose spelling stays the same when its characters are reversed. For Example −Numbers = {1, 121, 131, 656, 1221, 1551}Words = {saas, malayalam, level, mom}It looks complicated but very easy to perform on any system. So let's discuss the palindrome in brief.Nth Even length Palindrome Number11, 22, 33, 44, 55, 66, 77, 88, ... Read More

Number of pairs with maximum sum in C++

Aishwarya Naglot
Updated on 12-Nov-2024 10:33:30

473 Views

We need to find a number of all the pairs that has the maximum sum, In a given array of integers. A pair consists of two numbers, and the sum is simply the result of adding them. We will see multiple solutions to this problem, starting with a naive (brute force) approach and moving to more optimized solutions. Below is the list of approaches we will cover in this article: Brute Force approach using two loops Max Sum Pairs with Sorting ... Read More

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

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

231 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

909 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

187 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

188 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

223 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

368 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

642 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

203 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

Advertisements