📌  相关文章
📜  查找数组最后一个元素的最大可能值

📅  最后修改于: 2021-04-22 09:17:43             🧑  作者: Mango

给定一个大小为N的非负数组arr和一个表示移动次数的整数M ,这样一动作,该数组中任何一个元素的值将减少1,而其右边相邻元素的值将增加1。任务是在给定的M个移动中找到数组最后一个元素的最大可能值。

例子:

方法:
将一个值从一个元素移动到最后一个元素所需的移动次数由它们之间的距离计算得出。对于数组中的每个元素,如果该元素与最后一个元素之间的距离小于等于M,则可以将该元素移到最后一个元素。因此,为了移动它,请随着距离增加最后一个元素,并随着距离减少剩余的移动次数。

下面是上述方法的实现:

CPP
// C++ program to find the maximum possible
// value of last element of the array
  
#include 
using namespace std;
  
// Function to find the maximum possible
// value of last element of the array
int maxValue(int arr[], int n, int moves)
{
  
    // Traverse for all element
    for (int i = n - 2; i >= 0; i--) {
        if (arr[i] > 0) {
            // Find the distance
            int distance = n - 1 - i;
  
            // If moves less than distance then
            // we can not move this number to end
            if (moves < distance)
                break;
  
            // How many number we can move to end
            int can_take = moves / distance;
  
            // Take the minimum of both of them
            int take = min(arr[i], can_take);
  
            // Increment in the end
            arr[n - 1] += take;
  
            // Remove taken moves
            moves -= take * distance;
        }
    }
  
    // Return the last element
    return arr[n - 1];
}
  
// Driver code
int main()
{
    int arr[] = { 2, 3, 0, 1 };
    int M = 5;
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    cout << maxValue(arr, N, M);
  
    return 0;
}


Java
// Java program to find the maximum possible
// value of last element of the array
import java.util.*;
  
class GFG{
   
// Function to find the maximum possible
// value of last element of the array
static int maxValue(int arr[], int n, int moves)
{
   
    // Traverse for all element
    for (int i = n - 2; i >= 0; i--) {
        if (arr[i] > 0) {
            // Find the distance
            int distance = n - 1 - i;
   
            // If moves less than distance then
            // we can not move this number to end
            if (moves < distance)
                break;
   
            // How many number we can move to end
            int can_take = moves / distance;
   
            // Take the minimum of both of them
            int take = Math.min(arr[i], can_take);
   
            // Increment in the end
            arr[n - 1] += take;
   
            // Remove taken moves
            moves -= take * distance;
        }
    }
   
    // Return the last element
    return arr[n - 1];
}
   
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 0, 1 };
    int M = 5;
    int N = arr.length;
   
    // Function call
    System.out.print(maxValue(arr, N, M)); 
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to find the maximum possible
# value of last element of the array
  
# Function to find the maximum possible
# value of last element of the array
def maxValue(arr, n, moves):
  
    # Traverse for all element
    for i in range(n - 2, -1, -1):
        if (arr[i] > 0):
              
            # Find the distance
            distance = n - 1 - i
  
            # If moves less than distance then
            # we can not move this number to end
            if (moves < distance):
                break
  
            # How many number we can move to end
            can_take = moves // distance
  
            # Take the minimum of both of them
            take = min(arr[i], can_take)
  
            # Increment in the end
            arr[n - 1] += take
  
            # Remove taken moves
            moves -= take * distance
  
    # Return the last element
    return arr[n - 1]
  
# Driver code
if __name__ == '__main__':
    arr= [2, 3, 0, 1]
    M = 5
    N = len(arr)
  
    # Function call
    print(maxValue(arr, N, M))
      
# This code is contributed by mohit kumar 29


C#
// C# program to find the maximum possible
// value of last element of the array
using System;
  
class GFG{
    
// Function to find the maximum possible
// value of last element of the array
static int maxValue(int []arr, int n, int moves)
{
    
    // Traverse for all element
    for (int i = n - 2; i >= 0; i--) {
        if (arr[i] > 0) {
            // Find the distance
            int distance = n - 1 - i;
    
            // If moves less than distance then
            // we can not move this number to end
            if (moves < distance)
                break;
    
            // How many number we can move to end
            int can_take = moves / distance;
    
            // Take the minimum of both of them
            int take = Math.Min(arr[i], can_take);
    
            // Increment in the end
            arr[n - 1] += take;
    
            // Remove taken moves
            moves -= take * distance;
        }
    }
    
    // Return the last element
    return arr[n - 1];
}
    
// Driver code
public static void Main(String[] args)
{
    int []arr = { 2, 3, 0, 1 };
    int M = 5;
    int N = arr.Length;
    
    // Function call
    Console.Write(maxValue(arr, N, M)); 
}
}
  
// This code is contributed by PrinciRaj1992


输出:
3