# Maximum possible value of array elements that can be made based on given capacity conditions

• Last Updated : 07 Oct, 2021

Given two arrays arr[] and cap[] both consisting of N positive integers such that the ith element cap[i] denotes the capacity of arr[i], the task is to find the maximum possible value of array elements that can be made such that it is allowed to decrease an array element arr[i] by some arbitrary value and increment any of its adjacent element by the same value if the final value of the adjacent element does not exceed its corresponding capacity.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = {2, 3}, cap[] = {5, 6}
Output: 5
Explanation:
Following operations are performed to maximize value of any element in arr[]:
Operation 1: Decrease arr by 2 and increase arr by 2. Now arr[] = {0, 5}.
Therefore, the maximum element in arr[] is 5.

Input: arr[] = {1, 2, 1}, cap[] = {2, 3, 2}
Output: 3

Approach: The given problem can be solved by using the Greedy Approach which is based on the observation that after performing any number of operations the maximum value cannot exceed the maximum capacity in cap[]. Therefore the answer will be min(sum of all array elements, maximum capacity in cap[]).

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the maximum element``// after shifting operations in arr[]``int` `maxShiftArrayValue(``int` `arr[], ``int` `cap[],``                       ``int` `N)``{``    ``// Stores the sum of array element``    ``int` `sumVals = 0;``    ``for` `(``int` `i = 0; i < N; i++) {``        ``sumVals += arr[i];``    ``}` `    ``// Stores the maximum element in cap[]``    ``int` `maxCapacity = 0;` `    ``// Iterate to find maximum element``    ``for` `(``int` `i = 0; i < N; i++) {``        ``maxCapacity = max(cap[i], maxCapacity);``    ``}` `    ``// Return the resultant maximum value``    ``return` `min(maxCapacity, sumVals);``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 2, 3 };``    ``int` `cap[] = { 5, 6 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``cout << maxShiftArrayValue(arr, cap, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG``{``  ` `    ``// Function to find the maximum element``    ``// after shifting operations in arr[]``    ``public` `static` `int` `maxShiftArrayValue(``int` `arr[], ``int` `cap[], ``int` `N)``    ``{``      ` `        ``// Stores the sum of array element``        ``int` `sumVals = ``0``;``        ``for` `(``int` `i = ``0``; i < N; i++) {``            ``sumVals += arr[i];``        ``}` `        ``// Stores the maximum element in cap[]``        ``int` `maxCapacity = ``0``;` `        ``// Iterate to find maximum element``        ``for` `(``int` `i = ``0``; i < N; i++) {``            ``maxCapacity = Math.max(cap[i], maxCapacity);``        ``}` `        ``// Return the resultant maximum value``        ``return` `Math.min(maxCapacity, sumVals);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[]) {``        ``int` `arr[] = { ``2``, ``3` `};``        ``int` `cap[] = { ``5``, ``6` `};``        ``int` `N = arr.length;` `        ``System.out.println(maxShiftArrayValue(arr, cap, N));``    ``}``}` `// This code is contributed by gfgking.`

## Python3

 `# Python 3 program for the above approach` `# Function to find the maximum element``# after shifting operations in arr[]``def` `maxShiftArrayValue(arr, cap, N):``  ` `    ``# Stores the sum of array element``    ``sumVals ``=` `0``    ``for` `i ``in` `range``(N):``        ``sumVals ``+``=` `arr[i]` `    ``# Stores the maximum element in cap[]``    ``maxCapacity ``=` `0` `    ``# Iterate to find maximum element``    ``for` `i ``in` `range``(N):``        ``maxCapacity ``=` `max``(cap[i], maxCapacity)` `    ``# Return the resultant maximum value``    ``return` `min``(maxCapacity, sumVals)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``arr  ``=` `[``2``, ``3``]``    ``cap  ``=` `[``5``, ``6``]``    ``N ``=` `len``(arr)``    ``print``(maxShiftArrayValue(arr, cap, N))``    ` `    ``# This code is contributed by ipg2016107.`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG {` `    ``// Function to find the maximum element``    ``// after shifting operations in arr[]``    ``public` `static` `int` `maxShiftArrayValue(``int``[] arr,``                                         ``int``[] cap, ``int` `N)``    ``{` `        ``// Stores the sum of array element``        ``int` `sumVals = 0;``        ``for` `(``int` `i = 0; i < N; i++) {``            ``sumVals += arr[i];``        ``}` `        ``// Stores the maximum element in cap[]``        ``int` `maxCapacity = 0;` `        ``// Iterate to find maximum element``        ``for` `(``int` `i = 0; i < N; i++) {``            ``maxCapacity = Math.Max(cap[i], maxCapacity);``        ``}` `        ``// Return the resultant maximum value``        ``return` `Math.Min(maxCapacity, sumVals);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int``[] arr = { 2, 3 };``        ``int``[] cap = { 5, 6 };``        ``int` `N = arr.Length;` `        ``Console.WriteLine(maxShiftArrayValue(arr, cap, N));``    ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``

Output:
`5`

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

My Personal Notes arrow_drop_up