Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

Here is the C code for the given problem include include include using ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Artificial Intelligence A Modern Approach

Authors: Stuart Russell, Peter Norvig

4th Edition

0134610997, 978-0134610993

More Books

Students also viewed these Programming questions

Question

How does unemployment behave over the business cycle?

Answered: 1 week ago

Question

Define induction and what are its objectives ?

Answered: 1 week ago

Question

Discuss the techniques of job analysis.

Answered: 1 week ago

Question

How do we do subnetting in IPv6?Explain with a suitable example.

Answered: 1 week ago

Question

Explain the guideline for job description.

Answered: 1 week ago

Question

What is job description ? State the uses of job description.

Answered: 1 week ago