📌  相关文章
📜  最大化数组的第一个元素,使平均值保持不变

📅  最后修改于: 2021-09-06 06:16:26             🧑  作者: Mango

给定一个长度为N的数组arr[]和一个整数X ,任务是找到更新后的第一个元素的最大值R ,使得数组的平均值保持不变并且数组中的任何元素都不应该变成负数。第一个元素的最大值应在 arr[0] <= R <= X 范围内
例子:

方法:这个想法是利用这样一个事实,即第一个元素可以以数组元素保持正数的方式递增,然后每个元素 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)。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live