The Wayback Machine - https://web.archive.org/web/20210219152742/https://www.geeksforgeeks.org/largest-area-possible-after-removal-of-a-series-of-horizontal-vertical-bars/
Skip to content
Related Articles

Related Articles

Largest area possible after removal of a series of horizontal & vertical bars
  • Last Updated : 19 Feb, 2021

Given a grid consisting of horizontal & vertical bars of size (N + 2) x (M + 2) and two arrays H[] and V[] denoting the number of horizontal & vertical bars required to be removed, the task is to find the largest area when a series of vertical and horizontal bars are removed.

Examples:

Input: N = 3, M = 3, H[] = {2}, V[] = {2}
Output: 4
Explanation: 
There are 3 bars in horizontal as well as vertical direction. 
After removing bars the matrix looks like this:                          

Therefore, the largest area is 4.



Input: N = 3, M = 2, H[] = {1, 2, 3}, V[] = {1, 2}
Output: 12

Approach: Follow the steps below to solve the problem:

  • Initialize two sets, s1 & s2 to store the integers.
  • Iterate over the range [1, N+1] and store every integer in s1.
  • Similarly, iterate over the range [1, M + 1] and store every integer in s2.
  • Traverse the array H[] and remove all H[i] from s1.
  • Similarly, traverse the array V[] and remove all V[i] from s2.
  • Convert updated s1 and s2 sets into lists l1 and l2.
  • Sort both the lists in ascending order.
  • Traverse the list l1 and l2 and store the maximum distance between two neighbours as maxH and maxV respectively.
  • Print the product of maxH and maxV as the largest area.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
  
import java.lang.*;
import java.util.*;
  
class GFG {
  
    // Function to find the largest area
    // when a series of horizontal &
    // vertical bars are removed
    static void largestArea(int N, int M,
                            int[] H, int[] V)
    {
        // Stores all bars
        Set<Integer> s1 = new HashSet<>();
        Set<Integer> s2 = new HashSet<>();
  
        // Insert horizontal bars
        for (int i = 1; i <= N + 1; i++)
            s1.add(i);
  
        // Insert vertictal bars
        for (int i = 1; i <= M + 1; i++)
            s2.add(i);
  
        // Remove horizontal separators from s1
        for (int i = 0; i < H.length; i++) {
  
            s1.remove(H[i]);
        }
  
        // Remove vertical separators from s2
        for (int i = 0; i < V.length; i++) {
  
            s2.remove(V[i]);
        }
  
        // Stores left out horizontal and
        // vertical separators
        int[] list1 = new int[s1.size()];
        int[] list2 = new int[s2.size()];
  
        int i = 0;
        Iterator it1 = s1.iterator();
        while (it1.hasNext()) {
            list1[i++] = (int)it1.next();
        }
  
        i = 0;
        Iterator it2 = s2.iterator();
        while (it2.hasNext()) {
            list2[i++] = (int)it2.next();
        }
  
        // Sort both list in
        // ascending order
        Arrays.sort(list1);
        Arrays.sort(list2);
  
        int maxH = 0, p1 = 0, maxV = 0, p2 = 0;
  
        // Find maximum difference of neighbors of list1
        for (int j = 0; j < list1.length; j++) {
            maxH = Math.max(maxH, list1[j] - p1);
            p1 = list1[j];
        }
  
        // Find max difference of neighbors of list2
        for (int j = 0; j < list2.length; j++) {
            maxV = Math.max(maxV, list2[j] - p2);
            p2 = list2[j];
        }
  
        // Print largest volume
        System.out.println(maxV * maxH);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Given value of N & M
        int N = 3, M = 3;
  
        // Given arrays
        int[] H = { 2 };
        int[] V = { 2 };
  
        // Function call to find the largest
        // area when a series of horizontal &
        // vertical bars are removed
        largestArea(N, M, H, V);
    }
}

chevron_right


Output:

4

Time Complexity: max(N, M) * (log(max(N, M)))
Auxiliary Space: O(max(N, M))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :