Question: Nearest Greater Element Actions Options You are given an integer array A of size N and an index idx. Find the distance from the element

Nearest Greater Element
Actions
Options
You are given an integer array A of size N and an index idx.
Find the distance from the element at idx to the nearest element on its left that is greater than it. If there is no such element for the chosen index, return -1.
Notes:
For example, for the array (4,2,5,10,8] and idx=2, the nearest greater element on the left for the element at index 2(which is 5) is 4, and the distance is 2.
idx follows 0-based indexing.
Function description
Complete the distanceToGreaterLeft function in the editor below. It has the following parameter(s):
Name
Type
Description
N
INTEGER
The size of array A.
A
INTEGER ARRAY
The given array.
idx
INTEGER
The index of the element of interest in A.
Return
The function must return an INTEGER denoting the distance from the element at idx to its nearest greater element on the left. If no such element exists, it should return -1.
Constraints
Input format for debugging
 Nearest Greater Element Actions Options You are given an integer array

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!