Who Gets the Catch?

Sort by

recency

|

26 Discussions

|

  • + 0 comments
    # For those who are still struggling to pass all test cases:
    def whoGetsTheCatch(n, x, X, V):
        minTime = 10**10
        for i in range(n):
            dis = abs(X[i]-x)
            time = dis/V[i]
            if time < minTime:
                minTime = time
                index = i
                count = 0           
            if minTime == time:
                count +=1       
        if count > 1:
            return -1
        else:
            return index
    
  • + 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;
    }
    
  • + 0 comments

    passed all 55 test cases.

  • + 0 comments

    i got all test right

    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <limits.h>
    #include <stdbool.h>
    #include <iostream>
    
    int whoGetsTheCatch(int n, int x, int pInt[], int velocities[]);
    
    int main() {
        int n;
        int x;
        std::cin>>n>>x;
        int positions[n],velocities[n];
        int temp;
        for (int i = 0; i <n ; ++i) {
            std::cin>>temp;
            positions[i]=temp;
        }
    
        for (int i = 0; i <n ; ++i) {
            std::cin>>temp;
            velocities[i]=temp;
        }
        int result=whoGetsTheCatch(n,x,positions,velocities);
        std::cout<<result;
    
        return 0;
    }
    
    int whoGetsTheCatch(int n, int x, int pInt[], int velocities[]) {
        float min_so_far=INT16_MAX;
        int numCatchers=0;
        int catcher=-1;
        for (int i = 0; i <n ; ++i) {
            float temp=((float)abs(x-pInt[i]))/velocities[i];
            if (temp==min_so_far)
                numCatchers+=1;
    
            if (temp<min_so_far){
                min_so_far=temp;
                catcher=i;
                numCatchers=1;
            }
        }
    
        if (numCatchers==1)
            return catcher;
        return -1;
    }
    
  • + 0 comments

    All cases is not correct! Such as for the case 44 - 6 and 13 catchers have equaly catching time therefore result = -1 but the cases output is 25!