Related Articles

# Count number of coordinates from an array satisfying the given conditions

• Last Updated : 21 May, 2021

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:

1. All possible arr[i] must be less than X and arr[i] must be equal to Y.
2. All possible arr[i] must be greater than X and arr[i] must be equal to Y.
3. All possible arr[i] must be less than Y and arr[i] must be equal to X.
4. All possible arr[i] must be greater than Y and arr[i] 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:

1. arr = {1, 0}: Since arr(= 1) > X(= 0) and arr(= 0) == Y(= 0), condition 1 is satisfied.
2. arr = {-1, 0}: Since arr(= -1) < 0 and arr(= 0) == Y(= 0), condition 2 is satisfied.
3. arr = {0, 1}: Since arr(= 0) == X(= 0) and arr(= 1) > Y(= 0), condition 3 is satisfied.
4. arr = {0, -1}: Since arr(= 0) == X(= 0) and arr(= -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], arr[i]) as the resultant coordinates, and if the coordinate (arr[i], arr[i]) 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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to count the number of``// coordinates from a given set``// that satisfies the given conditions``int` `centralPoints(``int` `arr[], ``int` `N)``{``    ``// Stores the count of central points``    ``int` `count = 0;` `    ``// Store the count of``    ``// each x and y coordinates``    ``int` `c1, c2, c3, c4;` `    ``// Find all possible pairs``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Initialize variables c1, c2,``        ``// c3, c4 to define the status``        ``// of conditions``        ``c1 = 0, c2 = 0, c3 = 0;``        ``c4 = 0;` `        ``// Stores value of each point``        ``int` `x = arr[i];``        ``int` `y = arr[i];` `        ``// Check the conditions for``        ``// each point by generating``        ``// all possible pairs``        ``for` `(``int` `j = 0; j < N; j++) {` `            ``// If arr[j] > x and``            ``// arr[j] == y``            ``if` `(arr[j] > x``                ``&& arr[j] == y) {``                ``c1 = 1;``            ``}` `            ``// If arr[j] < x and``            ``// arr[j] == y``            ``if` `(arr[j] > y``                ``&& arr[j] == x) {``                ``c2 = 1;``            ``}` `            ``// If arr[j] > y and``            ``// arr[j] == x``            ``if` `(arr[j] < x``                ``&& arr[j] == y) {``                ``c3 = 1;``            ``}` `            ``// If arr[j] < y and``            ``// arr[j] == x``            ``if` `(arr[j] < y``                ``&& arr[j] == x) {``                ``c4 = 1;``            ``}``        ``}` `        ``// If all conditions satisfy``        ``// then point is central point``        ``if` `(c1 + c2 + c3 + c4 == 4) {` `            ``// Increment the count by 1``            ``count++;``        ``}``    ``}` `    ``// Return the count``    ``return` `count;``}` `// Driver Code``int` `main()``{``    ``int` `arr = { { 1, 0 }, { 2, 0 },``                      ``{ 1, 1 }, { 1, -1 } };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << centralPoints(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `public` `class` `GFG {` `    ``// Function to count the number of``    ``// coordinates from a given set``    ``// that satisfies the given conditions``    ``static` `int` `centralPoints(``int` `arr[][], ``int` `N)``    ``{``      ` `        ``// Stores the count of central points``        ``int` `count = ``0``;` `        ``// Store the count of``        ``// each x and y coordinates``        ``int` `c1, c2, c3, c4;` `        ``// Find all possible pairs``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// Initialize variables c1, c2,``            ``// c3, c4 to define the status``            ``// of conditions``            ``c1 = ``0``;``            ``c2 = ``0``;``            ``c3 = ``0``;``            ``c4 = ``0``;` `            ``// Stores value of each point``            ``int` `x = arr[i][``0``];``            ``int` `y = arr[i][``1``];` `            ``// Check the conditions for``            ``// each point by generating``            ``// all possible pairs``            ``for` `(``int` `j = ``0``; j < N; j++) {` `                ``// If arr[j] > x and``                ``// arr[j] == y``                ``if` `(arr[j][``0``] > x && arr[j][``1``] == y) {``                    ``c1 = ``1``;``                ``}` `                ``// If arr[j] < x and``                ``// arr[j] == y``                ``if` `(arr[j][``1``] > y && arr[j][``0``] == x) {``                    ``c2 = ``1``;``                ``}` `                ``// If arr[j] > y and``                ``// arr[j] == x``                ``if` `(arr[j][``0``] < x && arr[j][``1``] == y) {``                    ``c3 = ``1``;``                ``}` `                ``// If arr[j] < y and``                ``// arr[j] == x``                ``if` `(arr[j][``1``] < y && arr[j][``0``] == x) {``                    ``c4 = ``1``;``                ``}``            ``}` `            ``// If all conditions satisfy``            ``// then point is central point``            ``if` `(c1 + c2 + c3 + c4 == ``4``) {` `                ``// Increment the count by 1``                ``count++;``            ``}``        ``}` `        ``// Return the count``        ``return` `count;``    ``}``  ` `    ``// Driver Code``    ``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));``    ``}``}` `// This code is contributed by Kingash.`

