Who Gets the Catch?

  • + 0 comments
    static int whoGetsTheCatch(int n, int x, int[] X, int[] V){
        float[] times = new float[n];
        int minPosition = -1;
        int numAtMin = 0;
    
        for (int i = 0 ; i < n ;  i++) {
            times[i] = Math.abs((float) X[i]-x) / V[i];
            if (minPosition == -1 || times[i] < times[minPosition]) {
                minPosition = i;
                numAtMin = 1;
            } else if (times[i] == times[minPosition]) {
                numAtMin++;
            }
        }
    
        return numAtMin == 1 ? minPosition: -1;
    }