Find the number of points that have atleast 1 point above, below, left or right of it
Given N points in 2 dimensional plane. A point is said to be above another point if the X coordinates of both points are same and the Y coordinate of the first point is greater than the Y coordinate of the second point. Similarly, we define below, left and right. The task is to count the number of points that have atleast one point above, below, left or right of it.
Examples:
Input: arr[] = {{0, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 0}}
Output: 1
The only point which satisfies the condition is the point (0, 0).
Input: arr[] = {{0, 0}, {1, 0}, {0, -2}, {5, 0}}
Output: 0
Approach: For every X coordinate, find 2 values, the minimum and maximum Y coordinate among all points that have this X coordinate. Do the same thing for every Y coordinate. Now, for a point to satisfy the constraints, its Y coordinate must lie in between the 2 calculated values for that X coordinate. Check the same thing for its X coordinate.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define MX 2001 #define OFF 1000 // Represents a point in 2-D space struct point { int x, y; }; // Function to return the count of // required points int countPoints( int n, struct point points[]) { int minx[MX]; int miny[MX]; // Initialize minimum values to infinity fill(minx, minx + MX, INT_MAX); fill(miny, miny + MX, INT_MAX); // Initialize maximum values to zero int maxx[MX] = { 0 }; int maxy[MX] = { 0 }; int x, y; for ( int i = 0; i < n; i++) { // Add offset to deal with negative // values points[i].x += OFF; points[i].y += OFF; x = points[i].x; y = points[i].y; // Update the minimum and maximum // values minx[y] = min(minx[y], x); maxx[y] = max(maxx[y], x); miny[x] = min(miny[x], y); maxy[x] = max(maxy[x], y); } int count = 0; for ( int i = 0; i < n; i++) { x = points[i].x; y = points[i].y; // Check if condition is satisfied // for X coordinate if (x > minx[y] && x < maxx[y]) // Check if condition is satisfied // for Y coordinate if (y > miny[x] && y < maxy[x]) count++; } return count; } // Driver code int main() { struct point points[] = { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; int n = sizeof (points) / sizeof (points[0]); cout << countPoints(n, points); } |
Java
// Java implementation of the approach import java.util.*; class GFG { static int MX = 2001 ; static int OFF = 1000 ; // Represents a point in 2-D space static class point { int x, y; public point( int x, int y) { this .x = x; this .y = y; } }; // Function to return the count of // required points static int countPoints( int n, point points[]) { int []minx = new int [MX]; int []miny = new int [MX]; // Initialize minimum values to infinity for ( int i = 0 ; i < n; i++) { minx[i]=Integer.MAX_VALUE; miny[i]=Integer.MAX_VALUE; } // Initialize maximum values to zero int []maxx = new int [MX]; int []maxy = new int [MX]; int x, y; for ( int i = 0 ; i < n; i++) { // Add offset to deal with negative // values points[i].x += OFF; points[i].y += OFF; x = points[i].x; y = points[i].y; // Update the minimum and maximum // values minx[y] = Math.min(minx[y], x); maxx[y] = Math.max(maxx[y], x); miny[x] = Math.min(miny[x], y); maxy[x] = Math.max(maxy[x], y); } int count = 0 ; for ( int i = 0 ; i < n; i++) { x = points[i].x; y = points[i].y; // Check if condition is satisfied // for X coordinate if (x > minx[y] && x < maxx[y]) // Check if condition is satisfied // for Y coordinate if (y > miny[x] && y < maxy[x]) count++; } return count; } // Driver code public static void main(String[] args) { point points[] = { new point( 0 , 0 ), new point( 0 , 1 ), new point( 1 , 0 ), new point( 0 , - 1 ), new point(- 1 , 0 )}; int n = points.length; System.out.println(countPoints(n, points)); } } // This code is contributed by PrinciRaj1992 |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { static int MX = 2001; static int OFF = 1000; // Represents a point in 2-D space public class point { public int x, y; public point( int x, int y) { this .x = x; this .y = y; } }; // Function to return the count of // required points static int countPoints( int n, point []points) { int []minx = new int [MX]; int []miny = new int [MX]; // Initialize minimum values to infinity for ( int i = 0; i < n; i++) { minx[i]= int .MaxValue; miny[i]= int .MaxValue; } // Initialize maximum values to zero int []maxx = new int [MX]; int []maxy = new int [MX]; int x, y; for ( int i = 0; i < n; i++) { // Add offset to deal with negative // values points[i].x += OFF; points[i].y += OFF; x = points[i].x; y = points[i].y; // Update the minimum and maximum // values minx[y] = Math.Min(minx[y], x); maxx[y] = Math.Max(maxx[y], x); miny[x] = Math.Min(miny[x], y); maxy[x] = Math.Max(maxy[x], y); } int count = 0; for ( int i = 0; i < n; i++) { x = points[i].x; y = points[i].y; // Check if condition is satisfied // for X coordinate if (x > minx[y] && x < maxx[y]) // Check if condition is satisfied // for Y coordinate if (y > miny[x] && y < maxy[x]) count++; } return count; } // Driver code public static void Main(String[] args) { point []points = { new point(0, 0), new point(0, 1), new point(1, 0), new point(0, -1), new point(-1, 0)}; int n = points.Length; Console.WriteLine(countPoints(n, points)); } } // This code is contributed by 29AjayKumar |
1
Time Complexity: O(N)
Recommended Posts:
- Find minimum radius such that atleast k point lie inside the circle
- Find the minimum number of rectangles left after inserting one into another
- Minimum number of points to be removed to get remaining points on one side of axis
- Number of Integral Points between Two Points
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Find K Closest Points to the Origin
- Find Corners of Rectangle using mid points
- Steps required to visit M points in order on a circular ring of N points
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Find Simple Closed Path for a given set of points
- Program to find line passing through 2 Points
- Find whether only two parallel lines contain all coordinates points or not
- Find X and Y intercepts of a line passing through the given points
- Find the maximum possible distance from origin using given points
- Find points at a given distance on a line of given slope
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.