We are given n points in a Cartesian plane. Our task is to find the minimum number of points that should be removed in order to get the remaining points on one side of any axis.
Input : 4 1 1 2 2 -1 -1 -2 2 Output : 1 Explanation : If we remove (-1, -1) then all the remaining points are above x-axis. Thus the answer is 1. Input : 3 1 10 2 3 4 11 Output : 0 Explanation : All points are already above X-axis. Hence the answer is 0.
This problem is a simple example of constructive brute force algorithm on Geometry. The solution can be approached simply by finding the number of points on all sides of the X-axis and Y-axis. The minimum of this will be the answer.
# Python3 program to find minimum points to be
# moved so that all points are on same side.
# Function to find the minimum number
# of points
def findmin(p, n):
a, b, c, d = 0, 0, 0, 0
for i in range(n):
# Number of points on the left
# of Y-axis.
if (p[i] <= 0): a += 1 # Number of points on the right # of Y-axis. elif (p[i] >= 0):
b += 1
# Number of points above X-axis.
if (p[i] >= 0):
c += 1
# Number of points below X-axis.
elif (p[i] <= 0): d += 1 return min([a, b, c, d]) # Driver Code p = [ [1, 1], [2, 2], [-1, -1], [-2, 2] ] n = len(p) print(findmin(p, n)) # This code is contributed by Mohit Kumar [tabbyending] Output :
- Program to check if the points are parallel to X axis or Y axis
- Number of Integral Points between Two Points
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Ways to choose three points with distance between the most distant points <= L
- Minimum lines to cover all points
- Minimum area of a Polygon with three points given
- Number of quadrilaterals possible from the given points
- Number of triangles that can be formed with given N points
- Maximum number of segments that can contain the given points
- Number of triangles in a plane if no more than two points are collinear
- Find the number of points that have atleast 1 point above, below, left or right of it
- Number of horizontal or vertical line segments to connect 3 points
- Number of ordered points pair satisfying line equation
- Orientation of 3 ordered points
- Circle and Lattice Points
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29