Maximize the first element of the array such that average remains constant

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 constant

Actual 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 2nd element.
Index 2: Substract 21 from 3rd element.
Index 3: Substract 26 from 4th element.

Below is the implmentation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implmentation 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implmentation 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implmentation 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implmentation 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


Output:

5

Performance Analysis:

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.