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

Given two arrays **arr[]** and **cap[]** both consisting of **N** positive integers such that the **i ^{th}** 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:5Explanation:

Following operations are performed to maximize value of any element in arr[]:Operation 1:Decrease arr[0] by 2 and increase arr[1] 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 <bits/stdc++.h>` `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[0]);` ` ` `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

`<script>` ` ` `// JavaScript Program to implement` ` ` `// the above approach` ` ` `// Function to find the maximum element` ` ` `// after shifting operations in arr[]` ` ` `function` `maxShiftArrayValue(arr, cap, N)` ` ` `{` ` ` ` ` `// Stores the sum of array element` ` ` `let sumVals = 0;` ` ` `for` `(let i = 0; i < N; i++) {` ` ` `sumVals += arr[i];` ` ` `}` ` ` `// Stores the maximum element in cap[]` ` ` `let maxCapacity = 0;` ` ` `// Iterate to find maximum element` ` ` `for` `(let i = 0; i < N; i++) {` ` ` `maxCapacity = Math.max(cap[i], maxCapacity);` ` ` `}` ` ` `// Return the resultant maximum value` ` ` `return` `Math.min(maxCapacity, sumVals);` ` ` `}` ` ` `// Driver Code` ` ` `let arr = [2, 3];` ` ` `let cap = [5, 6];` ` ` `let N = arr.length` ` ` `document.write(maxShiftArrayValue(arr, cap, N));` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output:**

5

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