Next Greater Element in Array

Find next greater element in array or find next greater element to right.

Given an input array, find the next greater element for every element of an array. The next greater element x is the first greater element on the right side of x in an array.

For example:

Example 1:

Input:     {4,   2,   6,   8,  1,   0}

Output:  {6,   6,   8, -1, -1,  -1}

4 => 6 (The first next greater element of 4 is 6)
2 => 6 (Next greater element of 2 is 6)
6 => 8 (Next greater element of 6 is 8)
8 => -1 (No next greater element found)
1 => -1 (No next greater element found)
0 => -1

Example 2:

Input:     {7,   8,   1,   4}

Output:  {8, -1,  4,  -1}

Program to Check Armstrong Number in Java

In this post, we are going to write a program to check armstrong number in java.

Given a number x, write a code to check whether a number x is Armstrong Number or not. Let’s first understand what is Armstrong number?

Armstrong number

An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.

For example – 153, 371 is an Armstrong number.

Example 1: 153 has 3 digits.

153 : 13 + 53 + 33 = 153

Example 2 – Take an example of 6 which has 1 digit.

6: 61 = 6 It’s an Armstrong number

Example 3 – Take another example let’s say 15, it has 2 digits.

15 : 12 + 52 = 26

So 15 is not an Armstrong number.

Example 4 – This time let’s take bigger number 1634, it has 4 digits.

1634 : 14 + 64 + 34 + 44 = 1634

So, we can say 1634 is a 4 digit armstrong number.