프로그래머스 - 가까운 수

차관호·2023년 3월 17일
0

문제링크 - 프로그래머스 - 가까운 수

import java.util.Arrays;

class Solution {
    public int solution(int[] array, int n) {
        Arrays.sort(array);
       int Ad=(Math.abs(array[0]-n)),result=array[0];


       for(int i = 1; i< array.length;i++){
           if(Ad > Math.abs(array[i] - n)){
               Ad = Math.abs(array[i]-n);
               result = array[i];
           }
       }
        return result;
    }
}
profile
안녕하세요 :-)

0개의 댓글