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;
}
}