You are viewing a single comment's thread. Return to all comments →
Java Collections.sort(arr);
int menorDiferenca = Integer.MAX_VALUE; List<Integer> result = new ArrayList<>(); for(int i = 0; i < arr.size() - 1; i++){ int diferenca = Math.abs(arr.get(i) - arr.get(i + 1)); if(diferenca < menorDiferenca){ menorDiferenca = diferenca; result.clear(); result.add(arr.get(i)); result.add(arr.get(i + 1)); } else if (diferenca == menorDiferenca) { result.add(arr.get(i)); result.add(arr.get(i + 1)); } } return result; }
Seems like cookies are disabled on this browser, please enable them to open this website
Closest Numbers
You are viewing a single comment's thread. Return to all comments →
Java Collections.sort(arr);