Trends

Find Subarray whose end points don’t have maximum or minimum

Trending 1 year ago
beritaja.com

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

// Java algorithm of nan supra approach import java.util.*; class GFG { // Driver's Code nationalist fixed void main(String[] args) { int N = 7; int[] nums = { 1, 3, 2, 4, 6, 5, 7 }; findSubArray(nums, N); } // Function to find nan subarray nationalist fixed void findSubArray(int[] nums, int N) { // Initializing decreasing // privilege queue PriorityQueue<Integer> decreasing = caller PriorityQueue<>( Collections.reverseOrder()); // Initializing increasing // privilege queue PriorityQueue<Integer> increasing = caller PriorityQueue<>(); // Adding each elements to both // of nan privilege queue for (int v : nums) { decreasing.add(v); increasing.add(v); } // Initializing 2 pointers 1 at // starting different astatine nan ending int one = 0; int j = N - 1; // Boolean emblem which ensures // whether we sewage nan subarray aliases not boolean emblem = true; while (i <= j && flag) { // Checking if starting element // of subarray is maximum aliases not if (nums[i] == decreasing.peek()) { i++; decreasing.remove(); } // Checking if starting element // of subarray is minimum aliases not other if (nums[i] == increasing.peek()) { i++; increasing.remove(); } // Checking if past constituent of // subarray is maximum aliases not other if (nums[j] == decreasing.peek()) { j--; decreasing.remove(); } // Checking if past constituent of // subarray is minimum aliases not other if (nums[j] == increasing.peek()) { j--; increasing.remove(); } // If nary of nan supra lawsuit is // applied past we tin opportunity for // judge that nan existent subarray // satisfies nan required condition else emblem = false; } // If emblem is existent means, we are // not capable to find nan subarray // and will people -1. if (flag) System.out.println(-1); // Else we will people nan starting // and nan ending scale of nan subarray else System.out.println("start = " + one + ", " + "end = " + j); } }

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

Editor: Naga



Read other contents from Beritaja.com at
More Source
close