We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int howManyGames(int p, int d, int m, int s) {
// invalid condition
if (s < p) {
return 0;
}
int n1 = 1 + (p - m) / d; // max number of games down to 'm'
int s1 = n1 * (2*p - (n1 - 1) * d) / 2; // total cost down to 'm'
int res;
// case 01 : 's' is in between 'p' and 's1'
if (s < s1) {
double det = sqrt((2*p + d) * (2*p + d) - 8*s*d);
res = (2*p + d - det) / (2 * d);
}
// case 02 : 's' is greater than 's1'
else{
res = n1 + (s - s1) / m;
}
return res;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Halloween Sale
You are viewing a single comment's thread. Return to all comments →
Time Complexity : O(1)
Visit my code in github : https://github.com/praveen-de-silva/HackerRank_ProblemSolving/blob/main/HaloweenSale.cpp
int howManyGames(int p, int d, int m, int s) { // invalid condition if (s < p) { return 0; }
}