给定长度为N且整数X的数组arr [] ,任务是找到更新后的第一个元素的最大值R ,以使数组的平均值保持恒定,并且该数组的任何元素都不应为负。第一个元素的最大值应在arr [0] <= R <= X的范围内
例子:
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
方法:想法是利用第一个元素可以以使数组的元素保持正数的方式递增的事实,然后每个元素arr [i]都可以在0到其原始值arr [i]的范围内。因此,数组的第一个元素可以达到的最大值将是数组的总和,并且由于R的最大值应在arr [0] <= R <= X的范围内,因此R将为S和X的最小值。
算法:
- 通过从0到length – 1的循环迭代,找到数组的总和(例如S ),其中length是数组的长度。
- 在X和S之间找到最小值,该最小值将是可以达到的最大值,范围在arr [0] <= R <= X范围内,以使数组的平均值保持恒定。
举例说明:
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.
下面是上述方法的实现:
C++
// C++ implementation of to
// maximize the first element of
// the array such that average
// of the array remains constant
#include
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
Javascript
5
性能分析:
- 时间复杂度: O(N)
- 辅助空间: O(1)。