📌  相关文章
📜  通过执行最多 K 次给定的操作来最大化第一个数组元素

📅  最后修改于: 2021-09-05 11:52:23             🧑  作者: Mango

给定一个大小为N和整数K的数组arr[] ,任务是通过执行以下操作至多K次来找到数组的第一个元素的最大值:

  1. 选择一对索引ij (0 ≤ i, j ≤ N-1)使得|i − j| = 1arr i > 0
  2. 在这两个索引上设置arr i = arr i − 1arr j = arr j + 1

例子:

处理方法:按照以下步骤解决问题:

  1. 在任何时候,最好选择最接近数组第一个元素的索引ij ,其中i > j
  2. 因此,对于每个操作,从左到右遍历数组并将元素移近第一个元素。
  3. 如果所有元素在某个时刻都位于第一个位置,则停止遍历并打印第一个数组元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to maximize
// the first array element
int getMax(int arr[], int N, int K)
{
 
    // Traverse the array
    for (int i = 1; i < N; i++) {
 
        // Initialize cur_val to a[i]
        int cur_val = arr[i];
 
        // If all operations
        // are not over yet
        while (K >= i) {
 
            // If current value is
            // greater than zero
            if (cur_val > 0) {
 
                // Incrementing first
                // element of array by 1
                arr[0] = arr[0] + 1;
 
                // Decrementing current
                // value of array by 1
                cur_val = cur_val - 1;
 
                // Decrementing number
                // of operations by i
                K = K - i;
            }
 
            // If current value is
            // zero, then break
            else
                break;
        }
    }
 
    // Print first array element
    cout << arr[0];
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 0, 3, 2 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given K
    int K = 5;
 
    // Prints the maximum
    // possible value of the
    // first array element
    getMax(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
  // Function to maximize
  // the first array element
  static void getMax(int arr[], int N, int K)
  {
 
    // Traverse the array
    for (int i = 1; i < N; i++)
    {
 
      // Initialize cur_val to a[i]
      int cur_val = arr[i];
 
      // If all operations
      // are not over yet
      while (K >= i)
      {
 
        // If current value is
        // greater than zero
        if (cur_val > 0)
        {
 
          // Incrementing first
          // element of array by 1
          arr[0] = arr[0] + 1;
 
          // Decrementing current
          // value of array by 1
          cur_val = cur_val - 1;
 
          // Decrementing number
          // of operations by i
          K = K - i;
        }
 
        // If current value is
        // zero, then break
        else
          break;
      }
    }
 
    // Print first array element
    System.out.print(arr[0]);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
     
    // Given array
    int arr[] = { 1, 0, 3, 2 };
 
    // Size of the array
    int N = arr.length;
 
    // Given K
    int K = 5;
 
    // Prints the maximum
    // possible value of the
    // first array element
    getMax(arr, N, K);
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to maximize
# the first array element
def getMax(arr, N, K):
     
    # Traverse the array
    for i in range(1, N, 1):
         
        # Initialize cur_val to a[i]
        cur_val = arr[i]
 
        # If all operations
        # are not over yet
        while (K >= i):
             
            # If current value is
            # greater than zero
            if (cur_val > 0):
 
                # Incrementing first
                # element of array by 1
                arr[0] = arr[0] + 1
 
                # Decrementing current
                # value of array by 1
                cur_val = cur_val - 1
 
                # Decrementing number
                # of operations by i
                K = K - i
 
            # If current value is
            # zero, then break
            else:
                break
 
    # Print first array element
    print(arr[0])
 
# Driver Code
if __name__ == '__main__':
     
    # Given array
    arr = [ 1, 0, 3, 2 ]
 
    # Size of the array
    N = len(arr)
 
    # Given K
    K = 5
 
    # Prints the maximum
    # possible value of the
    # first array element
    getMax(arr, N, K)
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to maximize
// the first array element
static void getMax(int[] arr, int N,
                   int K)
{
     
    // Traverse the array
    for(int i = 1; i < N; i++)
    {
         
        // Initialize cur_val to a[i]
        int cur_val = arr[i];
   
        // If all operations
        // are not over yet
        while (K >= i)
        {
             
            // If current value is
            // greater than zero
            if (cur_val > 0)
            {
                 
                // Incrementing first
                // element of array by 1
                arr[0] = arr[0] + 1;
                 
                // Decrementing current
                // value of array by 1
                cur_val = cur_val - 1;
                 
                // Decrementing number
                // of operations by i
                K = K - i;
            }
   
            // If current value is
            // zero, then break
            else
                break;
        }
    }
     
    // Print first array element
    Console.Write(arr[0]);
} 
 
// Driver code
static void Main()
{
     
    // Given array
    int[] arr = { 1, 0, 3, 2 };
     
    // Size of the array
    int N = arr.Length;
     
    // Given K
    int K = 5;
     
    // Prints the maximum
    // possible value of the
    // first array element
    getMax(arr, N, K);
}
}
 
// This code is contributed by divyesh072019


Javascript


输出:
3

时间复杂度: O(N)
辅助空间: O(1)

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