Coding Friends

  • + 0 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; }

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

    }****