Given N points in a 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++
#include <bits/stdc++.h>
using namespace std;
#define MX 2001
#define OFF 1000
struct point {
int x, y;
};
int countPoints( int n, struct point points[])
{
int minx[MX];
int miny[MX];
fill(minx, minx + MX, INT_MAX);
fill(miny, miny + MX, INT_MAX);
int maxx[MX] = { 0 };
int maxy[MX] = { 0 };
int x, y;
for ( int i = 0; i < n; i++) {
points[i].x += OFF;
points[i].y += OFF;
x = points[i].x;
y = points[i].y;
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;
if (x > minx[y] && x < maxx[y])
if (y > miny[x] && y < maxy[x])
count++;
}
return count;
}
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
import java.util.*;
class GFG
{
static int MX = 2001 ;
static int OFF = 1000 ;
static class point
{
int x, y;
public point( int x, int y)
{
this .x = x;
this .y = y;
}
};
static int countPoints( int n, point points[])
{
int []minx = new int [MX];
int []miny = new int [MX];
for ( int i = 0 ; i < n; i++)
{
minx[i]=Integer.MAX_VALUE;
miny[i]=Integer.MAX_VALUE;
}
int []maxx = new int [MX];
int []maxy = new int [MX];
int x, y;
for ( int i = 0 ; i < n; i++)
{
points[i].x += OFF;
points[i].y += OFF;
x = points[i].x;
y = points[i].y;
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;
if (x > minx[y] && x < maxx[y])
if (y > miny[x] && y < maxy[x])
count++;
}
return count;
}
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));
}
}
|
Python3
from sys import maxsize as INT_MAX
MX = 2001
OFF = 1000
class point:
def __init__( self , x, y):
self .x = x
self .y = y
def countPoints(n: int , points: list ) - > int :
minx = [INT_MAX] * MX
miny = [INT_MAX] * MX
maxx = [ 0 ] * MX
maxy = [ 0 ] * MX
x, y = 0 , 0
for i in range (n):
points[i].x + = OFF
points[i].y + = OFF
x = points[i].x
y = points[i].y
minx[y] = min (minx[y], x)
maxx[y] = max (maxx[y], x)
miny[x] = min (miny[x], y)
maxy[x] = max (maxy[x], y)
count = 0
for i in range (n):
x = points[i].x
y = points[i].y
if (x > minx[y] and x < maxx[y]):
if (y > miny[x] and y < maxy[x]):
count + = 1
return count
if __name__ = = "__main__" :
points = [point( 0 , 0 ),
point( 0 , 1 ),
point( 1 , 0 ),
point( 0 , - 1 ),
point( - 1 , 0 )]
n = len (points)
print (countPoints(n, points))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MX = 2001;
static int OFF = 1000;
public class point
{
public int x, y;
public point( int x, int y)
{
this .x = x;
this .y = y;
}
};
static int countPoints( int n, point []points)
{
int []minx = new int [MX];
int []miny = new int [MX];
for ( int i = 0; i < n; i++)
{
minx[i]= int .MaxValue;
miny[i]= int .MaxValue;
}
int []maxx = new int [MX];
int []maxy = new int [MX];
int x, y;
for ( int i = 0; i < n; i++)
{
points[i].x += OFF;
points[i].y += OFF;
x = points[i].x;
y = points[i].y;
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;
if (x > minx[y] && x < maxx[y])
if (y > miny[x] && y < maxy[x])
count++;
}
return count;
}
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));
}
}
|
Javascript
<script>
var MX = 2001;
var OFF = 1000;
function countPoints( n, points)
{
var minx = Array(MX).fill(1000000000);
var miny = Array(MX).fill(1000000000);
var maxx = Array(MX).fill(0);
var maxy = Array(MX).fill(0);
var x, y;
for ( var i = 0; i < n; i++) {
points[i][0] += OFF;
points[i][1] += OFF;
x = points[i][0];
y = points[i][1];
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);
}
var count = 0;
for ( var i = 0; i < n; i++) {
x = points[i][0];
y = points[i][1];
if (x > minx[y] && x < maxx[y])
if (y > miny[x] && y < maxy[x])
count++;
}
return count;
}
var points = [ [ 0, 0 ],
[ 0, 1 ],
[ 1, 0 ],
[ 0, -1 ],
[ -1, 0 ] ];
var n = points.length;
document.write( countPoints(n, points));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N) because it is using auxiliary space for array minx and miny