Average of an Array should be less than or equal to k

  addition, average, c++, floor, minimum

Given an array we have to take average of an array and then take floor of that average and that value should be less than k.
Link of this question:- https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/minimum-additions-0142ac80/

MY CODE:-

#include "bits/stdc++.h"
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        int arr[n];
        int s = 0;
        for(int i=0;i<n;i++){
            cin>>arr[i];
            s += arr[i];
        }

        int ans;

        if(floor(s/n)<=k){
            cout<<0<<endl;
        }      
        else{
            cout<<floor( s/(k+1) - n + 1 )<<endl;
        }
    }
    return 0;
}
  

In my code floor function is not working properly it pass small test case but when it run for large test case it fails. In discussion for some people are using (sum/n)<=k and ll int result=sum/(k+1)-n+1; and their all test case get pass.

We can do this with floor function also but floor function is not working properly in my code .

Can anyone help with my code and tell why floor function is not working in my code and why we are using k+1 and n+1 in the code can we do this differently with any different logic?

Source: Windows Questions C++

LEAVE A COMMENT