Horje
Minimize the maximum subarray sum with 1s and -2s

Given two integers X and Y. X and Y represent the frequency of elements 1 and -2 you have. You have to arrange all elements such that the maximum sum over all subarrays is minimized, and then find the maximum subarray sum of such rearrangement.

Examples:

Input: X = 1, Y = 1
Output: 1
Explanation: X = 1 and Y = 1 means we have one 1 and one -2. There can be only two possible arrangements with these elements as {1, -2} or {-2, 1}. In both of the cases the maximum subarray sum will be 1.

Input: X = 5, Y = 1
Output: 3
Explanation: The optimal arrangement of elements will be {1, 1, -2, 1, 1, 1}. Then, the maximum subarray sum will be 3.

Approach: To solve the problem, follow the below idea:

  • In case the number of 1’s are too great, the -2s won’t affect the subarray sum too much. Hence, we also calculate the sum of the whole array, which is (X – 2*Y) (Since the whole array is also its own subarray).
  • If there are Y number of -2s then there will be (Y+1) groups of 1s in arrangement. For example, X = 5 and Y = 2, then the arrangement will be as follows: {}, -2, {}, -2, {}. Where “{}” shows the collective group of ones (equal to (Y+1) = 3). We need to distribute X number of 1s in (Y+1) spaces equally. So that the distribution will be as Ceil (X/ (Y+1)).
  • Then, the maximum subarray sum will be max of both of these as max((X – 2*Y), Ceil (X/ (Y+1))).
  • The problem is observation based. The main idea is, for any subarray to have its sum minimized, we need to distribute the 1s among the -2s such that the count of 1s in between -2s are smallest. This can only be done by optimally dividing the number of 1’s between the 2’s.

Illustration:

Let us understand it with some examples and find some observations:

  • Example 1: X = 5, Y = 2
    • We have five 1s and two -2s.
    • We need to distribute 1s in between -2s, then the optimal arrangement will be as follows: {1, 1, -2, 1, -2, 1, 1}. The maximum subarray sum will be 2 in this case. Max((X – 2*Y), Ciel (X/ (Y+1))) = 2.
  • Example 2: X = 5, Y = 1
    • We have five 1s and one -2s.
    • The optimal arrangement will be as follows: {1, 1, -2, 1, 1, 1}. The maximum subarray sum will be 3 in this case. Ceil(5/(1+1)) = 3. Max((X – 2*Y), Ciel (X/ (Y+1))) = 3.
  • Example 3: X = 1, Y = 3
    • We have one 1 and three -2s.
    • The optimal arrangement will be as follows: {-2, -2, -2, 1}. The maximum subarray sum will be 1. Max((X – 2*Y), Ciel (X/ (Y+1))) = 1.

Step-by-step algorithm:

  • Declare two variables let say A and B.
  • Initialize A with Ciel (X/ (Y+1)).
  • Initialize B with sum of all elements as (X – 2*Y).
  • Output Max (A, B).

Below is the implementation of the algorithm:

C++

#include <iostream>
#include <cmath>
 
// Function to output the maximum subarray sum after
// optimal arrangement
void FindMaxSum(double X, double Y)
{
    double A = std::ceil(X / (Y + 1.0));
    double B = (X - 2 * Y);
    std::cout << std::max(A, B) << std::endl;
}
 
// Driver Function
int main()
{
    // Inputs
    double X = 5, Y = 2;
 
    // Function_call
    FindMaxSum(X, Y);
 
    return 0;
}
 
 
// This code is contributed by akshitaguprzj3

Java

// Java code to implement the approach
 
import java.util.*;
 
// Driver Class
public class Main {
 
    // Driver Function
    public static void main(String[] args)
    {
        // Inputs
        double X = 5, Y = 2;
 
        // Function_call
        FindMaxSum(X, Y);
    }
 
    // Function to output the maximum subarray sum after
    // optimal arrangement
    public static void FindMaxSum(Double X, Double Y)
    {
        double A = Math.ceil(X / (Y + 1.0));
        double B = (X - 2 * Y);
        System.out.println(Math.max(A, B));
    }
}

Python3

# Python Implementation
 
import math
 
def find_max_sum(X, Y):
    A = math.ceil(X / (Y + 1.0))
    B = X - 2 * Y
    return max(A, B)
 
X = 5
Y = 2
 
max_sum = find_max_sum(X, Y)
print(max_sum)
 
# This code is contributed by Sakshi

C#

// C# code to implement the approach
using System;
 
// Driver Class
public class GFG
{
    // Driver Function
    public static void Main(string[] args)
    {
        // Inputs
        double X = 5, Y = 2;
 
        // Function_call
        FindMaxSum(X, Y);
    }
 
    // Function to output the maximum subarray sum after
    // optimal arrangement
    public static void FindMaxSum(double X, double Y)
    {
        double A = Math.Ceiling(X / (Y + 1.0));
        double B = (X - 2 * Y);
        Console.WriteLine(Math.Max(A, B));
    }
}

Javascript

// Function to output the maximum subarray sum after optimal arrangement
function findMaxSum(X, Y) {
    let A = Math.ceil(X / (Y + 1));
    let B = X - 2 * Y;
    console.log(Math.max(A, B));
}
 
// Driver Function
function main() {
    // Inputs
    let X = 5, Y = 2;
 
    // Function call
    findMaxSum(X, Y);
}
 
// Invoke main function
main();

Output

2.0





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




Reffered: https://www.geeksforgeeks.org


DSA

Related
Does a Data Scientist/Machine Learning Engineer require in depth knowledge of Data Structures and Algorithms? Does a Data Scientist/Machine Learning Engineer require in depth knowledge of Data Structures and Algorithms?
Find longest substring of S1 that matches in S2 with a given cost Find longest substring of S1 that matches in S2 with a given cost
Find the winner by incrementing co-ordinates till Euclidean distance &lt;= D Find the winner by incrementing co-ordinates till Euclidean distance &lt;= D
Modify shortest distance between source and destination to K Modify shortest distance between source and destination to K
Minimum number of keypresses to type the given string Minimum number of keypresses to type the given string

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13