lrecknagel
lrecknagel

Reputation: 317

Find to closest higher and lower number in an array

Heyo,

I´m actually try to implement a function that takes an integer as input. I´ve also have an array of ascendent integer numbers.

Now i´ve try to find the closest lower and closest higher number to my single integer.

I´ve like to return it as an array but I´ve only found a solution to find THE one closest number to a given input.

public int getClosestTimeValue(int time) {
int nearest = -1;
int bestDistanceFoundYet = Integer.getInteger(null);
int[] array = null;     
// We iterate on the array...
for (int i = 0; i < array.length; i++) {
     // if we found the desired number, we return it.
if (array[i] == time) {
    return array[i];
    } else {
        int d = Math.abs(time - array[i]);
if (d < bestDistanceFoundYet) {
    nearest = array[i];
}
    }
}
    return nearest;
}

Has anyone an idea how I can solve this problem in java?

Thank you, Lucas

Upvotes: 0

Views: 2378

Answers (3)

danrodi
danrodi

Reputation: 296

At the moment you are searching for one time only. To find both the closest lower and closest higher time, you should have two variables. Then you can check whether the iterated time is lower or higher than the input and store the values in corresponding variables. Also at the moment you are returning only one value, but in order to return multiple values, you should do it through an array.

I'm not sure whether it answers your question, but here's how I would solve the problem:

array = new int[]; // Array of times you have declared elsewhere.

// Method which returns the array of found times.
public int[] getClosestTime(int time) {
    int closestLowerTime = 0;
    int closestHigherTime = 100; // Value bigger than the largest value in the array.
    times = new int[2]; // Array for keeping the two closest values.
    // Iterating the array.
    for (int i = 0; i < array.length; i++) {
        // Finding the two closest values.
        int difference = time - array[i];
        if (difference > 0 && array[i] > closestLowerTime) {
            closestLowerTime = array[i];
        } else if (difference < 0 && array[i] < closestHigherTime) {
            closestHigherTime = array[i];
        }
    }
    times[0] = closestLowerTime;
    times[1] = closestHigherTime;
    return times;
}

This finds both the closest lower and higher value and returns them as an array. At the moment I solved it as the times were between 0 and 100, but in case you don't know the largest time value, you can find it through another loop which iterates through the array and stores the largest value in closestHigherTime. I didn't find a proper way to return the exact value through an array, but is it required?

Upvotes: 1

Neil Madden
Neil Madden

Reputation: 568

If you are not required to use an array directly, then you can use a NavigableSet and the ceiling()/floor() methods to get the nearest greater/lesser elements in the set. Example:

NavigableSet<Integer> values = new TreeSet<Integer>();
for (int x : array) { values.add(x); }
int lower = values.floor(time);
int higher = values.ceiling(time);

If you are required to use an array (homework?) then find a good reference on binary search.

Upvotes: 2

Srinath Mandava
Srinath Mandava

Reputation: 3462

As the array is sorted....

1) Check the middle two elements ..if both are less than the number check the left half (.i.e repeat step1)
else if both are greater than the number repeat step1 for right half...else the selected two numbers are your required answer

Upvotes: 0

Related Questions