Maximize the first element of the array such that average remains constant
Last Updated :
29 Aug, 2021
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: Subtract 289 from 2nd element.
Index 2: Subtract 21 from 3rd element.
Index 3: Subtract 26 from 4th element.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getmax( int arr[], int n,
int x){
int s = 0;
for ( int i = 0; i < n; i++) {
s = s + arr[i];
}
cout << min(s, x);
}
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
import java.util.*;
class GFG{
static void getmax( int arr[], int n,
int x){
int s = 0 ;
for ( int i = 0 ; i < n; i++) {
s = s + arr[i];
}
System.out.print(Math.min(s, x));
}
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);
}
}
|
Python3
def getmax(arr, n, x):
s = 0
for i in range (n):
s = s + arr[i]
print ( min (s, x))
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ]
x = 5
arr_size = len (arr)
getmax(arr, arr_size, x)
|
C#
using System;
class GFG
{
static void getmax( int [] arr, int n , int x)
{
int s = 0;
for ( int i = 0; i < n; i++)
{
s = s + arr[i];
}
Console.WriteLine(Math.Min(s, x));
}
static void Main()
{
int [] arr = new int [] { 1, 2, 3, 4 };
int x = 5;
int arr_size = arr.Length;
getmax(arr, arr_size, x);
}
}
|
Javascript
<script>
function getmax( arr, n, x)
{
let s = 0;
for (let i = 0; i < n; i++)
{
s = s + arr[i];
}
document.write(Math.min(s, x));
}
let arr = [ 1, 2, 3, 4 ];
let x = 5;
let arr_size = arr.length;
getmax(arr, arr_size, x);
</script>
|
Performance Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...