Maximum subarray sum modulo M

Bhoot

Most of us are familiar with the maximum sum subarray problem. I came across a variant of this problem which asks the programmer to output the maximum of all subarray sums modulo some number M.

The naive approach to solve this variant would be to find all possible subarray sums (which would be of the order of N^2 where N is the size of the array). Of course, this is not good enough. The question is - how can we do better?

Example: Let us consider the following array:

6 6 11 15 12 1

Let M = 13. In this case, subarray 6 6 (or 12 or 6 6 11 15 or 11 15 12) will yield maximum sum ( = 12 ).

Pham Trung

We can do this as follow:

Maintaining an array sum which at index ith, it contains the modulus sum from 0 to ith.

For each index ith, we need to find the maximum sub sum that end at this index:

For each subarray (start + 1 , i ), we know that the mod sum of this sub array is

int a = (sum[i] - sum[start] + M) % M

So, we can only achieve a sub-sum larger than sum[i] if sum[start] is larger than sum[i] and as close to sum[i] as possible.

This can be done easily if you using a binary search tree.

Pseudo code:

int[] sum;
sum[0] = A[0];
Tree tree;
tree.add(sum[0]);
int result = sum[0];
for(int i = 1; i < n; i++){
    sum[i] = sum[i - 1] + A[i];
    sum[i] %= M;
    int a = tree.getMinimumValueLargerThan(sum[i]);
    result = max((sum[i] - a + M) % M, result);
    tree.add(sum[i]);
}
print result;

Time complexity :O(n log n)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

find the maximum value of sum of subarray modulo M

From Dev

Maximum Sum SubArray

From Dev

Maximum subarray sum such that sum is odd

From Dev

Maximum-sum subarray given constraints on indices

From Dev

Unable to understand algorithm to find maximum sum of Subarray

From Dev

Maximum sum in a contiguous subarray of a given 2D array

From Dev

To return subarray as well alongside maximum sum of subarray using kadane's algorithm

From Dev

Maximum subarray algorithm in clojure

From Dev

Maximum Product Subarray issue

From Dev

"Maximum Sum mod M" ranges in an array: sum and count

From Dev

Continuous Subarray sum

From Dev

Sum of subarray values

From Dev

Finding subarray with given sum

From Dev

Variation of a Maximum_Subarray_Problem

From Dev

Maximum subarray with given number of elements

From Dev

finding the value of maximum xor subarray

From Dev

Maximise the profit of shares (Maximum subarray)

From Dev

Algorithm to find Sum of the first r binomial coefficients for fixed n modulo m

From Dev

Finding minimal absolute sum of a subarray

From Dev

a dynamic program about subarray sum

From Dev

Largest subarray with sum equal to 0

From Dev

Max Sum Subarray - Divide and Conquer

From Dev

Range of maximum product subarray (Kadane algorithm variant)

From Dev

Using induction to prove linear maximum subarray algorithm

From Dev

Print SubArray of Maximum Contiguous product in Array

From Dev

Using induction to prove linear maximum subarray algorithm

From Dev

Maximum Subarray (Kadane's algorithm approach)

From Dev

hardware implementation of Modulo m adder

From Dev

given array A, form array M such that sum of products (a1*m1+...+an*mn) is maximum

Related Related

HotTag

Archive