Minimum number of points to be removed to get remaining points on one side of axis
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 a 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.
Efficient Approach:
- Create a structure Point to store the coordinates of a point.
- Create a function findmin which takes an array of Point p and integer n as arguments.
Declare and initialize integer variables a, b, c, and d to 0.
- Iterate from i=0 to i=n-1:
a. If the x-coordinate of p[i] is less than or equal to 0, increment a by 1.
b. Else if the x-coordinate of p[i] is greater than or equal to 0, increment b by 1.
c. If the y-coordinate of p[i] is greater than or equal to 0, increment c by 1.
d. Else if the y-coordinate of p[i] is less than or equal to 0, increment d by 1.
- Return the minimum value of a, b, c, and d.
- In the main function:
a. Declare an array of Point p and initialize it with coordinates of some points.
b. Calculate the size of the array.
c. Call the findmin function with the array and size as arguments and print the returned value.
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Point
{
int x, y;
};
int findmin(Point p[], int n)
{
int a = 0, b = 0, c = 0, d = 0;
for ( int i = 0; i < n; i++)
{
if (p[i].x <= 0)
a++;
else if (p[i].x >= 0)
b++;
if (p[i].y >= 0)
c++;
else if (p[i].y <= 0)
d++;
}
return min({a, b, c, d});
}
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;
}
|
Java
import java.util.*;
class GFG
{
static class Point
{
int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
};
static int findmin(Point p[], int n)
{
int a = 0 , b = 0 , c = 0 , d = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (p[i].x <= 0 )
a++;
else if (p[i].x >= 0 )
b++;
if (p[i].y >= 0 )
c++;
else if (p[i].y <= 0 )
d++;
}
return Math.min(Math.min(a, b),
Math.min(c, d));
}
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));
}
}
|
Python3
def findmin(p, n):
a, b, c, d = 0 , 0 , 0 , 0
for i in range (n):
if (p[i][ 0 ] < = 0 ):
a + = 1
elif (p[i][ 0 ] > = 0 ):
b + = 1
if (p[i][ 1 ] > = 0 ):
c + = 1
elif (p[i][ 1 ] < = 0 ):
d + = 1
return min ([a, b, c, d])
p = [ [ 1 , 1 ], [ 2 , 2 ], [ - 1 , - 1 ], [ - 2 , 2 ] ]
n = len (p)
print (findmin(p, n))
|
C#
using System;
class GFG
{
public class Point
{
public int x, y;
public Point( int x, int y)
{
this .x = x;
this .y = y;
}
};
static int findmin(Point []p, int n)
{
int a = 0, b = 0, c = 0, d = 0;
for ( int i = 0; i < n; i++)
{
if (p[i].x <= 0)
a++;
else if (p[i].x >= 0)
b++;
if (p[i].y >= 0)
c++;
else if (p[i].y <= 0)
d++;
}
return Math.Min(Math.Min(a, b),
Math.Min(c, d));
}
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));
}
}
|
Javascript
<script>
function findmin(p,n)
{
let a = 0, b = 0, c = 0, d = 0;
for (let i = 0; i < n; i++)
{
if (p[i][0] <= 0)
a++;
else if (p[i][0] >= 0)
b++;
if (p[i][1] >= 0)
c++;
else if (p[i][1] <= 0)
d++;
}
return Math.min(Math.min(a, b),
Math.min(c, d));
}
let p = [ [1, 1], [2, 2], [-1, -1], [-2, 2] ]
let n = p.length;
document.write(findmin(p, n));
</script>
|
Output:
1
Time Complexity: O(n)
Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Last Updated :
15 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...