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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Percentage increase in the cylinder if the height is increased by given percentage but radius remains constant
- Kth smallest element in the array using constant space when array can't be modified
- Remove an element to maximize the GCD of the given array
- Maximize the sum of modulus with every Array element
- Remove array end element to maximize the sum of product
- Maximize array sum by X increments when each element is divided by 10
- Maximize 3rd element sum in quadruplet sets formed from given Array
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Maximize the number of indices such that element is greater than element to its left
- Find Array formed by adding each element of given array with largest element in new array to its left
- Constant time range add operation on an array
- Find duplicates in constant array with elements 0 to N-1 in O(1) space
- Maximize the maximum subarray sum after removing atmost one element
- Program to print an array in Pendulum Arrangement with constant space
- Maximize distinct elements by incrementing/decrementing an element or keeping it same
- Maximize number of groups formed with size not smaller than its largest element
- Average numbers in array
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Largest element smaller than current element on left for every element in Array
- Rearrange the array to maximize the number of primes in prefix sum of the array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.