Given an array **nums[]** of magnitude **N **which contains integers, nan task is to find a subarray specified that the** **first and nan past constituent of that subarray is neither nan maximum nor minimum constituent coming successful that subarray and people nan starting and ending scale if location is specified a subarray different people -1.

**Examples:**

**Input: **N = 7, nums = [1, 3, 2, 4, 6, 5, 7]**Output: **start = 1, extremity = 5**Explanation: **Subarray starting from nan 1st scale and ending astatine 5th scale follows nan norm that is:

minimum successful nan subarray is 2, maximum successful nan subarray is 5 so,

nums[1] = 3 and nums[5] = 5 which is neither nan minimum nor maximum of that subarray.

**Input: **N = 6, nums = [2, 3, 6, 5, 4, 1]**Output: **-1

**Approach: **To lick nan problem travel nan beneath idea:

We will usage 2 Priority Queues, 1 of **increasing type** and nan different of **decreasing type**, and astatine nan aforesaid time, we will constituent two pointers astatine nan commencement and astatine nan extremity of nan array respectively. Now, those 2 privilege queue contains nan maximum and minimum constituent of nan existent subarray formed by those 2 pointers and if nan peek() constituent of those privilege queues is not adjacent to either of nan pointed indexed array past we will people nan subarray.

For a clear understanding, spot nan beneath mentation for nan supra trial case:

Suppose for nan supra illustration wherever **N = 7 **and **nums[] = [1, 3, 2, 4, 6, 5, 7]**, we will person 2 privilege queue 1 which is expanding and nan different which is decreasing and they will contain:

- increasing: [1, 3, 2, 4, 6, 5, 7] and decreasing: [7, 4, 6, 1, 3, 2, 5] and, our 2 pointer is astatine one = 0, and j = 6.
- As nums[i] == increasing.peek() = 1, truthful we will increment i, besides region a apical constituent from the

- increasing privilege queue, aft that our 2 pointers and 2 privilege queue will be:
- increasing: [2, 3, 5, 4, 6, 7], decreasing = [7, 4, 6, 1, 3, 2, 5], one = 1, and j = 6.
- As nums[j] == decreasing.peek() = 7, truthful we will decrement j and region nan apical constituent from the

- decreasing privilege queue, aft that our pointers and privilege queues will be:
- increasing: [2, 3, 5, 4, 6, 7], decreasing: [6, 4, 5, 1, 3, 2], one = 1 and j = 5.
- Now nums[i], arsenic good arsenic nums[j], is not adjacent to immoderate of nan peek() elements from nan privilege queues
- Which intends this subarray satisfies our condition, truthful we will people it.

- If successful immoderate lawsuit one crosses j without satisfying nan information past we will people -1.

Steps to travel to lick nan problem:

- Enter each elements successful 2 privilege queues, 1 which is decreasing and nan different which is increasing.
- Now we will usage 2 pointer approach, 1 pointer will commencement from nan 0th scale and nan different from (N -1)th index.
- If nan first indexed constituent is adjacent to either of nan peek() elements of nan maximum privilege queue aliases minimum privilege queue we will increment it aliases if, nan 2nd indexed constituent is adjacent to either of nan peek() constituent of nan maximum privilege queue aliases minimum privilege queue we will decrement it.
- If nary of nan indexed constituent is adjacent to nan peek() constituent of nan maximum and minimum privilege queue, we will people those indices and people a boolean emblem to existent which signify that location beryllium specified requires subarray.
- If nan emblem is false, past we will people -1.
- To understand nan concept, usage nan beneath explanation.

Below is nan implementation of nan supra approach:

## Java

**Time Complexity: **O(N * log(N)), log(N) for nan privilege queue.**Auxiliary Space: **O(N) is utilized by nan privilege queue.

**Editor:**Naga

**Beritaja.com**at