Question: You are given an array arr of size n . . The array consists of only positive integers. Your task is to find the maximum

You are given an array arr of size n.. The array consists of only positive integers. Your task is to find the maximum difference from each index. Here, the Maximum difference is calculated as the absolute difference between the next greater element((if it exists)) on the right side and the maximum element on the right side.
Note:
If there is no next greater element at any index, return 0.0.
Constraints:
1<=1<= n <=100000<=100000
1<=1<= arr[[i]<=100000]<=100000
Input Format:
The first line contains a single integer n
The second line contains n space-separated Integers denoting the array arr.
Output Format:
Print the integer, i..e.,., maximum difference.
Sample Testcase #00:
Testcase Input:
44
41234123
Testcase output:
11
Explanation:
For each Index:
index 00: no greater element than 44 is present.
Index 11: next greater=2=2 and maximum=3,=3, Hence 32=1,3-2=1,
Index 22: next greater=3=3 and maximum=3.=3. Hence 330.3-3-0.
Index 33: no greater element than 33 Is present.
Hence, 11 Is printed.
Sample Testcase #11:
Testcase Input:
44
43214321
Testcase Output:
00
explaination:
For each Index:
index 00: no greater element than 44 is present.
index 11: no greater element than 33 is present.
Index 22: no greater element than 22 Is present.
Index 33: no greater element than 11 is present.
Hence, 00 is printed.
Write code in cpp..

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Here is the C code for the given problem include include include using ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!