HackerEarth Minimum additions problem solution

In this HackerEarth Minimum additions problem solution, You have given an array A of N positive integers. Your task is to add a minimum number of non-negative integers to the array such that the floor of an average of array A becomes less than or equal to K.

The floor of an average of array A containing N integers is equal to [sigma(i=1 to N, Ai) / N]. Here [.] is the floor function. You are given T test cases.


HackerEarth Minimum additions problem solution


HackerEarth Minimum additions problem solution.

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
assert(1 <= t && t <= 10);
while(t--)
{
int n, k;
cin >> n >> k;
assert(1 <= n && n <= 5e5);
int i;
int arr[n+1];
int sum = 0;
for(i=1;i<=n;i++)
{
cin >> arr[i];
sum+=arr[i];
}

int add = 1 + sum/(k+1) - n;
add = max(add, 0LL);
cout << add << '\n';
}
return 0;
}

Second solution

t = int(input())
while t > 0:
t -= 1
n, k = map(int, input().split())
s = sum(map(int, input().split()))
print(max(0, s // (k + 1) - n + 1))


Post a Comment

0 Comments