## Python3

 `# Python3 program for the above approach` `# Function to count the number of``# coordinates from a given set``# that satisfies the given conditions``def` `centralPoints(arr, N):``    ` `    ``# Stores the count of central points``    ``count ``=` `0` `    ``# Find all possible pairs``    ``for` `i ``in` `range``(N):``        ` `        ``# Initialize variables c1, c2,``        ``# c3, c4 to define the status``        ``# of conditions``        ``c1 ``=` `0``        ``c2 ``=` `0``        ``c3 ``=` `0``        ``c4 ``=` `0` `        ``# Stores value of each point``        ``x ``=` `arr[i][``0``]``        ``y ``=` `arr[i][``1``]` `        ``# Check the conditions for``        ``# each point by generating``        ``# all possible pairs``        ``for` `j ``in` `range``(N):``            ` `            ``# If arr[j] > x and``            ``# arr[j] == y``            ``if` `(arr[j][``0``] > x ``and` `arr[j][``1``] ``=``=` `y):``                ``c1 ``=` `1` `            ``# If arr[j] < x and``            ``# arr[j] == y``            ``if` `(arr[j][``1``] > y ``and` `arr[j][``0``] ``=``=` `x):``                ``c2 ``=` `1` `            ``# If arr[j] > y and``            ``# arr[j] == x``            ``if` `(arr[j][``0``] < x ``and` `arr[j][``1``] ``=``=` `y):``                ``c3 ``=` `1` `            ``# If arr[j] < y and``            ``# arr[j] == x``            ``if` `(arr[j][``1``] < y ``and` `arr[j][``0``] ``=``=` `x):``                ``c4 ``=` `1` `        ``# If all conditions satisfy``        ``# then point is central point``        ``if` `(c1 ``+` `c2 ``+` `c3 ``+` `c4 ``=``=` `4``):``            ` `            ``# Increment the count by 1``            ``count ``+``=` `1` `    ``# Return the count``    ``return` `count` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``arr ``=` `[ [ ``1``, ``0` `], [ ``2``, ``0` `],``            ``[ ``1``, ``1` `], [ ``1``, ``-``1` `] ]``    ``N ``=` `len``(arr)``    ` `    ``print``(centralPoints(arr, N));` `# This code is contributed by SURENDRA_GANGWAR`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to count the number of``// coordinates from a given set``// that satisfies the given conditions``static` `int` `centralPoints(``int``[,] arr, ``int` `N)``{``    ` `    ``// Stores the count of central points``    ``int` `count = 0;` `    ``// Store the count of``    ``// each x and y coordinates``    ``int` `c1, c2, c3, c4;` `    ``// Find all possible pairs``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ` `        ``// Initialize variables c1, c2,``        ``// c3, c4 to define the status``        ``// of conditions``        ``c1 = 0;``        ``c2 = 0;``        ``c3 = 0;``        ``c4 = 0;` `        ``// Stores value of each point``        ``int` `x = arr[i, 0];``        ``int` `y = arr[i, 1];` `        ``// Check the conditions for``        ``// each point by generating``        ``// all possible pairs``        ``for``(``int` `j = 0; j < N; j++)``        ``{``            ` `            ``// If arr[j] > x and``            ``// arr[j] == y``            ``if` `(arr[j, 0] > x && arr[j, 1] == y)``            ``{``                ``c1 = 1;``            ``}` `            ``// If arr[j] < x and``            ``// arr[j] == y``            ``if` `(arr[j, 1] > y && arr[j, 0] == x)``            ``{``                ``c2 = 1;``            ``}` `            ``// If arr[j] > y and``            ``// arr[j] == x``            ``if` `(arr[j, 0] < x && arr[j, 1] == y)``            ``{``                ``c3 = 1;``            ``}` `            ``// If arr[j] < y and``            ``// arr[j] == x``            ``if` `(arr[j, 1] < y && arr[j, 0] == x)``            ``{``                ``c4 = 1;``            ``}``        ``}` `        ``// If all conditions satisfy``        ``// then point is central point``        ``if` `(c1 + c2 + c3 + c4 == 4)``        ``{``            ` `            ``// Increment the count by 1``            ``count++;``        ``}``    ``}` `    ``// Return the count``    ``return` `count;``}` `// Driver Code``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));``}``}` `// This code is contributed by ukasp`

## Javascript

 ``
Output:
`0`

Time Complexity: O(N2)
Auxiliary Space: O(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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up