Im getting WA in this easy problem.Please help me

Problem link-https://www.codechef.com/problems/CHEFGR

My Solution Link-https://www.codechef.com/viewsolution/13789926

Im getting WA in this easy problem.Please help me

Problem link-https://www.codechef.com/problems/CHEFGR

My Solution Link-https://www.codechef.com/viewsolution/13789926

consider the case where N=5, M=5 and the heights of the ground are 1, 1, 1, 1, 1. Then according to your code you will get max = 1 and ans = 0 which is not equal to M, and so your code will return NO. But there is a possibility to increase heights of all the columns by one to make them 2,2,2,2,2 using M=5 and consequently they become equal. So the answer will be YES.

1 Like

For the below Input:

1

3 6

4 2 3

The Output of your solution is:

No

Whereas the expected output should be:

Yes

You are trying to make the heights of all the columns equal to the maximum of the initial heights given.

But the heights can be greater than that also.

As in the above test case, the heights of all the columns can be 5 5 5 after spending all the cubes.

1 Like

Basically in this problem, we are given an array of size N and using EXACTLY M increments we have to make all elements equal.

How to achieve this,

Now, if all the elements of the array are equal,

```
Sum of all elements = N * X, where X is value of each element.
```

It is clear that this Sum is a multiple of N, i.e Sum % N == 0

Since we have to add Exactly M blocks (and each block is of size 1), it means that we will increase the value of sum by M.

If this new value is multiple of N, then answer is Yes.

Otherwise, No.

Pseudo code:

```
Input(N,M)
Input(Array A of size N)
Sum = Sum of every element of A
if (Sum + M) % N ==0:
print(Yes)
else
print(No)
```

My C++

```
[1]
Hope this helps, if any doubts please feel free to ask! :)
Happy Coding.
[1]: https://www.codechef.com/viewsolution/13790370
```

2 Likes

means we have consume m completely whether are already equals or not? am i right? how can i correct my code? please explain your approach.Thanks a lot

is this the only case in which my code will get fail? if i add this case then it will be fine?

means we have to make them equal by using exact n cubes,please tell me correct approach this?

Yes.

You have to spend exactly M cubes in such a way that the heights of all columns can become equal.

You can check that if the number of remaining cubes(after making the heights of all columns equal to the maximum of initial heights) is a multiple of the number of columns, then it is possible to make the heights of all columns equal, otherwise not.

nice,means we are reverse checking the condition,if this possible then this condition must be true? am i right?

any tips senior?so i can improve my thinking

one more help,how to set the lines of code,so everytime i dnt have to rewrite the lines again in codeblocks(c++)?

I’d like to quote you, “Try and try until you get AC!!!”.

And for your query:

you can follow these steps: https://stackoverflow.com/questions/25346321/default-template-in-code-blocks

but the template has to be chosen every time you create a new project, which i find annoying. So i just changed the default file. You can find it at

C:\Program Files (x86)\CodeBlocks\share\CodeBlocks\templates\wizard\console\cpp

By default, it will have the basic “Hello World” program you see everytime you make a new project, just over write it, Make sure to save it in administrator mode.

1 Like

i got it,you guys are really helpful,Thanks a lot

1 Like