×

# Need help in this very easy problem

 2 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++ Code Hope this helps, if any doubts please feel free to ask! :) Happy Coding. answered 26 May '17, 17:07 768●1●8 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★ Yes @vivek96 (26 May '17, 17:20) 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★ 1 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. (26 May '17, 17:26)
 1 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 96●2 accept rate: 42% 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★
 1 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. answered 26 May '17, 17:04 329●2●9 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★ 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. (26 May '17, 17:11) 1 i got it,you guys are really helpful,Thanks a lot (26 May '17, 17:32) vivek961★
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×3,706
×2,698
×846
×13

question asked: 26 May '17, 16:18

question was seen: 538 times

last updated: 26 May '17, 17:37