Count number of coordinates from an array satisfying the given conditions
Last Updated :
20 Jul, 2022
Given an array arr[] consisting of N coordinates in the Cartesian Plane, the task is to find the number of coordinates, such as (X, Y), that satisfies all the following conditions:
- All possible arr[i][0] must be less than X and arr[i][1] must be equal to Y.
- All possible arr[i][0] must be greater than X and arr[i][1] must be equal to Y.
- All possible arr[i][0] must be less than Y and arr[i][1] must be equal to X.
- All possible arr[i][0] must be greater than Y and arr[i][1] must be equal to X.
Examples:
Input: arr[][] = {{0, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 0}}
Output: 1
Explanation: There exists only one coordinate that satisfy the given condition, i.e. (0, 0), based on the following conditions:
- arr[2] = {1, 0}: Since arr[2][0](= 1) > X(= 0) and arr[2][1](= 0) == Y(= 0), condition 1 is satisfied.
- arr[4] = {-1, 0}: Since arr[4][0](= -1) < 0 and arr[4][1](= 0) == Y(= 0), condition 2 is satisfied.
- arr[1] = {0, 1}: Since arr[1][0](= 0) == X(= 0) and arr[1][1](= 1) > Y(= 0), condition 3 is satisfied.
- arr[3] = {0, -1}: Since arr[3][0](= 0) == X(= 0) and arr[3][1](= -1) < Y(= 0), condition 4 is satisfied.
Therefore, the total number of points is 1.
Input: arr[][] = {{1, 0}, {2, 0}, {1, 1}, {1, -1}}
Output: 0
Approach: The given problem can be solved by considering every coordinate, say (arr[i][0], arr[i][1]) as the resultant coordinates, and if the coordinate (arr[i][0], arr[i][1]) satisfies all the given conditions, then count the current coordinates. After checking for all the coordinates, print the value of the total count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int centralPoints( int arr[][2], int N)
{
int count = 0;
int c1, c2, c3, c4;
for ( int i = 0; i < N; i++) {
c1 = 0, c2 = 0, c3 = 0;
c4 = 0;
int x = arr[i][0];
int y = arr[i][1];
for ( int j = 0; j < N; j++) {
if (arr[j][0] > x
&& arr[j][1] == y) {
c1 = 1;
}
if (arr[j][1] > y
&& arr[j][0] == x) {
c2 = 1;
}
if (arr[j][0] < x
&& arr[j][1] == y) {
c3 = 1;
}
if (arr[j][1] < y
&& arr[j][0] == x) {
c4 = 1;
}
}
if (c1 + c2 + c3 + c4 == 4) {
count++;
}
}
return count;
}
int main()
{
int arr[4][2] = { { 1, 0 }, { 2, 0 },
{ 1, 1 }, { 1, -1 } };
int N = sizeof (arr) / sizeof (arr[0]);
cout << centralPoints(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static int centralPoints( int arr[][], int N)
{
int count = 0 ;
int c1, c2, c3, c4;
for ( int i = 0 ; i < N; i++) {
c1 = 0 ;
c2 = 0 ;
c3 = 0 ;
c4 = 0 ;
int x = arr[i][ 0 ];
int y = arr[i][ 1 ];
for ( int j = 0 ; j < N; j++) {
if (arr[j][ 0 ] > x && arr[j][ 1 ] == y) {
c1 = 1 ;
}
if (arr[j][ 1 ] > y && arr[j][ 0 ] == x) {
c2 = 1 ;
}
if (arr[j][ 0 ] < x && arr[j][ 1 ] == y) {
c3 = 1 ;
}
if (arr[j][ 1 ] < y && arr[j][ 0 ] == x) {
c4 = 1 ;
}
}
if (c1 + c2 + c3 + c4 == 4 ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int arr[][]
= { { 1 , 0 }, { 2 , 0 }, { 1 , 1 }, { 1 , - 1 } };
int N = arr.length;
System.out.println(centralPoints(arr, N));
}
}
|
Python3
def centralPoints(arr, N):
count = 0
for i in range (N):
c1 = 0
c2 = 0
c3 = 0
c4 = 0
x = arr[i][ 0 ]
y = arr[i][ 1 ]
for j in range (N):
if (arr[j][ 0 ] > x and arr[j][ 1 ] = = y):
c1 = 1
if (arr[j][ 1 ] > y and arr[j][ 0 ] = = x):
c2 = 1
if (arr[j][ 0 ] < x and arr[j][ 1 ] = = y):
c3 = 1
if (arr[j][ 1 ] < y and arr[j][ 0 ] = = x):
c4 = 1
if (c1 + c2 + c3 + c4 = = 4 ):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ [ 1 , 0 ], [ 2 , 0 ],
[ 1 , 1 ], [ 1 , - 1 ] ]
N = len (arr)
print (centralPoints(arr, N));
|
C#
using System;
class GFG{
static int centralPoints( int [,] arr, int N)
{
int count = 0;
int c1, c2, c3, c4;
for ( int i = 0; i < N; i++)
{
c1 = 0;
c2 = 0;
c3 = 0;
c4 = 0;
int x = arr[i, 0];
int y = arr[i, 1];
for ( int j = 0; j < N; j++)
{
if (arr[j, 0] > x && arr[j, 1] == y)
{
c1 = 1;
}
if (arr[j, 1] > y && arr[j, 0] == x)
{
c2 = 1;
}
if (arr[j, 0] < x && arr[j, 1] == y)
{
c3 = 1;
}
if (arr[j, 1] < y && arr[j, 0] == x)
{
c4 = 1;
}
}
if (c1 + c2 + c3 + c4 == 4)
{
count++;
}
}
return count;
}
public static void Main( string [] args)
{
int [,] arr = { { 1, 0 }, { 2, 0 },
{ 1, 1 }, { 1, -1 } };
int N = arr.GetLength(0);
Console.WriteLine(centralPoints(arr, N));
}
}
|
Javascript
<script>
function centralPoints(arr, N)
{
let count = 0;
let c1, c2, c3, c4;
for (let i = 0; i < N; i++) {
c1 = 0;
c2 = 0;
c3 = 0;
c4 = 0;
let x = arr[i][0];
let y = arr[i][1];
for (let j = 0; j < N; j++) {
if (arr[j][0] > x && arr[j][1] == y) {
c1 = 1;
}
if (arr[j][1] > y && arr[j][0] == x) {
c2 = 1;
}
if (arr[j][0] < x && arr[j][1] == y) {
c3 = 1;
}
if (arr[j][1] < y && arr[j][0] == x) {
c4 = 1;
}
}
if (c1 + c2 + c3 + c4 == 4) {
count++;
}
}
return count;
}
let arr = [[ 1, 0 ], [ 2, 0 ], [ 1, 1 ], [ 1, -1 ]];
let N = arr.length;
document.write(centralPoints(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...