Programming Articles - Page 1168 of 3363

Next Greater Element in C++

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

1K+ 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

147 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

171 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

259 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

3K+ 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++

Aishwarya Naglot
Updated on 05-Nov-2024 15:15:19

239 Views

In this article, we will show you how to find the minimum distance from each cell in a binary matrix to the nearest cell that contains the digit 1. We will see brute force as well as optimized approaches. Problem Statement Given a binary matrix, our task is to calculate the minimum distance for each cell to the nearest cell that contains the value 1, and if the current cell itself has 1 as a value, the distance will be 0. If the input matrix is as follows - 0 0 1 1 1 0 0 0 0 The ... Read More

Natural Numbers in C++ Program

Aishwarya Naglot
Updated on 11-Nov-2024 15:30:26

4K+ Views

In this article, we will cover C++ programs to work with natural numbers. we use natural numbers in various operations, such as indexing arrays, performing loops, and validating user input. we will also write code to demonstrate how natural numbers can be used for these purposes in C++. Natural Numbers are a set of positive numbers, starting from 1 and going to infinity. natural numbers are: 1, 2, 3, 4, 5... etc In C++, understand concepts like conditions, loops, and how we store and manipulate variables. We will provide everything you need to know about natural numbers along with programs. ... Read More

N’th Smart Number in C++

Hafeezul Kareem
Updated on 23-Oct-2021 06:31:40

238 Views

A smart number is a number that contains at least three distinct prime factors. You are given a number N. Find the n-th smart number.The smart number series are30, 42, 60, 66, 70, 78...AlgorithmInitialise the number N.Initialise the count to 0.Write a function that checks whether the given number is prime or not.Write a function that checks whether the number is smart or not.Write a loop that iterates from 30 as first smart number is 30.Check whether the current number is smart number or not using the prime number function.Increment the count by 1 when you find a smart number.Return ... Read More

n’th Pentagonal Number in C++

Hafeezul Kareem
Updated on 22-Oct-2021 11:04:32

344 Views

In this tutorial, we are going to write a program that finds the n-th pentagonal number.A pentagonal number is a number represented as dots or pebbles arranged in the shape of a regular polygon. Refer to the wiki for better understanding.The n-th pentagonal number is (3 * n * n - n) / 2.The series of pentagonal numbers are 1, 5, 12, 22, 35, 51, 70, 92...AlgorithmInitialise the number n.Use the formula to find the n'th pentagonal number.Print the resultant number.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthPentagonalNumber(int n) {   ... Read More

Advertisements