Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • 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 solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes