Sort by

recency

|

953 Discussions

|

  • + 0 comments

    Java Solution

        static int flatlandSpaceStations(int n, int[] c) {
            Arrays.sort(c);
            int max = Math.max(c[0], n - 1 - c[c.length - 1]);
            for (int i = 1; i < c.length; i++) {
                max = Math.max(max, (c[i] - c[i - 1]) / 2);
            }
            return max;
        }
    
  • + 0 comments

    Here is problem solution in Python, java, c++, c and javascript - https://programmingoneonone.com/hackerrank-flatland-space-stations-problem-solution.html

  • + 0 comments

    Java 8 solution:

    static int flatlandSpaceStations(int n, int[] c) {
            // Sort space station location in ascending order
            Arrays.sort(c);
            
            // Set max distance intially as the distance between city 0 to the first space station
            int maxDistance = c[0];
            
            // Calculate the max distance of the city between two space station
            for(int i = 1; i <c.length; i++){
                maxDistance = Math.max(maxDistance, (c[i] - c[i-1])/2);
            }
            
            // Calculate the distance between the last n city to the last space station
            maxDistance = Math.max(maxDistance, (n-1) - c[c.length-1]);
            
            return maxDistance;
        }
    
  • + 0 comments

    I tried the below solution which seemed to pass all the test cases. This solution sorts the input array and has a time complexity of O(nlog(n)). The algorithm used is somewhat related to the two-pointer algorithm.

    static int flatlandSpaceStations(int n, int[] c) {

        if(c.length == n)
            return 0;
    
        int maxDist = Integer.MIN_VALUE;
    
        if(c.length == 1){
            for(int i=0; i < n; i++){
                maxDist = Math.max(calculateDistance(i, c[0]), maxDist);
            }    
        }else{
            int left = 0;
            int right = 1;
            int leftDist = 0;
            int rightDist = 0;
            Arrays.sort(c);
            for(int i=0; i < n; i++){
                leftDist = calculateDistance(i, c[left]);
                rightDist = calculateDistance(i, c[right]);
    
                if(leftDist > rightDist){
                    maxDist = Math.max(rightDist, maxDist);
                    if(right < c.length -1){
                        left++;
                        right++;    
                    }
    
                }else{
                    maxDist = Math.max(leftDist, maxDist);
                }
            }
        }
    
        return maxDist;
    
    
    }
    
  • + 0 comments

    Is the custom comparator too much? Maybe. But it's funnier that way.

    int flatlandSpaceStations(int n, vector<int> c) {
        int longuest = 0;
        
        for(int i = 0; i<n; i++ ){
            
            if(find(c.begin(), c.end(), i)==c.end()){
                int closest = *min_element(c.begin(),c.end(),
                                [&](int a, int b){
                                    return abs(a - i) < abs(b - i);
                                });
                
                longuest = abs(closest-i)>longuest? abs(closest-i) : longuest;
            }
            
        }
        
        return longuest;
    }