Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- public class SafeHouses {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- String[] numbersAsString = scanner.nextLine().split(" ");
- // int[] positions = new int[numbersAsString.length];
- List<Integer> positionsList = new ArrayList<>();
- for (int i = 0; i < numbersAsString.length; i++) {
- // positions [i] = Integer.parseInt(numbersAsString[i]);
- positionsList.add(Integer.parseInt(numbersAsString[i]));
- }
- int maxDistance = 0;
- if(n==positionsList.size()){
- System.out.println(maxDistance);
- return;
- }
- if(positionsList.size()>1){
- Collections.sort(positionsList);
- }else{
- if(positionsList.get(0)>=(n*1.0/2)){
- maxDistance = positionsList.get(0);
- }else{
- maxDistance = (n -1) - positionsList.get(0);
- }
- }
- int distance = 0;
- // int nextPosition = 0;
- for (int i = 0; i < positionsList.size()-1; i++) {
- int nextPosition = positionsList.get(i+1);
- int difference = nextPosition - positionsList.get(i);
- if(i!=0){
- if(i+1==positionsList.size()-1){
- if((difference * 1.0/2)> ((n-1)- nextPosition)){
- if(difference%2==0){
- distance = difference/2;
- }else{
- distance = (difference - 1)/2;
- }
- }else{
- distance = (n-1)- nextPosition;
- }
- }else{
- if(difference%2==0){
- distance = difference/2;
- }else{
- distance = (difference - 1)/2;
- }
- }
- }else{
- if((difference * 1.0/2)>positionsList.get(i)){
- if(i+1==positionsList.size()-1){
- if((difference * 1.0/2)> ((n-1)- nextPosition)){
- if(difference%2==0){
- distance = difference/2;
- }else{
- distance = (difference - 1)/2;
- }
- }else{
- distance = (n-1)- nextPosition;
- }
- }else{
- if(difference%2==0){
- distance = difference/2;
- }else{
- distance = (difference - 1)/2;
- }
- }
- }else{
- distance = positionsList.get(i);
- }
- }
- if(distance>maxDistance){
- maxDistance = distance;
- }
- }
- System.out.println(maxDistance);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement