Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find Minimum Number Of Arrows Needed To Burst All Balloons

  • Difficulty Level : Medium
  • Last Updated : 27 Oct, 2021

Given an array points[][] of size N, where points[i] represents a balloon over the area of X-coordinates from points[i][0] to points[i][1]. The Y-coordinates don’t matter. All the balloons are required to be burst. To burst a balloon, an arrow can be launched at point (x, 0) and it travels vertically upwards and bursts all the balloons which satisfy the condition points[i][0] <= x <= points[i][1]. The task is to find the minimum number of arrows required to burst all the balloons.

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: N = 4, points = {{10, 16}, {2, 8}, {1, 6}, {7, 12}}
Output: 2
Explanation: One way is to shoot one arrow for example at x = 6 (bursting the balloons [2, 8] and [1, 6]) and another arrow at x = 11 (bursting the other two balloons).



Input: N = 1, points = {{1, 6}}
Output: 1
Explanation: One single arrow can burst the balloon.

 

Approach: The given problem can be solved by using the Greedy Approach to find the balloons which are overlapping with each other so that the arrow can pass through all such balloons and burst them. To do that optimally, sort the array with respect to the X-coordinate in ascending order. So, now consider 2 balloons, if the second balloon is starting before the first balloon then it must be ending after the first balloon or at the same position.

For example [1, 6], [2, 8] -> the second balloon starting position i.e 2 which is before the ending position of the first balloon i.e 6, and since the array is sorted the end of the second balloon is always greater than the end of the first balloon. The second balloon end i.e 8 is after the end of the first balloon i.e 6. which shows us the overlapping is there between [2, 6].

Follow the steps below to solve the problem:

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
bool cmp(vector<int> a, vector<int> b)
{
    return b[1] > a[1];
}
 
// Function to find the minimum count of
// arrows required to burst all balloons
int minArrows(vector<vector<int>> points)
{
   
    // To sort our array according
    // to end position of balloons
    sort(points.begin(), points.end(), cmp);
 
    // Initialize end variable with
    // the end of first balloon
    int end = points[0][1];
 
    // Initialize arrow with 1
    int arrow = 1;
 
    // Iterate through the entire
    // arrow of points
    for (int i = 1; i < points.size(); i++)
    {
 
        // If the start of ith balloon
        // <= end than do nothing
        if (points[i][0] <= end)
        {
            continue;
        }
 
        // if start of the next balloon
        // >= end of the first balloon
        // then increment the arrow
        else
        {
 
            // Update the new end
            end = points[i][1];
            arrow++;
        }
    }
 
    // Return the total count of arrows
    return arrow;
}
 
// Driver code
int main()
{
 
    vector<vector<int>> points = {{10, 16}, {2, 8}, {1, 6}, {7, 12}};
    cout << (minArrows(points));
    return 0;
}
 
// This code is contributed by Potta Lokesh

Java




// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum count of
    // arrows required to burst all balloons
    public static int minArrows(int points[][])
    {
        // To sort our array according
        // to end position of balloons
        Arrays.sort(points,
                    (a, b) -> Integer.compare(a[1], b[1]));
 
        // Initialize end variable with
        // the end of first balloon
        int end = points[0][1];
 
        // Initialize arrow with 1
        int arrow = 1;
 
        // Iterate through the entire
        // arrow of points
        for (int i = 1; i < points.length; i++) {
 
            // If the start of ith balloon
            // <= end than do nothing
            if (points[i][0] <= end) {
                continue;
            }
 
            // if start of the next balloon
            // >= end of the first balloon
            // then increment the arrow
            else {
 
                // Update the new end
                end = points[i][1];
                arrow++;
            }
        }
 
        // Return the total count of arrows
        return arrow;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[][] points
            = { { 10, 16 }, { 2, 8 }, { 1, 6 }, { 7, 12 } };
 
        System.out.println(
            minArrows(points));
    }
}

Python3




# Python3 program for the above approach
 
# Function to find the minimum count of
# arrows required to burst all balloons
def minArrows(points):
 
    # To sort our array according
    # to end position of balloons
    points = sorted(points,  key = lambda x:x[1])
 
    # Initialize end variable with
    # the end of first balloon
    end = points[0][1];
 
    # Initialize arrow with 1
    arrow = 1;
 
    # Iterate through the entire
    # arrow of points
    for i in range (1, len(points)) :
 
        # If the start of ith balloon
          # <= end than do nothing
        if (points[i][0] <= end) :
            continue;
     
 
        # if start of the next balloon
        # >= end of the first balloon
        # then increment the arrow
        else :
 
            # Update the new end
            end = points[i][1]       
            arrow = arrow + 1
    
    # Return the total count of arrows
    return arrow;
 
# Driver Code
points = [[10, 16 ], [ 2, 8 ], [1, 6 ], [ 7, 12 ]]
print(minArrows(points))
   
# This code is contributed by AR_Gaurav

Javascript




<script>
// Javascript program for the above approach
 
function cmp(a, b) {
  return a[1] - b[1];
}
 
// Function to find the minimum count of
// arrows required to burst all balloons
function minArrows(points)
{
 
  // To sort our array according
  // to end position of balloons
  points.sort(cmp);
 
 
  // Initialize end variable with
  // the end of first balloon
  let end = points[0][1];
 
  // Initialize arrow with 1
  let arrow = 1;
 
  // Iterate through the entire
  // arrow of points
  for (let i = 1; i < points.length; i++) {
    // If the start of ith balloon
    // <= end than do nothing
    if (points[i][0] <= end) {
      continue;
    }
 
    // if start of the next balloon
    // >= end of the first balloon
    // then increment the arrow
    else {
      // Update the new end
      end = points[i][1];
      arrow++;
    }
  }
 
  // Return the total count of arrows
  return arrow;
}
 
// Driver code
let points = [
  [10, 16],
  [2, 8],
  [1, 6],
  [7, 12],
];
document.write(minArrows(points));
 
// This code is contributed by gfgking.
</script>
Output: 
2

 

Time Complexity: O(N*log(N))
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :