Unusual sorting (with.Java)
This article looks into the “unusual sorting (with.Java)” problem.
As I solve coding test problems, I look back on the problems I solved and look into different solution methods to get to know myself.
Let’s look at the problem first.
problem
We want to sort the numbers closest to n based on the integer n.
At this time, if the distance from n is the same, place the larger number first.
Given an array of integers, numlist, and an integer n, complete the solution function so that it returns an array that sorts the elements of numlist in order of proximity to n.
Restrictions
- 1 ≤ n ≤ 10,000
- 1 ≤ elements of numlist ≤ 10,000
- 1 ≤ length of numlist ≤ 100
- numlist does not have duplicate elements.
Input/Output Example
numlist | n | result |
---|---|---|
[1, 2, 3, 4, 5, 6] | 4 | [4, 5, 3, 6, 2, 1] |
[10000,20,36,47,40,6,10,7000] | 30 | [36, 40, 20, 47, 10, 6, 7000, 10000] |
My solution to the problem
import java.util.*;
class Solution {
public int[] solution(int[] numlist, int n) {
int[] answer = new int[numlist.length];
int absValue = 0;
int temp = Integer.MAX_VALUE;
int idx = 0;
Arrays.sort(numlist);
ArrayList<Integer> arr_new = new ArrayList<Integer>();
for (int i : numlist){
arr_new.add(i);
}
for(int i = 0; i < answer.length; i++){
for(int j = arr_new.size() - 1; j >= 0; j--){
absValue = Math.abs(n - arr_new.get(j));
if(temp > absValue){
temp = absValue;
idx = j;
}
}
answer[i] = arr_new.get(idx);
temp = Integer.MAX_VALUE;
arr_new.remove(arr_new.get(idx));
}
return answer;
}
}
Solution explanation
Sorts the given array numlist.
Initialize the array answer to store the correct answer.
Creates an ArrayList arr_new to store each element of the array numlist, and adds each element of the sorted numlist to arr_new.
Calculates the difference between n and the current element and stores it in absValue.
If absValue is less than the minimum value temp so far, temp is updated to absValue and the current index idx is set to the index of the element.
Store the closest number found using temp and idx in answer.
Remove that number from arr_new.
When the iteration is complete, it returns an answer.