tempfile*

 avatar
bruteCoder
java
8 months ago
916 B
5
Indexable
Never
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Solution {
     public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] houseNumbers = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int deliveryDistance = calculateDeliveryDistance(houseNumbers);
        System.out.println(deliveryDistance);
    }

    public static int calculateDeliveryDistance(int[] houseNumbers) {
        Arrays.sort(houseNumbers);
        Set<Integer> uniqueDistances = new HashSet<>();
        for (int i = 1; i < houseNumbers.length; i++) {
            int distance = houseNumbers[i] - houseNumbers[i - 1] - 1;
            if (distance > 0) { // Only consider non-zero distances
                uniqueDistances.add(distance);
            }
        }
        return uniqueDistances.size();
    }
}
Leave a Comment