GeeksforGeeks App
Open App
Browser
Continue

# Maximize first array element by performing given operations at most K times

Given an array arr[] of size N an integer K, the task is to find the maximize the first element of the array by performing the following operations at most K times:

1. Choose a pair of indices i and j (0 ≤ i, j ≤ N-1) such that |i − j| = 1 and arri > 0.
2. Set arri = arri − 1 and arrj = arrj + 1 on those two indices.

Examples:

Input: arr[ ] = {1, 0, 3, 2}, K = 5
Output: 3
Explanation:
One of the possible set of operations can be:
Operation 1: Select i = 3 and j = 2. Therefore, the array modifies to {1, 1, 2, 2}.
Operation 2: Select i = 3 and j = 2. Therefore, the array modifies to {1, 2, 1, 2}.
Operation 3: Select i = 2 and j = 1. Therefore, the array modifies to {2, 1, 1, 2}.
Operation 4: Select i = 2 and j = 1. Therefore, the array modifies to {3, 0, 1, 2}.

Input: arr[] = {5, 1}, K = 2
Output: 6

Approach: Follow the steps below to solve the problem:

1. At any point, it is optimal to choose indices i and j closest to first element of the array, with i > j.
2. Therefore, for every operation, traverse the array from left to right and move the elements closer towards the first element.
3. If all the elements are in the first position at some point, stop traversing and print the first array element.

Below is the implementation of the above approach:

## 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

 ``

Output:

`3`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up