info@meritcampus.com    +91-85006-22255
...
`

Write a program to get the nearest value from the list of values as explained below.

Input (Double value, List of double values) Output
12.0
[3.0, 23.0, 34.02, 11.23, 56.89, 10.98, 9.78]
10.98
Note: Nearest value of 12.0 is 11.23 and before the nearest is 10.98(According to ascending order of list like [3.0, 9.78, 10.98, 11.23, 23.0, 34.02, 56.89])
29.00
[-10.23, 12.34, -23.45, 27.00, 31.00, 32.32, 28.55]
27.00
Note: Nearest value of 29.00 is 28.55 and before the nearest is 27.00(According to ascending order of list like [-23.45, -10.23, 12.34, 27.00, 28.55, 31.00, 32.32])
23.00
[20.00, 55.23, 96.23, 34.56, 45.67, 12.34, 44.55]
12.34
Note: Nearest value of 23.00 is 20.00 and before the nearest is 12.34(According to ascending order of list like [12.34, 20.00, 34.56, 44.55, 45.67, 55.23, 96.23])
25.00
[11.0, 11.00, 23.00, 27.00, 29.00, 40.00, 18.00]
18.00
Note: Nearest value of 25.00 is 23.00 and before the nearest is 18.00(According to ascending order of list like [11.0, 11.00, 18.00, 23.00, 27.00, 29.00, 40.00])
12.00
[] {EMPTY LIST}
null
null
[11.0, 11.00, 23.00, 27.00, 29.00, 40.00, 18.00]
null
null
null
null

``` import java.util.*; class GetBeforeNearestValue {     public static void main(String s[])    {        Double value = 12.0;        ArrayList<Double> list = new ArrayList<Double>();        list.add(3.0);        list.add(23.0);        list.add(34.02);        list.add(11.23);        list.add(56.89);        list.add(10.98);        Double result = getBeforeNearestValue(value, list);        System.out.println("The value before the nearest value for given value "+value+" from the list of values "+list+" is "+result);    }         public static Double getBeforeNearestValue(Double value, List<Double> list)         {             Double result = null; ```
```             return result;         } } ```
Topic:

### User comments below.All of them might not be correct.

*check for the proper values..if not return null;
* here we need to find the Difference between the value and each element in the array..use the Math.abs() to get the Positive value
*result=list.get(0);
* initially store the difference between  0th  element and value..
*sort the List using Collections.sort(list);
*start for loop..initialize loop control to 1 Bcz we already find the 0th element difference
*compare previous difference with  the current difference..if previous>current than assign    previous=current and result=list.get(i)
if(temp>Math.abs(list.get(i)-value))
temp=Math.abs(list.get(i)-value);
result=list.get(i);
* after coming out of the loop we need to return the before element so get index of result using indexOf(result) and now we need to get before elementd so list.get(index-1);
result=list.get(list.indexOf(result)-1);

Posted by Uday Kumar    2014-12-26 06:41:59

here we have a value and list ......and we have to find the nearest num of value in the list... ......firstly we have to chek wethr value or list is not null.. if either is null den retrn null........ else do the following->>> since we have to find the before nearest value so frstly we will find th nearest to the value in list.....let it be n1..... now then we will find the nearest num to n1 in th list .....this obtained nmbr will be the befor nearest to the actual value...... for this take a variable a=1 this variable is for finding nearest and second nearest numbr...... take a while loop while(a<=2) so wen a=1 it will find nearest num... wen a=2 it will find before nearest amd then exit...... now to find a nearest num we have to compare each num in th list with value.... the num whose difrnc is minimum is nearest...... frstly take a loop to traverse every elemnt of list... take dfrnc=math. abs(value-list(0) ....dis will gv initial value to dfrnc and n1=list(0)....now take j=1 to list. length..... so take a variabl temp=math.abs(value-list(j)) now if(temp<dfrnc) den dfrnc=temp...and..n1=list(j)..... exit for loop.... now aftr this we will have the nearest num to the Input value... now to find before nearest assign value=n1 so in the next iteratn it will compute nearest value to n1 obtained in previous iteratn.......exit while loop... and now finally rtrn n1 ....wich is our desired result

Posted by Asma Mujtaba Khan    2014-12-26 12:46:29

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Second Correct Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-29 05:33:42

© meritcampus 2019

All Rights Reserved.