# 2454. Next Greater Element IV | LeetCode Solution

## 2454. Next Greater Element IV

You are given a 0-indexed array of non-negative integers `nums`. For each integer in `nums`, you must find its respective second greater integer.

The second greater integer of `nums[i]` is `nums[j]` such that:

• `j > i`
• `nums[j] > nums[i]`
• There exists exactly one index `k` such that `nums[k] > nums[i]` and `i < k < j`.

If there is no such `nums[j]`, the second greater integer is considered to be `-1`.

• For example, in the array `[1, 2, 4, 3]`, the second greater integer of `1` is `4``2` is `3`, and that of `3` and `4` is `-1`.

Return an integer array `answer`, where `answer[i]` is the second greater integer of `nums[i]`.

Example 1:

```Input: nums = [2,4,0,9,6]
Output: [9,6,6,-1,-1]
Explanation:
0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.
1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.
2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.
3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.
4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.
Thus, we return [9,6,6,-1,-1].
```

Example 2:

```Input: nums = [3,3]
Output: [-1,-1]
Explanation:
We return [-1,-1] since neither integer has any integer greater than it.
```

Constraints:

• `1 <= nums.length <= 105`
• `0 <= nums[i] <= 109`

1. CREATE DATABASE trail;
Use trail;
Delimiter &&

-- PL/SQL Program to Find Factorial of a Number
Create Procedure fund()
Begin
Declare num int default 10;
Declare c_num int default num;
Declare factorial int default 1;
factLoop: Loop
set factorial = factorial * num;
set num = num - 1;
if num = 1 then
leave factLoop;
end if;
End Loop factLoop;
Select c_num as factotial_num,factorial as factorial;
End &&

-- PL/SQL Program to Print Table of a Number
Create Procedure fune()
Begin
Declare num int default 9;
Declare mul int default 1;
Create table multiplication_table(
multiplicand int,
multiplier int,
result int
);
mulLoop: Loop
Insert into multiplication_table value (num,mul,num*mul);
if mul = 10 then
leave mulLoop;
end if;
set mul = mul + 1;
End Loop mulLoop;
Select * from multiplication_table;
End &&

-- PL/SQL Program for Reverse of a Number
Create Procedure funf()
Begin
Declare num int default 12345678;
Declare cp_num int default num;
Declare rev_num int default 0;
revLoop: While
num >0 Do
set rev_num = rev_num * 10 + num mod 10;
set num = num div 10;
End While revLoop;
Select cp_num as original_number,rev_num as reversed_number;
End &&

-- PL/SQL Program to Check Number is Odd or Even
Create Procedure funh()
Begin
Declare a int default 8;
if a mod 2 = 1 then
Select a as num ,"Odd" as output;
else
Select a as num ,"Even" as output;
end if;
End &&

-- PL/SQL Program to Reverse a String
Create Procedure funi()
Begin
Declare message varchar(20) default "Hello World";
Select message as Original_string, Reverse(message) as Reversed_string;
End &&

-- PL/SQL Program for Palindrome Number
Create Procedure funuj()
Begin
Declare num,cp_num int default 1234321;
Declare rev_num int default 0;
revLoop: While
cp_num > 0 Do
set rev_num = rev_num * 10 + cp_num mod 10;
set cp_num = cp_num div 10;
End While revLoop;
if rev_num = num then
Select num as num, "Palindrome" as result;
else
Select num as num, "Not Palindrome" as result;
end if;
End &&

-- PL/SQL Program to Swap two Numbers
Create Procedure funk()
Begin
Declare a int default 6;
Declare b int default 12;
Declare temp int default 1;
Select a as num1,b as num2;
set temp = a;
set a = b;
set b = temp;
Select a as num1,b as num2;
End &&

-- PL/SQL Program for Armstrong Number
Create Procedure funl()
Begin
Declare num,cp_num int default 9736;
Declare len int default length(num);
Declare armNum int default 0;
numLoop: While
cp_num > 0 Do
set armNum = armNum + pow(cp_num mod 10,len);
set cp_num = cp_num div 10;
End While numLoop;
if armNum = num then
Select num as num,"Armstrong" as result;
else
Select num as num,"Not Armstrong" as result;
end if;
End &&

-- PL/SQL Program to Find Greatest of Three Numbers
Create Procedure funm()
Begin
Declare a int default 7;
Declare b int default 77;
Declare c int default 777;
Select Greatest(a,b,c) as Max_Of_three;
End &&

Delimiter ;
-- call all functions from 1 to 13 for output
Drop Database trail;

If you have any doubts/suggestion/any query or want to improve this article, you can comment down below and let me know. Will reply to you soon.