0

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?

  • 4
    Please don't use so-called "competition" or "online judge" sites to learn C++ or programming in general. that's not really what they're for. Many examples tend to use extremely bad habits, habits which can make you virtually unemployable. And unfortunately this code displays many of those bad habits. One which makes the code invalid C++ actually. If you're serious about learning programming and C++, invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take classes. – Some programmer dude Jul 12 '21 at 07:19
  • 1
    "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." thats not what the task is requiring your code do to – 463035818_is_not_an_ai Jul 12 '21 at 07:24
  • Make the divisions of type float. – starboy_jb Jul 12 '21 at 07:24
  • BTW, you don't need an array (or better a `std::vector`) at all, because you don't actually need to *store* each element, just read it and add to the sum. – Bob__ Jul 12 '21 at 14:08

1 Answers1

0
int n; // non const
int arr[n]; // bad

Don't use VLAs. Consider using a vector in cases where you need variable length.

The problem constraints specify that the values can be upto 1e9. So at max 2e5 (length of array) * 1e9 which will overflow for a 32 bit integer. Use int64_t instead for summation.
Also using floor is redundant here. Integral values are implicitly rounded down. So you can emit the floors entirely.

Staz
  • 348
  • 1
  • 2
  • 6
  • Sorry but i didn't get your answer. So your are saying that I should convert my array into vector and remote floor function from my code? Can you please explain then why we are using sum/(k+1) -n+1, I,m not able to get this why we are using this instead of floor function? Please explain. – YASH PRATAP SINGH Jul 12 '21 at 10:12