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.
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;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Choose and Calculate
You are viewing a single comment's thread. Return to all 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; }