get closest value to a number in array

后端 未结 12 1678
庸人自扰
庸人自扰 2020-12-01 07:52

I have an array of positive/negative ints

int[] numbers = new int[10];
numbers[0] = 100;
numbers[1] = -34200;
numbers[2] = 3040;
numbers[3] = 400433;
numbers         


        
相关标签:
12条回答
  • 2020-12-01 08:09
    public int getClosestToTarget(int target, int[] values) {
    
        if (values.length < 1)
            throw new IllegalArgumentException("The values should be at least one element");
        if (values.length == 1) {
            return values[0];
        }
        int closestValue = values[0];
        int leastDistance = Math.abs(values[0] - target);
        for (int i = 0; i < values.length; i++) {
            int currentDistance = Math.abs(values[i] - target);
            if (currentDistance < leastDistance) {
                closestValue = values[i];
                leastDistance = currentDistance;
            }
        }
        return closestValue;
    }
    
    0 讨论(0)
  • 2020-12-01 08:12

    you are very close. I think the initial value of 'distance' should be a big number instead of 0. And use the absolute value for the cdistance.

    0 讨论(0)
  • 2020-12-01 08:13

    Kotlin is so helpful

    fun List<Int>.closestValue(value: Int) = minBy { abs(value - it) }
    
    val values = listOf(1, 8, 4, -6)
    
    println(values.closestValue(-7)) // -6
    println(values.closestValue(2)) // 1
    println(values.closestValue(7)) // 8
    
    

    List doesn't need to be sorted BTW

    0 讨论(0)
  • 2020-12-01 08:14
    public class Main    
    {
        public static void main(String[] args)
        {   
            int[] numbers = {6,5,10,1,3,4,2,14,11,12};
    
            for(int i =0; i<numbers.length; i++)
            {
                sum(numbers, i, numbers[i], 12, String.valueOf(numbers[i]));
            }
        }
    
        static void sum(int[] arr, int i, int sum, int target, String s)
        {
    
            int flag = 0;
    
            for(int j = i+1; j<arr.length; j++)
            {
    
                if(arr[i] == target && flag==0)
                {
                    System.out.println(String.valueOf(arr[i]));
                    flag =1;
    
                }
                else if(sum+arr[j] == target)
                { 
                    System.out.println(s+" "+String.valueOf(arr[j]));
    
                }
                else
                {
                    sum(arr, j, sum+arr[j], target, s+" "+String.valueOf(arr[j]));
                }
            }        
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:18
    int valueToFind = 490;
    
    Map<Integer, Integer> map = new HashMap();
    
    for (int i = 0, i < numbers.length; i++){
        map.put(Math.abs(numbers[i] - valueToFind), numbers[i]);
    }
    
    List<Integer> keys = new ArrayList(map.keySet());
    Collections.sort(keys);
    
    return map.get(keys.get(0));
    
    0 讨论(0)
  • 2020-12-01 08:22

    In Java 8:

    List<Integer> list = Arrays.stream(numbers).boxed().collect(Collectors.toList());
    
    int n = 490;
    
    int c = list.stream()
                .min(Comparator.comparingInt(i -> Math.abs(i - n)))
                .orElseThrow(() -> new NoSuchElementException("No value present"));
    

    Initially, you can use a List instead of an Array (lists have much more functionality).

    0 讨论(0)
提交回复
热议问题