Page 1 of 1
Given an array arr of n positive integers, the following operation can be performed any number of times. Use a 1-based i
Posted: Sun Jul 10, 2022 11:25 am
by answerhappygod

- Given An Array Arr Of N Positive Integers The Following Operation Can Be Performed Any Number Of Times Use A 1 Based I 1 (44.22 KiB) Viewed 80 times

- Given An Array Arr Of N Positive Integers The Following Operation Can Be Performed Any Number Of Times Use A 1 Based I 2 (40.63 KiB) Viewed 80 times
Given an array arr of n positive integers, the following operation can be performed any number of times. Use a 1-based index for the array. • Choose any / such that 2 s is n. • Choose any x such that 1 sxs arr
• Set arr[i-1] to arr[i-1]+x • Set arr to arr-x Minimize the maximum value of arr using the operation and return the value. Example n=4 arr = [1, 5, 7, 6] Assuming 1-based indexing. One optimal sequences is: • Operation 1: choose /-3, x=4 (note that x <= arr[3], I.e. 4 < 7). • Replace arr[i-1] with arr[i-1]+x or 5 + 4 =9 o Replace arr[] with arr-x or 7-4=3 o The array is now [1, 9, 3, 6] (maximum = 9) • Operation 2:1=2, x=4 o Replace arr[2-1] with 1+4=5 • Replace arr[2] with 9-4-5 • The array is now [5, 5, 3, 6] (maximum = 6) • Operation 3:1-4, x-1, the resulting array is [5. 5, 4, 5] (maximum = 5) The minimum possible value of max(arr) is 5
• Operation 2: i = 2, x = 4 • Replace arr[2-1] with 1+4= 5 • Replace arr[2] with 9-4 = 5 o The array is now [5, 5, 3, 6] (maximum = 6) • Operation 3: i = 4, x= 1, the resulting array is [5, 5, 4, 5] (maximum = 5) The minimum possible value of max(arr) is 5 after operation 3. Function Description Complete the function getMaximum in the editor below. getMaximum has the following parameter: int arr[n]: an array of integers Returns int: the minimum maximum value possible Constraints • 1≤ns105 • 1 s arr ≤ 10⁹ Input Format For Custom Testing The first line contains an integer, n, the number of elements in arr. Each line / of the n subsequent lines (where O si<n) contains an integer, arr. Sample Case O Sample Input For Custom Testing