Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmMaximum possible value of array elements that can be made based on...

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




// 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)

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments