Sort by

recency

|

15 Discussions

|

  • + 0 comments

    Hi everyone, I get TLE for the last 4 test cases... please advise me on how to optimize the code. Thank you!

    >

    mod = 10**9 + 7 def solve(balls, k): if k == 1: return 0

    balls.sort()
    ans = 0
    n = len(balls)
    
    for i, ball in enumerate(balls):
        if i >= k-1:
            ans += ball * (comb(i, k-1))
    
        if i < n-k+1:
            ans -= ball * comb(n-i-1, k-1)
    
    return ans % mod
    
  • + 0 comments

    I am running a scholarships website for international students on wordpress. Can I use this,"Choose and Calculate" functions in my website for smooth and easy data input?

  • + 0 comments

    have coded the following (naive) O(n^2) solution code below. (Sorry HackerRank people for posting code, I'll not do it again :-) ) It passes only a few test cases and times out as I expected on others. Can anyone see a problem with the logic?

  • [deleted]
    + 0 comments

    Could someone explain what the loops mean? This is taken from Editorial

        for(int i=k; i<=n; i++) ans += a[i] * C(i-1, k-1) % MOD;
    for(int i=1; i<=n-k+1; i++) ans -= a[i] * C(n-i, k-1) % MOD;
    
  • + 0 comments

    I have coded the following (naive) O(n^2) solution code below. (Sorry HackerRank people for posting code, I'll not do it again :-) ) It passes only a few test cases and times out as I expected on others. Can anyone see a problem with the logic?

    include

    using namespace std;

    typedef unsigned int ui; typedef unsigned long long ull; ull MOD = 1e9+7;

    int main () { ui N, K; ull sum = 0, max; cin >> N >> K; vector balls (N, 0); for (ull i = 0; i < N; i++) { cin >> balls[i]; } sort(balls.begin(), balls.end()); for (ull i = 0; K > 1 && i < N; i++) { for (ull j = i+K-1; j < N; j++) {
    sum += balls[j] - balls[i] % MOD; sum %= MOD; } } cout << sum << endl; return 0; }