Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Minimum additions problem solution

YASH PAL, 31 July 2024
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))
coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programmingoneonone | WordPress Theme by SuperbThemes