Bus Routes - Google Kickstart Round B 2020

Can anyone check what is wrong with my approach for Bus Routes question?
I am finding the bus which runs on day that is “farthest” from D (making sure the same bus does not run in between those days). Now that “farthest” day is passed on to the same function as D, this process keeps on repeating till there is only one element left in array. The final returned value should be the answer according to me.
Test Set 2 was showing Runtime Error but Test Set 1 passed without any problem.

Here is my code:

#include<iostream>
using namespace std;
int long long find_max(int *arr, int n, int long long d){
    int index=n-1, mx=d%arr[n-1];
    for(int i=n-1; i>=0; i--){
        if(d%arr[i]>mx){
            mx=d%arr[i];
            index=i+1;
        }
    }
    if(n==1){
        return d-mx;
    }
    int long long ans=find_max(arr, index, d-mx);
    return ans;
}
int main(){
	int t;
	cin >> t;
	for(int i=0; i<t; i++){
		int n;
		int long long d;
		cin >> n >> d;
		int arr[n];
		for(int j=0; j<n; j++){
			cin >> arr[j];
		}
		int long long ans=find_max(arr, n, d);
		cout << "Case #" << i+1 << ": " <<ans << endl;
	}
}

Array should be made of type long long

1 Like

I had a doubt related to first test case
can’t the answer be 7
as the days of journey can be day 7 for x = 7
day 8 for x =2
and day 9 for x =3?

Then It would give result as 7(latest by which the journey can be started)

No not 7 as it would not be divisible by 3

I had the same doubt but the buses were supposed to be taken in the order so the correct answer is 6

1 Like

Got it, but it is now showing wrong answer for Test Set 2.