Found 7347 Articles for C++

Next higher number using atmost one swap operation in C++

Hafeezul Kareem
Updated on 25-Oct-2021 03:51:21

77 Views

Given a number n, swap any two digits of the number so that the resulting number is greater than the number n. If it's not possible then print -1. Let's see an example.Input12345Output12354We have swapped the digits 4 and 5. And we got the higher number with one swap.AlgorithmIt's not possible to form the number if the digits of the number are in decreasing order.Find the index of the digit from the right of the number which is less than the last digit.Find the index of the digit which is greater than the previous digit and less than all digits.Swap ... Read More

Next greater Number than N with the same quantity of digits A and B in C++

Hafeezul Kareem
Updated on 25-Oct-2021 03:46:00

80 Views

Given N, A, and B. Find the number which is greater than N with the same number of A and B digits. Let's see an example.N = 1234 A = 2 B = 3We need to check for every possibility of the given number of digits. There are two digits to form the number. And each digit count in the number should be the same.AlgorithmInitialise A, B, and N.Write a recursive function.Check whether the current number is greater than N and has equal number of A and B digits.Return the number if the above condition satisfies.Add the digit A to the result.Add the digit B ... Read More

Next greater integer having one more number of set bits in C++

Hafeezul Kareem
Updated on 25-Oct-2021 03:38:03

95 Views

We are given a number n, we have to find the number that is greater than n with one more set bit than n in its binary representation.The digit 1 in the binary representation is called set bit.Let's see an example.Input124Output125AlgorithmInitialise the number n.Write a function get the count of number of set bits.Initialise the iterative variable with n + 1.Write an infinite loop.Check for the number of set bits for numbers greater than n.Return the number when you find it.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getSetBitsCount(int n) {    int count ... Read More

Next Greater Element in C++

Hafeezul Kareem
Updated on 23-Oct-2021 18:16:34

671 Views

The next greater element is the element that is first greater element after it. Let's see an example.arr = [4, 5, 3, 2, 1]The next greater element for 4 is 5 and the next greater element for elements 3, 2, 1 is -1 as there is no greater element after them.AlgorithmInitialise the array with random numbers.Initialise a stack.Add first element to the stack.Iterate through the element of the array.If the stack is empty, add the current element to the stack.While the current element is greater than the top element of the stack.Print the top element with the next greater element ... Read More

Next greater element in same order as input in C++

Hafeezul Kareem
Updated on 23-Oct-2021 17:54:39

63 Views

The next greater element is the element that is first greater element after it. Let's see an example.arr = [4, 5, 3, 2, 1]The next greater element for 4 is 5 and the next greater element for elements 3, 2, 1 is -1 as there is no greater element after them.AlgorithmInitialise the array with random numbers.Initialise a stack and an array.Iterate from the end of the array.Remove the elements from the stack until it empty and the top element is less than or equal to the current element.If the stack is empty, then there is no next greater element. So ... Read More

Newman-Shanks-Williams prime in C++

Hafeezul Kareem
Updated on 23-Oct-2021 17:48:11

84 Views

The newman-shanks-williams prime sequence is as follows1, 1, 3, 7, 17, 41...If we generalise the sequence items, we geta0=1 a1=1 an=2*a(n-1)+a(n-2)AlgorithmInitialise the number n.Initialise the first numbers of the sequence 1 and 1.Write a loop that iterates till n.Compute the next number using the previous numbers.Update the previous two numbers.Return the last number.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthTerm(int n) {    if(n == 0 || n == 1) {       return 1;    }    int a = 1, b = 1;    for(int i = 3; i

Nesbitt’s Inequality in C++

Hafeezul Kareem
Updated on 23-Oct-2021 17:42:01

166 Views

The nesbitt's inequality is (a/(b + c)) + (b/(c + a)) + (c/(a + b))>= 1.5, a > 0, b > 0, c > 0Given three number, we need to check whether the three numbers satisfy Nesbitt's inequality or not.We can test whether three number are satisfied nesbitt's inequality or not. It's a straightforward program.AlgorithmInitialise three numbers a, b, and c.Compute the values of each part from the equation.Add them all.If the total sum is greater than or equal to 1.5 then it satisfies the Nesbitt's inequality else it is not satisfied.ImplementationFollowing is the implementation of the above algorithm in ... Read More

Neon Number in C++

Hafeezul Kareem
Updated on 23-Oct-2021 08:01:01

2K+ Views

A neon number is a number where the sum of digits of the square of the number is equal to the number. Let's take an example.n = 9square = 81sum of digits of square = 8 + 1 = 9So, the number 9 is a neon number.We need to check whether the given number is a neon number or not. If the given number is a neon number, then print Yes else print No.AlgorithmInitialise the number n.Find the square of the number.Find the sum of the digits of the squareIf the sum of digits of the square is equal to ... Read More

Nearest prime less than given number n C++

Hafeezul Kareem
Updated on 23-Oct-2021 07:54:29

1K+ Views

We are given a number n, we need to find the nearest prime number that is less than n. We can find the number easily if we start checking from the n - 1. Let's see some examples.Input10Output7AlgorithmInitialise the number n.Write a loop that iterates from n - 1 to 1Return the first prime number that you foundReturn -1 if you didn't find any prime that's less than given nImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; bool isPrime(int n) {    if (n == 2) {       return true;    } ... Read More

Nearest 1 in a binary matrix in C++

Hafeezul Kareem
Updated on 27-Oct-2021 05:34:37

169 Views

Given a binary matrix, we need to find the minimum distance from each cell to the nearest cell that contains 1.Let's see an example.Input0 0 1 1 1 0 0 0 0Output1 1 0 0 0 1 1 1 2The minimum distance is the one that is minimum from current cell row - 1 cell row + current cell column - 1 cell column.AlgorithmInitialise the matrix of desired size.Initialise another matrix of same size to store distance.Iterate over the entire matrix.If the current cell value is 1, then set the distance to 0 as distance from 1 to 1 is ... Read More

Advertisements