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.

**Examples :**

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.

**Approach :**

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.

## C++

`// CPP program to find minimum points to be moved ` `// so that all points are on same side. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `typedef` `long` `long` `ll; ` ` ` `// Structure to store the coordinates of a point. ` `struct` `Point ` `{ ` ` ` `int` `x, y; ` `}; ` ` ` `// Function to find the minimum number of points ` `int` `findmin(Point p[], ` `int` `n) ` `{ ` ` ` `int` `a = 0, b = 0, c = 0, d = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// Number of points on the left of Y-axis. ` ` ` `if` `(p[i].x <= 0) ` ` ` `a++; ` ` ` ` ` `// Number of points on the right of Y-axis. ` ` ` `else` `if` `(p[i].x >= 0) ` ` ` `b++; ` ` ` ` ` `// Number of points above X-axis. ` ` ` `if` `(p[i].y >= 0) ` ` ` `c++; ` ` ` ` ` `// Number of points below X-axis. ` ` ` `else` `if` `(p[i].y <= 0) ` ` ` `d++; ` ` ` `} ` ` ` ` ` `return` `min({a, b, c, d}); ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` `Point p[] = { {1, 1}, {2, 2}, {-1, -1}, {-2, 2} }; ` ` ` `int` `n = ` `sizeof` `(p)/` `sizeof` `(p[0]); ` ` ` `cout << findmin(p, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum points to be moved ` `// so that all points are on same side. ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Structure to store the coordinates of a point. ` `static` `class` `Point ` `{ ` ` ` `int` `x, y; ` ` ` ` ` `public` `Point(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `this` `.x = x; ` ` ` `this` `.y = y; ` ` ` `} ` `}; ` ` ` `// Function to find the minimum number of points ` `static` `int` `findmin(Point p[], ` `int` `n) ` `{ ` ` ` `int` `a = ` `0` `, b = ` `0` `, c = ` `0` `, d = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `// Number of points on the left of Y-axis. ` ` ` `if` `(p[i].x <= ` `0` `) ` ` ` `a++; ` ` ` ` ` `// Number of points on the right of Y-axis. ` ` ` `else` `if` `(p[i].x >= ` `0` `) ` ` ` `b++; ` ` ` ` ` `// Number of points above X-axis. ` ` ` `if` `(p[i].y >= ` `0` `) ` ` ` `c++; ` ` ` ` ` `// Number of points below X-axis. ` ` ` `else` `if` `(p[i].y <= ` `0` `) ` ` ` `d++; ` ` ` `} ` ` ` `return` `Math.min(Math.min(a, b), ` ` ` `Math.min(c, d)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `Point p[] = {` `new` `Point(` `1` `, ` `1` `), ` `new` `Point(` `2` `, ` `2` `), ` ` ` `new` `Point(-` `1` `, -` `1` `), ` `new` `Point(-` `2` `, ` `2` `)}; ` ` ` `int` `n = p.length; ` ` ` `System.out.println(findmin(p, n)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# 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` `] <` `=` `0` `): ` ` ` `a ` `+` `=` `1` ` ` ` ` `# Number of points on the right ` ` ` `# of Y-axis. ` ` ` `elif` `(p[i][` `0` `] >` `=` `0` `): ` ` ` `b ` `+` `=` `1` ` ` ` ` `# Number of points above X-axis. ` ` ` `if` `(p[i][` `1` `] >` `=` `0` `): ` ` ` `c ` `+` `=` `1` ` ` ` ` `# Number of points below X-axis. ` ` ` `elif` `(p[i][` `1` `] <` `=` `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 ` |

*chevron_right*

*filter_none*

## C#

`// C# rogram to find minimum points to be moved ` `// so that all points are on same side. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Structure to store the coordinates of a point. ` `public` `class` `Point ` `{ ` ` ` `public` `int` `x, y; ` ` ` ` ` `public` `Point(` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` `this` `.x = x; ` ` ` `this` `.y = y; ` ` ` `} ` `}; ` ` ` `// Function to find the minimum number of points ` `static` `int` `findmin(Point []p, ` `int` `n) ` `{ ` ` ` `int` `a = 0, b = 0, c = 0, d = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// Number of points on the left of Y-axis. ` ` ` `if` `(p[i].x <= 0) ` ` ` `a++; ` ` ` ` ` `// Number of points on the right of Y-axis. ` ` ` `else` `if` `(p[i].x >= 0) ` ` ` `b++; ` ` ` ` ` `// Number of points above X-axis. ` ` ` `if` `(p[i].y >= 0) ` ` ` `c++; ` ` ` ` ` `// Number of points below X-axis. ` ` ` `else` `if` `(p[i].y <= 0) ` ` ` `d++; ` ` ` `} ` ` ` `return` `Math.Min(Math.Min(a, b), ` ` ` `Math.Min(c, d)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `Point []p = {` `new` `Point(1, 1), ` ` ` `new` `Point(2, 2), ` ` ` `new` `Point(-1, -1), ` ` ` `new` `Point(-2, 2)}; ` ` ` `int` `n = p.Length; ` ` ` `Console.WriteLine(findmin(p, n)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output :**

1

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.

## Recommended Posts:

- Program to check if the points are parallel to X axis or Y axis
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Number of lines from given N points not parallel to X or Y axis
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Minimum number of edges required to be removed from an Undirected Graph to make it acyclic
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Find the equation of plane which passes through two points and parallel to a given axis
- Count of Right-Angled Triangle formed from given N points whose base or perpendicular are parallel to X or Y axis
- Program to calculate area of a rhombus whose one side and diagonal are given
- Minimum letters to be removed to make all occurrences of a given letter continuous
- Minimum size substring to be removed to make a given string palindromic
- Minimum characters required to be removed to sort binary string in ascending order
- Number of Integral Points between Two Points
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Minimum side of square embedded in Regular polygon with N sides
- 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

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.