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.
**Binary Search approach **
bool canSurpass(int samDaily, int kellyDaily, int difference, int days) {
int samSolved = difference + days * samDaily;
int kellySolved = days * kellyDaily;
return kellySolved > samSolved;
}
int minNum(int samDaily, int kellyDaily, int difference) {
if (samDaily >= kellyDaily) {
return -1;
}
int low = 0;
int high = difference / (kellyDaily - samDaily) + 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (canSurpass(samDaily, kellyDaily, difference, mid)) {
high = mid - 1;
} else {
low = mid + 1;
}
}
if (canSurpass(samDaily, kellyDaily, difference, low)) {
return low;
} else {
return -1;
}
}****
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Coding Friends
You are viewing a single comment's thread. Return to all comments →
**Binary Search approach ** bool canSurpass(int samDaily, int kellyDaily, int difference, int days) { int samSolved = difference + days * samDaily; int kellySolved = days * kellyDaily; return kellySolved > samSolved; }
int minNum(int samDaily, int kellyDaily, int difference) { if (samDaily >= kellyDaily) { return -1; }
}****