# Fredo and Array Update in python

I will have an interview with a company which like the hackerearth.com. I don’t know how to work and doing the code perfectly. Could you help me with the following example?
This is the example for the .hackerearth.com, however, I don’t know that I should consider the constraint in the code? can I use a package like NumPy? or I should only use the basic calculation with my self? Could you check my response and let me know the problem with that? Thank you so much

Input Format:

First line of input consists of an integer N denoting the number of elements in the array A.
Second line consists of N space separated integers denoting the array elements.

Output Format:

The only line of output consists of the value of x.

Input Constraints:
1<N<100

1<A[i]<100

explanation:

An initial sum of array is 1+2+3+4+5=15
When we update all elements to 4, the sum of array which is greater than 15 .
Note that if we had updated the array elements to 3, which is not greater than 15 . So, 4 is the minimum value to which array elements need to be updated.

``````# Write your code here
import numpy as np
A= [1, 2, 3,4,5]

for i in range(1, max(A)+1):
old = sum(A)
new = sum(i*np.ones(len(A)))

diff = new-old
if diff>0:
print(i)
break
``````

## Solution

Well this isn’t Code Review stack exchange, but:

• You don’t say how to calculate `x`. It seems to be something to do with finding an average value, but no-one can judge your code without know what it’s trying to do. A web search suggests it is this:

Fredo is assigned a new task today. He is given an array A containing N integers. His task is to update all elements of array to some minimum value x , that is, ; such that sum of this new array is strictly greater than the sum of the initial array. Note that x should be as minimum as possible such that sum of the new array is greater than the sum of the initial array.

• Given that the task starts by accepting input, it’s important that your program does this part.
``````N = int(input()) # you can put a prompt string in here, but may conflict with limited output
A = list(map(int,input().split()))
# might need input checks
# might need range checks
# might check that A has exactly N values
``````
• you don’t need to recalculate `old = sum(A)` every time around your search loop
• calculation of `new` doesn’t need a `sum` at all – it’s just `new = i * len(A)`
• there’s no point in checking values of `i` at or below `min(A)`
• your search will fail if all values of `A` are the same (try it), because you never look above `max(A)`

These remarks apply to your approach; a more efficient search would be binary chop, and there is also a mathematical way to go straight to the answer from `sum(A)` without any searching:
`x = sum(A) // len(A) + 1`