You are not logged in. Please login at to post your questions!


Need help in this very easy problem

Im getting WA in this easy problem.Please help me

Problem link-

My Solution Link-

asked 26 May '17, 16:18

vivek96's gravatar image

accept rate: 7%

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(Array A of size N)
Sum = Sum of every element of A
if (Sum + M) % N ==0:

My C++ Code

Hope this helps, if any doubts please feel free to ask! :)

Happy Coding.


answered 26 May '17, 17:07

divyansh_gaba7's gravatar image

accept rate: 23%

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

(26 May '17, 17:18) vivek961★
(26 May '17, 17:20) divyansh_gaba74★

any tips senior?so i can improve my thinking

(26 May '17, 17:22) vivek961★

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

(26 May '17, 17:25) vivek961★

I'd like to quote you, "Try and try until you get AC!!!".

And for your query: you can follow these steps:

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.

(26 May '17, 17:26) divyansh_gaba74★

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.


answered 26 May '17, 16:30

asutoshrana's gravatar image

accept rate: 42%

edited 26 May '17, 16:31

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

(26 May '17, 16:36) vivek961★

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

(26 May '17, 16:39) vivek961★

For the below Input:
3 6
4 2 3
The Output of your solution is:
Whereas the expected output should be:
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.


answered 26 May '17, 17:04

pankaj_chopra's gravatar image

accept rate: 21%

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

(26 May '17, 17:08) vivek961★

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.

(26 May '17, 17:11) pankaj_chopra4★

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

(26 May '17, 17:32) vivek961★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 26 May '17, 16:18

question was seen: 538 times

last updated: 26 May '17, 17:37