1

Find the nearest number in a list of numbers

 2 years ago
source link: https://www.codesd.com/item/find-the-nearest-number-in-a-list-of-numbers.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find the nearest number in a list of numbers

advertisements

I have a list of constant numbers. I need to find the closest number to x in the list of the numbers. Any ideas on how to implement this algorithm?


Well, you cannot do this faster than O(N) because you have to check all numbers to be sure you have the closest one. That said, why not use a simple variation on finding the minimum, looking for the one with the minimum absolute difference with x?

If you can say the list is ordered from the beginning (and it allows random-access, like an array), then a better approach is to use a binary search. When you end the search at index i (without finding x), just pick the best out of that element and its neighbors.

Tags algorithm

Related Articles

How to find the maximum number in a list with numbers and strings in Python?

I have a list which contains a word and three different numbers. I want to find a way to select the largest of these three numbers, but whenever I use the max function it selects the word instead. Is there any solution to this? Part of my code goes a

C # Find the nearest number in Array

int[] array = new int[5]{5,7,8,15,20}; int TargetNumber = 13; The Nearest Number to 13 is 15 in my Array. How can I Find The nearest number in the array. ex: 15 in my arrayEDIT: Have adjusted the queries below to convert to using long arithmetic, so

Find the nearest number of the given numbers in a list ~ Python

How would you find the closest number in comparison to numbers given in a list? This is what I have tried but I have come of unsuccessfull: setted_list = [2,9,6,20,15] value_chosen = 17 while True: final_value = setted_list[0] if setted_list[1] - val

Find the largest number of selected list items in Python

I am using Python 3.2.3, and would like to find the largest number from specific elements in a list. It is also important that I retain some knowledge of which element is the largest number, but am quite flexible on how this occurs. Let me explain...

Beginning of Java: Find the largest number in a list

I am trying to write a code that prompts the user for three numbers and the program is supposed to say what number is the greatest. I didn't want to do a bunch of "System.out.print" in the if and else if statements. The error according to the de

C Function to find the nearest number in a file

I'm making a C function where I need to find the closest standard resistance value in a file after calculating the equivalent. req1 is the equivalent resistance value. Right now it only works if the equivalent value is equal to some value in the file

how to write a function to find the maximum number in a list of type number in ocaml?

how to write a function that returns the maximum number from a list of number values, if one exists. In the case it is given the empty list then it cannot return a number. i get: let rec max_number_list l = match l with |[] -> None |x::_ -> x |x::xs

How can I find the duplicate number in a list provided and print this number?

This question already has an answer here: Syntax error on print with Python 3 [duplicate] 10 answers I would like to write a short Python function that will detect the duplicate number in a list of integers and print out that number to standard outpu

Find the nearest number as the factors give a list of premiums

Say I have a number, I can find all the prime factors that make up that number. For instance, 6000 is 2^4 * 3 * 5^3. If I have a number that doesn't factorize well (given a list of acceptable primes), how can I find the next closest number? For insta

Java: find the largest number in a list of a number of integers that the user has configured

the code below compiles perfectly for 5 integers entered by the user. what i want to do is alter my code so that i can as the user how many numbers the user wants in the list. then the program will ask the user to enter that many integers and then th

How to find the index number in a list of lists

how to find the nth index in a arraylist ex: i hv a List<Character> charList = new ArrayList<Character>(Arrays.asList('s', 'h','a','r','a','n')) charList.indexOf('a'); always gives the first index of the a. How do I get the nth?Here's a Java 8

How do you find the maximum number in a list without using the & ldquo; Max?

I'm a beginner at programming, and I'm trying to create a program that generates 1000 random numbers, and then prints out the highest value of the numbers in the list of numbers (I am aware that my program only outputs 10 numbers, it is because I'm t

You are looking for an efficient algorithm to find the nearest integer in a list of integers

Here's the situation. I have a sorted list of integers representing events that need to be fired at a certain millisecond. That list might look like: 0 1500 5000 9348 89234 109280 109281 109283 150000 I then have a playhead that typically moves forwa

Find the nearest number when one of the fields is null but you need a value

I am working on a report that gives me a Monthly Rate for each Generator we have based on the GenAmps and GenVolts. If My Pricing table has the GenAmps # and GenVolt size then it will populate a Monthly Rate. There are cases as you can see below when

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK