Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • 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
Programmingoneonone

Learn everything about programming

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

Follow US

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