Open In App

Maximize minimum element of an Array using operations

Last Updated : 31 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an array A[] of N integers and two integers X and Y (X ≤ Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ≤ Y.

Examples:

Input: N= 3, A[] = {1, 5, 9}, X = 2, Y = 2
Output: 5
Explanation: We will perform the operation twice as follows:

  • Add X to first element and subtract Y from third element. Array becomes – {3, 5, 7}
  • Again, add X to first element and subtract Y from third element. Array becomes – {5, 5, 5}

The minimum element is 5. It can be checked that after performing operation with any other indices or any more number of times, the minimum element cannot be made greater than 5.

Input: N = 3, A[] = {11, 1, 2}, X = 2, Y = 3
Output: 3
Explanation: We will perform the operation twice as follows:

  • Add X to second element and subtract Y from first element. Array becomes – {8, 3, 2}
  • Add X to third element and subtract Y from first element. Array becomes – {5, 3, 4}

The minimum element is 3. It can be checked that after performing operation with any other indices or any more number of times, the minimum element cannot be made greater than 3.

Approach: To solve the problem follow the below idea:

The idea is to use binary search. Initialize two variables lo and hi to the minimum and maximum element of the array, respectively. Now, calculate mid value between lo and hi using binary search. Iterate the array to count the number of additions and subtractions required to make all elements either greater than or equal to mid, using X and Y respectively. If the total number of subtractions required is greater than or equal to the total number of additions required, the update lo=mid and continues the binary search, else update hi=mid-1 and continue the search. When lo and hi become equal, return this value as the answer.

Below are the steps for the above approach:

  • Initialize the lower bound (say “lo”) and upper bound(say “hi”) for binary search with minimum and maximum elements of the array respectively.
  • In each iteration of the binary search, initialize two variables, say “add” and “subtract” with 0.
  • Iterate through the array.
  • If the current element of the array is greater than or equal to mid, add (A[i]-mid)/Y to “subtract”.
  • Else, add (mid-A[i]+X-1)/X to “add”.
  • If “subtract” is greater than or equal to “add”, set lo=mid.
  • Else, set hi=mid-1.
  • When lo becomes greater than or equal to hi, return lo.

Below is the code for the above approach:

C++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
// Function to Maximize the minimum element
// of array by adding and subtracting X and
// Y from distinct elements any number
// of times
int maximizeMinimumElement(int N, int A[], int X, int Y)
{
 
    // Initializing lo and hi
    int lo = *min_element(A, A + N);
    int hi = *max_element(A, A + N);
 
    while (lo < hi) {
 
        // Initializing mid, add and
        // subtract
        int mid = lo + (hi - lo + 1) / 2;
        int add = 0, subtract = 0;
 
        // Iterating through the array
        for (int i = 0; i < N; i++) {
 
            // If mid is less than or equal
            // to A[i], accordingly increase
            // the value of subtract
            if (mid <= A[i]) {
                subtract += (A[i] - mid) / Y;
            }
 
            // Else, accordingly increase
            // the value of add
            else {
                add += (mid - A[i] + X - 1) / X;
            }
        }
 
        // If subtract is greater than or
        // equal to add, set lo = mid
        if (subtract >= add) {
            lo = mid;
        }
 
        // Else set hi = mid-1
        else {
            hi = mid - 1;
        }
    }
 
    // Return the answer
    return lo;
}
 
// Driver code
int32_t main()
{
    int N = 3, X = 2, Y = 2;
    int A[] = { 1, 5, 9 };
 
    // Function Call
    cout << maximizeMinimumElement(N, A, X, Y);
    return 0;
}


Java




// Java code for the above approach
import java.util.*;
 
class GFG {
 
    // Function to Maximize the minimum element
    // of array by adding and subtracting X and
    // Y from distinct elements any number
    // of times
    public static int maximizeMinimumElement(int N, int A[],
                                             int X, int Y)
    {
 
        // Initializing lo and hi
        int lo = Arrays.stream(A).min().getAsInt();
        int hi = Arrays.stream(A).max().getAsInt();
 
        while (lo < hi) {
 
            // Initializing mid, add and
            // subtract
            int mid = lo + (hi - lo + 1) / 2;
            int add = 0, subtract = 0;
 
            // Iterating through the array
            for (int i = 0; i < N; i++) {
 
                // If mid is less than or equal
                // to A[i], accordingly increase
                // the value of subtract
                if (mid <= A[i]) {
                    subtract += (A[i] - mid) / Y;
                }
 
                // Else, accordingly increase
                // the value of add
                else {
                    add += (mid - A[i] + X - 1) / X;
                }
            }
 
            // If subtract is greater than or
            // equal to add, set lo = mid
            if (subtract >= add) {
                lo = mid;
            }
 
            // Else set hi = mid-1
            else {
                hi = mid - 1;
            }
        }
 
        // Return the answer
        return lo;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int N = 3, X = 2, Y = 2;
        int A[] = { 1, 5, 9 };
 
        // Function Call
        System.out.println(
            maximizeMinimumElement(N, A, X, Y));
    }
}
// This code is contributed by prasad264


Python3




def maximizeMinimumElement(N, A, X, Y):
    # Initializing lo and hi
    lo = min(A)
    hi = max(A)
 
    while lo < hi:
        # Initializing mid, add and subtract
        mid = lo + (hi - lo + 1) // 2
        add = 0
        subtract = 0
 
        # Iterating through the array
        for i in range(N):
            # If mid is less than or equal to A[i],
            # accordingly increase the value of subtract
            if mid <= A[i]:
                subtract += (A[i] - mid) // Y
            # Else, accordingly increase the value of add
            else:
                add += (mid - A[i] + X - 1) // X
 
        # If subtract is greater than or equal to add, set lo = mid
        if subtract >= add:
            lo = mid
        # Else set hi = mid-1
        else:
            hi = mid - 1
 
    # Return the answer
    return lo
 
# Driver code
N = 3
X = 2
Y = 2
A = [1, 5, 9]
 
# Function Call
print(maximizeMinimumElement(N, A, X, Y))


C#




using System;
 
class GFG
{
    static int MaximizeMinimumElement(int N, int[] A, int X, int Y)
    {
        // Initializing lo and hi
        int lo = A[0];
        int hi = A[0];
         
        // Find the minimum and maximum values in the array
        for (int i = 1; i < N; i++)
        {
            lo = Math.Min(lo, A[i]);
            hi = Math.Max(hi, A[i]);
        }
 
        while (lo < hi)
        {
            // Initializing mid, add and subtract
            int mid = lo + (hi - lo + 1) / 2;
            int add = 0;
            int subtract = 0;
 
            // Iterating through the array
            for (int i = 0; i < N; i++)
            {
                // If mid is less than or equal to A[i],
                // accordingly increase the value of subtract
                if (mid <= A[i])
                {
                    subtract += (A[i] - mid) / Y;
                }
                // Else, accordingly increase the value of add
                else
                {
                    add += (mid - A[i] + X - 1) / X;
                }
            }
 
            // If subtract is greater than or equal to add, set lo = mid
            if (subtract >= add)
            {
                lo = mid;
            }
            // Else set hi = mid-1
            else
            {
                hi = mid - 1;
            }
        }
 
        // Return the answer
        return lo;
    }
 
    static void Main(string[] args)
    {
        int N = 3;
        int X = 2;
        int Y = 2;
        int[] A = new int[] { 1, 5, 9 };
 
        // Function Call
        Console.WriteLine(MaximizeMinimumElement(N, A, X, Y));
    }
}


Javascript




// JavaScript code for the above approach
 
// Function to maximize the minimum element
// of an array by adding and subtracting X and
// Y from distinct elements any number of times
function maximizeMinimumElement(N, A, X, Y) {
     
    // Initializing lo and hi
    let lo = Math.min(...A);
    let hi = Math.max(...A);
 
    while (lo < hi) {
        // Initializing mid, add and
        // subtract
        let mid = lo + Math.floor((hi - lo + 1) / 2);
        let add = 0;
        let subtract = 0;
 
        // Iterating through the array
        for (let i = 0; i < N; i++) {
            // If mid is less than or equal
            // to A[i], accordingly increase
            // the value of subtract
            if (mid <= A[i]) {
                subtract += Math.floor((A[i] - mid) / Y);
            }
             
            // Else, accordingly increase
            // the value of add
            else {
                add += Math.floor((mid - A[i] + X - 1) / X);
            }
        }
 
        // If subtract is greater than or
        // equal to add, set lo = mid
        if (subtract >= add) {
            lo = mid;
        }
         
        // Else set hi = mid-1
        else {
            hi = mid - 1;
        }
    }
 
    // Return the answer
    return lo;
}
 
// Driver code
let N = 3;
let X = 2;
let Y = 2;
let A = [1, 5, 9];
 
// Function call
console.log(maximizeMinimumElement(N, A, X, Y));


Output

5

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



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: