Cuente el número de coordenadas de una array que satisface las condiciones dadas

Dada una array arr[] que consta de N coordenadas en el plano cartesiano , la tarea es encontrar el número de coordenadas, como (X, Y) , que satisface todas las siguientes condiciones:

  1. Todos los posibles arr[i][0] deben ser menores que X y arr[i][1] deben ser iguales a Y .
  2. Todos los arr[i][0] posibles deben ser mayores que X y arr[i][1] debe ser igual a Y .
  3. Todos los posibles arr[i][0] deben ser menores que Y y arr[i][1] deben ser iguales a X .
  4. Todos los arr[i][0] posibles deben ser mayores que Y y arr[i][1] debe ser igual a X .

Ejemplos:

Entrada: arr[][] = {{0, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 0}}
Salida: 1
Explicación: Solo existe una coordenada que satisface la condición dada, es decir (0, 0) , en base a las siguientes condiciones:

  1. arr[2] = {1, 0}: Dado que arr[2][0](= 1) > X(= 0) y arr[2][1](= 0) == Y(= 0), condición 1 está satisfecho.
  2. arr[4] = {-1, 0}: Dado que arr[4][0](= -1) < 0 y arr[4][1](= 0) == Y(= 0), la condición 2 es satisfecho.
  3. arr[1] = {0, 1}: Dado que arr[1][0](= 0) == X(= 0) y arr[1][1](= 1) > Y(= 0), condición 3 está satisfecho.
  4. arr[3] = {0, -1}: Dado que arr[3][0](= 0) == X(= 0) y arr[3][1](= -1) < Y(= 0) , se cumple la condición 4.

Por lo tanto, el número total de puntos es 1.

Entrada: arr[][] = {{1, 0}, {2, 0}, {1, 1}, {1, -1}}
Salida: 0

Enfoque: El problema dado se puede resolver considerando cada coordenada, digamos (arr[i][0], arr[i][1]) como las coordenadas resultantes, y si la coordenada (arr[i][0], arr [i][1]) satisface todas las condiciones dadas, luego cuente las coordenadas actuales. Después de verificar todas las coordenadas, imprima el valor del conteo total obtenido.

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the number of
// coordinates from a given set
// that satisfies the given conditions
int centralPoints(int arr[][2], 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][0] > x and
            // arr[j][1] == y
            if (arr[j][0] > x
                && arr[j][1] == y) {
                c1 = 1;
            }
 
            // If arr[j][0] < x and
            // arr[j][1] == y
            if (arr[j][1] > y
                && arr[j][0] == x) {
                c2 = 1;
            }
 
            // If arr[j][1] > y and
            // arr[j][0] == x
            if (arr[j][0] < x
                && arr[j][1] == y) {
                c3 = 1;
            }
 
            // If arr[j][1] < y and
            // arr[j][0] == 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
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

// 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][0] > x and
                // arr[j][1] == y
                if (arr[j][0] > x && arr[j][1] == y) {
                    c1 = 1;
                }
 
                // If arr[j][0] < x and
                // arr[j][1] == y
                if (arr[j][1] > y && arr[j][0] == x) {
                    c2 = 1;
                }
 
                // If arr[j][1] > y and
                // arr[j][0] == x
                if (arr[j][0] < x && arr[j][1] == y) {
                    c3 = 1;
                }
 
                // If arr[j][1] < y and
                // arr[j][0] == 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][0] > x and
            # arr[j][1] == y
            if (arr[j][0] > x and arr[j][1] == y):
                c1 = 1
 
            # If arr[j][0] < x and
            # arr[j][1] == y
            if (arr[j][1] > y and arr[j][0] == x):
                c2 = 1
 
            # If arr[j][1] > y and
            # arr[j][0] == x
            if (arr[j][0] < x and arr[j][1] == y):
                c3 = 1
 
            # If arr[j][1] < y and
            # arr[j][0] == 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][0] > x and
            // arr[j][1] == y
            if (arr[j, 0] > x && arr[j, 1] == y)
            {
                c1 = 1;
            }
 
            // If arr[j][0] < x and
            // arr[j][1] == y
            if (arr[j, 1] > y && arr[j, 0] == x)
            {
                c2 = 1;
            }
 
            // If arr[j][1] > y and
            // arr[j][0] == x
            if (arr[j, 0] < x && arr[j, 1] == y)
            {
                c3 = 1;
            }
 
            // If arr[j][1] < y and
            // arr[j][0] == 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

<script>
 
// Javascript program to implement the
// above approach
 
    // Function to count the number of
    // coordinates from a given set
    // that satisfies the given conditions
    function centralPoints(arr, N)
    {
        
        // Stores the count of central points
        let count = 0;
  
        // Store the count of
        // each x and y coordinates
        let c1, c2, c3, c4;
  
        // Find all possible pairs
        for (let 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
            let x = arr[i][0];
            let y = arr[i][1];
  
            // Check the conditions for
            // each point by generating
            // all possible pairs
            for (let j = 0; j < N; j++) {
  
                // If arr[j][0] > x and
                // arr[j][1] == y
                if (arr[j][0] > x && arr[j][1] == y) {
                    c1 = 1;
                }
  
                // If arr[j][0] < x and
                // arr[j][1] == y
                if (arr[j][1] > y && arr[j][0] == x) {
                    c2 = 1;
                }
  
                // If arr[j][1] > y and
                // arr[j][0] == x
                if (arr[j][0] < x && arr[j][1] == y) {
                    c3 = 1;
                }
  
                // If arr[j][1] < y and
                // arr[j][0] == 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
    let arr = [[ 1, 0 ], [ 2, 0 ], [ 1, 1 ], [ 1, -1 ]];
    let N = arr.length;
   document.write(centralPoints(arr, N));
  
 // This code is contributed by splevel62.
</script>
Producción: 

0

 

Complejidad Temporal: O(N 2 )
Espacio Auxiliar: O(1), ya que no se ha tomado ningún espacio extra.

Publicación traducida automáticamente

Artículo escrito por abhijeet010304 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *