Determine the closest number, within the array, to the given number

0

Good I have this problem: from an array and a number determine which number of the array is closest to the number. I do not get it

 public static int masCercano(int[] numeros, int num)
 { 
    int cercano = 0;
    for (int i = 0; i < numeros.length; i++) 
    {
        if (numeros[i] ==  num) 
        {
            return numeros[i];
        }
        else
            if (numeros[i] > num || numeros[i] < num) 
            {
                cercano = numeros[i];
                if (cercano > numeros[i]) 
                {
                    cercano = numeros[i];
                }
                else
                    cercano = numeros[i];
            }
    }
    return cercano;
 }
    
asked by AlejandroB 12.06.2018 в 02:36
source

3 answers

1

The problem is that you are going through all the numbers asking if it is smaller or bigger than the number we need to compare, that is simply cercano will do it equal to the last number that was evaluated and was smaller or bigger than the number to be compared, but At no time are you looking for the nearest minor or the nearest major.

Using a variable called diferencia , we save the one that until now is the smallest difference between the numbers of the array and the number to be compared and likewise we keep saving which is this number with the smallest difference, be it greater or lesser

    public static int masCercano(int[] numeros, int num) {
        int cercano = 0;
        int diferencia = Integer.MAX_VALUE; //inicializado valor máximo de variable de tipo int
        for (int i = 0; i < numeros.length; i++) {
            if (numeros[i] == num) {
                return numeros[i];
            } else {
                if(Math.abs(numeros[i]-num)<diferencia){
                    cercano=numeros[i];
                    diferencia = Math.abs(numeros[i]-num);
                }
            }
        }
        return cercano;
    }

    
answered by 12.06.2018 / 04:02
source
1

This block of code allows you to determine the nearest number if an exact match is not found, which I understand is what you need.

public static int masCercano(int[] numeros, int numero) { 

    int menor = 0;
    int mayor = 0;
    int cercano = 0;

    for (int i = 0; i < numeros.length ; i++) {

        if (numeros[i] == numero) {
            return numeros[i];
        } else if (numeros[i] < numero) {
            menor = numeros[i];
        } else if (numeros[i] > numero) {
            mayor = numeros[i];
        }
    }

    if ((mayor - numero) < (numero - menor)) {
        cercano = mayor;
    } else {
        cercano = menor;
    }

    return cercano;

}
    
answered by 12.06.2018 в 03:33
1

It occurs to me, that you could save the nearest value index eh doing subtraction. Then the closest to 0 is the closest.

public static int masCercano(int[] numeros, int num) { 
  int menorDistanciaActual = Math.abs(num - numeros[0]); // Aca guardas la resta
  int posicionNumeroMasCercano = 0; // Si viene un array con un elemento, es el [0]

  // Empezas en 1, porque ya sabes que el 0 es el mas cercano hasta ahora.
  for (int i = 1; i < numeros.length; i++) {
    int distanciaEntreNumeros = Math.abs(num - numeros[i]);

    if (distanciaEntreNumeros < menorDistanciaActual) {
      menorDistanciaActual = distanciaEntreNumeros;
      posicionNumeroMasCercano = i;
    }
  }

  return numeros[posicionNumeroMasCercano];
}

Note:

  • Use Math.abs () because you want to know the minimum distance, and for that the module is used. If it gave a negative result for example, leaving -100 the distance would be greater than that of 5. In contrast with Math.abs(-100) = 100
  • If an array comes with only one element, that element will return to you.

I hope it serves you. Greetings!

    
answered by 12.06.2018 в 05:17