Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Minimum additions problem solution

YASH PAL, 31 July 202413 February 2026
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 solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes