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:
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[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++
#include <bits/stdc++.h>
using namespace std;
int maxShiftArrayValue( int arr[], int cap[],
int N)
{
int sumVals = 0;
for ( int i = 0; i < N; i++) {
sumVals += arr[i];
}
int maxCapacity = 0;
for ( int i = 0; i < N; i++) {
maxCapacity = max(cap[i], maxCapacity);
}
return min(maxCapacity, sumVals);
}
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
class GFG
{
public static int maxShiftArrayValue( int arr[], int cap[], int N)
{
int sumVals = 0 ;
for ( int i = 0 ; i < N; i++) {
sumVals += arr[i];
}
int maxCapacity = 0 ;
for ( int i = 0 ; i < N; i++) {
maxCapacity = Math.max(cap[i], maxCapacity);
}
return Math.min(maxCapacity, sumVals);
}
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));
}
}
|
Python3
def maxShiftArrayValue(arr, cap, N):
sumVals = 0
for i in range (N):
sumVals + = arr[i]
maxCapacity = 0
for i in range (N):
maxCapacity = max (cap[i], maxCapacity)
return min (maxCapacity, sumVals)
if __name__ = = '__main__' :
arr = [ 2 , 3 ]
cap = [ 5 , 6 ]
N = len (arr)
print (maxShiftArrayValue(arr, cap, N))
|
C#
using System;
class GFG {
public static int maxShiftArrayValue( int [] arr,
int [] cap, int N)
{
int sumVals = 0;
for ( int i = 0; i < N; i++) {
sumVals += arr[i];
}
int maxCapacity = 0;
for ( int i = 0; i < N; i++) {
maxCapacity = Math.Max(cap[i], maxCapacity);
}
return Math.Min(maxCapacity, sumVals);
}
public static void Main( string [] args)
{
int [] arr = { 2, 3 };
int [] cap = { 5, 6 };
int N = arr.Length;
Console.WriteLine(maxShiftArrayValue(arr, cap, N));
}
}
|
Javascript
<script>
function maxShiftArrayValue(arr, cap, N)
{
let sumVals = 0;
for (let i = 0; i < N; i++) {
sumVals += arr[i];
}
let maxCapacity = 0;
for (let i = 0; i < N; i++) {
maxCapacity = Math.max(cap[i], maxCapacity);
}
return Math.min(maxCapacity, sumVals);
}
let arr = [2, 3];
let cap = [5, 6];
let N = arr.length
document.write(maxShiftArrayValue(arr, cap, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...