Given an array **arr[]** of length **N** and an integer **X**, the task is to find the maximum value **R** of updated first element such that the average of the array remains constant and no elements of the array should become negative. The maximum value of the first element should be in the range arr[0] <= R <= X

**Examples:**

Input:arr[] = {1, 2, 3, 4}, X = 5

Output:5

Explanation:

In the above given example the maximum value of the first element

that can be obtained is 5 such that average of array remains constantActual Average of Array = (1 + 2 + 3 + 4) / 4 = 2.5

After incrementing Array will be in any of the following forms –

{5, 2, 3, 0}, Average of array = (5 + 2 + 3 + 0) / 4 = 2.5

{5, 0, 1, 4}, Average of array = (5 + 0 + 1 + 4) / 4 = 2.5

{5, 1, 0, 4}, Average of array = (5 + 1 + 0 + 4) / 4 = 2.5

…… and many more

Input:arr[] = {44, 289, 21, 26}, X = 999

Output:380

Explanation:

In the above given example the maximum value of the first element

that can be attained is 336 such that average of array remains constant

Actual Average of Array = (44 + 289 + 21 + 26) / 4 = 95

After incrementing Array will be in the following form –

{380, 0, 0, 0}, Average of array = (380 + 0 + 0 + 0) / 4 = 95

**Approach**: The idea is to use the fact that the first element can be incremented in such a way that elements of the array remain positive then every element arr[i] can be in the range of 0 to its original value arr[i]. So the maximum value that can be attained by the first element of the array will be the sum of the array, and since the maximum value of the R should be in range arr[0] <= R <= X, So R will be the minimum value of the sum S and X.

**Algorithm:**

- Find the sum (say
**S**) of the array by iterating a loop from 0 to length – 1, where length is the length of array. - Find the minimum value between X and S which will be the maximum value that can be attained which is in range arr[0] <= R <= X, such that average of the array remains constant.

**Explanation with Example:**

Given Array be - {44, 289, 21, 26} and X = 999 Sum of the Array - 44 + 289 + 21 + 26 = 380 Minimum value of the 380 and X = 999 is 380 The array that can be achieved with value as 380 is {380, 0, 0, 0} whereas, The operations are -Index 0:Add 289 + 21 + 26, which is 336.Index 1:Substract 289 from 2^{nd}element.Index 2:Substract 21 from 3^{rd}element.Index 3:Substract 26 from 4^{th}element.

Below is the implementation of above approach:

## C++

`// C++ implementation of to ` `// maximize the first element of ` `// the array such that average ` `// of the array remains constant ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Maximum value of the first ` `// array element that can be attained ` `void` `getmax(` `int` `arr[], ` `int` `n, ` ` ` `int` `x){ ` ` ` ` ` `// Variable to store the sum ` ` ` `int` `s = 0; ` ` ` ` ` `// Loop to find the sum of array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `s = s + arr[i]; ` ` ` `} ` ` ` ` ` `// Desired maximum value ` ` ` `cout << min(s, x); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 3, 4 }; ` ` ` `int` `x = 5; ` ` ` `int` `arr_size = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `getmax(arr, arr_size, x); ` ` ` ` ` `return` `0; ` `} ` |

## Java

`// Java implementation of to ` `// maximize the first element of ` `// the array such that average ` `// of the array remains constant ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Maximum value of the first ` `// array element that can be attained ` `static` `void` `getmax(` `int` `arr[], ` `int` `n, ` ` ` `int` `x){ ` ` ` ` ` `// Variable to store the sum ` ` ` `int` `s = ` `0` `; ` ` ` ` ` `// Loop to find the sum of array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `s = s + arr[i]; ` ` ` `} ` ` ` ` ` `// Desired maximum value ` ` ` `System.out.print(Math.min(s, x)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `}; ` ` ` `int` `x = ` `5` `; ` ` ` `int` `arr_size = arr.length; ` ` ` ` ` `getmax(arr, arr_size, x); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

## Python3

`# Python3 implementation of to ` `# maximize the first element of ` `# the array such that average ` `# of the array remains constant ` ` ` `# Maximum value of the first ` `# array element that can be attained ` `def` `getmax(arr, n, x): ` ` ` ` ` `# Variable to store the sum ` ` ` `s ` `=` `0` ` ` ` ` `# Loop to find the sum of array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `s ` `=` `s ` `+` `arr[i] ` ` ` ` ` `# Desired maximum value ` ` ` `print` `(` `min` `(s, x)) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `] ` ` ` `x ` `=` `5` ` ` `arr_size ` `=` `len` `(arr) ` ` ` ` ` `getmax(arr, arr_size, x) ` ` ` `# This code is contributed by mohit kumar 29 ` |

## C#

`// C# implementation of to ` `// maximize the first element of ` `// the array such that average ` `// of the array remains constant ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Maximum value of the first ` ` ` `// array element that can be attained ` ` ` `static` `void` `getmax(` `int` `[] arr, ` `int` `n , ` `int` `x) ` ` ` `{ ` ` ` `// Variable to store the sum ` ` ` `int` `s = 0; ` ` ` ` ` `// Loop to find the sum of array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `s = s + arr[i]; ` ` ` `} ` ` ` ` ` `// Desired maximum value ` ` ` `Console.WriteLine(Math.Min(s, x)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[] arr = ` `new` `int` `[] { 1, 2, 3, 4 }; ` ` ` `int` `x = 5; ` ` ` `int` `arr_size = arr.Length; ` ` ` ` ` `getmax(arr, arr_size, x); ` ` ` `} ` `} ` ` ` `// This code is contributed by shubhamsingh10 ` |

**Output:**

5

**Performance Analysis:**

**Time Complexity:**O(N)**Auxiliary Space:**O(1).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.