This is the second question
maximum array value
Given an array of n positive integers,the following ops can be done any time
1.choose i st 2<=i<=n
2.choose any x s.t. 1<=x<=arr[i]
3.then set arr[i-1] to arr[i-1]+x
4.set arr[i] to arr[i]-xWhat is the smallest maximum value in the array after performing these operations from 1-4.
T1 input n=4 [1,5,7,6]
output 5
choose a[3[ and x as 4
a[2] becomes 9 and a[3] becomes 3
choose a[2] and x as 4
a[1] becomes 5 and a[2] becomes 5
choose a[4] and x as 1
a[3] becomes 4 and a[4] becomes 5
final array [5,5,3,5] smallest max value is 5T2 n is 3 arr is [5,15,19]
output 13T3 n is 4 arr is [10,3,5,7]
output is 10my solution
vector<int> max_elements(N,0);
for(i=2;i<=N;i++){
for(int x=1;x<=arr[i];x++){
arr[i-1]=arr[i-1]+x;
arr[i]=arr[i]-x;
max_elements.emplace_back(*max_element(arr.begin(),arr.end()));
}
}
return *min_element(max_elements.begin(),max_elements.end());