Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- public class Solution {
- public static boolean c(int mid, List<Integer> a) {
- int ct = 1;
- int st = a.get(0);
- for (int i = 1; i < a.size(); i++) {
- if (st + mid < a.get(i)) {
- st = a.get(i);
- ct++;
- }
- if (ct > 2)
- return false;
- }
- return true;
- }
- public static int solution(List<Integer> a) {
- a.sort(null);
- int l = 1, r = Integer.MAX_VALUE;
- int ans = r;
- while (l <= r) {
- int mid = (l + r) / 2;
- if (c(mid, a)) {
- ans = mid;
- r = mid - 1;
- } else
- l = mid + 1;
- }
- return ans;
- }
- public static void main(String[] args) {
- List<Integer> a = Arrays.asList(3, 6, 9, 12, 15);
- System.out.println(solution(a));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement