Given an array of **n** elements and an integer m. The task is to find the maximum value of the sum of its subarray modulo m i.e find the sum of each subarray mod m and print the maximum value of this modulo operation.

**Examples:**

Input : arr[] = { 3, 3, 9, 9, 5 } m = 7 Output : 6 All sub-arrays and their value: { 9 } => 9%7 = 2 { 3 } => 3%7 = 3 { 5 } => 5%7 = 5 { 9, 5 } => 14%7 = 2 { 9, 9 } => 18%7 = 4 { 3, 9 } => 12%7 = 5 { 3, 3 } => 6%7 = 6 { 3, 9, 9 } => 21%7 = 0 { 3, 3, 9 } => 15%7 = 1 { 9, 9, 5 } => 23%7 = 2 { 3, 3, 9, 9 } => 24%7 = 3 { 3, 9, 9, 5 } => 26%7 = 5 { 3, 3, 9, 9, 5 } => 29%7 = 1 Input : arr[] = {10, 7, 18} m = 13 Output : 12 The subarray {7, 18} has maximum sub-array sum modulo 13.

**Method 1 (Brute Force):**

Use brute force to find all the subarrays of the given array and find sum of each subarray mod m and keep track of maximum.

**Method 2 (efficient approach):**

The idea is to compute prefix sum of array. We find maximum sum ending with every index and finally return overall maximum. To find maximum sum ending at index at index, we need to find the starting point of maximum sum ending with i. Below steps explain how to find the starting point.

Let prefix sum for index i be prefix_{i}, i.e., prefix_{i}= (arr[0] + arr[1] + .... arr[i] ) % m Let maximum sum ending with i be, maxSum_{i}. Let this sum begins with index j. maxSum_{i}= (prefix_{i}- prefix_{j}+ m) % m From above expression it is clear that the value of maxSum_{i}becomes maximum when prefix_{j}is greater than prefix_{i}and closest to prefix_{i}

We mainly have two operations in above algorithm.

- Store all prefixes.
- For current prefix, prefix
_{i}, find the smallest value greater than or equal to prefix_{i}+ 1.

For above operations, a self-balancing-binary-search-trees like AVL Tree, Red-Black Tree, etc are best suited. In below implementation we use set in STL which implements a self-balancing-binary-search-tree.

Below is the implementation of this approach:

## C++

`// C++ program to find sub-array having maximum ` `// sum of elements modulo m. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the maximum sum subarray mod m. ` `int` `maxSubarray(` `int` `arr[], ` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `x, prefix = 0, maxim = 0; ` ` ` ` ` `set<` `int` `> S; ` ` ` `S.insert(0); ` ` ` ` ` `// Traversing the array. ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// Finding prefix sum. ` ` ` `prefix = (prefix + arr[i])%m; ` ` ` ` ` `// Finding maximum of prefix sum. ` ` ` `maxim = max(maxim, prefix); ` ` ` ` ` `// Finding iterator pointing to the first ` ` ` `// element that is not less than value ` ` ` `// "prefix + 1", i.e., greater than or ` ` ` `// equal to this value. ` ` ` `auto` `it = S.lower_bound(prefix+1); ` ` ` ` ` `if` `(it != S.end()) ` ` ` `maxim = max(maxim, prefix - (*it) + m ); ` ` ` ` ` `// Inserting prefix in the set. ` ` ` `S.insert(prefix); ` ` ` `} ` ` ` ` ` `return` `maxim; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 3, 3, 9, 9, 5 }; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `int` `m = 7; ` ` ` `cout << maxSubarray(arr, n, m) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find sub-array ` `# having maximum sum of elements modulo m. ` ` ` `# Return the maximum sum subarray mod m. ` `def` `maxSubarray(arr, n, m): ` ` ` ` ` `x ` `=` `0` ` ` `prefix ` `=` `0` ` ` `maxim ` `=` `0` ` ` ` ` `S ` `=` `set` `() ` ` ` `S.add(` `0` `) ` ` ` ` ` `# Traversing the array. ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# Finding prefix sum. ` ` ` `prefix ` `=` `(prefix ` `+` `arr[i]) ` `%` `m ` ` ` ` ` `# Finding maximum of prefix sum. ` ` ` `maxim ` `=` `max` `(maxim, prefix) ` ` ` ` ` `# Finding iterator poing to the first ` ` ` `# element that is not less than value ` ` ` `# "prefix + 1", i.e., greater than or ` ` ` `# equal to this value. ` ` ` `it ` `=` `0` ` ` `for` `i ` `in` `S: ` ` ` `if` `i >` `=` `prefix ` `+` `1` `: ` ` ` `it ` `=` `i ` ` ` `if` `(it !` `=` `0` `) : ` ` ` `maxim ` `=` `max` `(maxim, prefix ` `-` `it ` `+` `m ) ` ` ` ` ` `# adding prefix in the set. ` ` ` `S.add(prefix) ` ` ` ` ` `return` `maxim ` ` ` `# Driver Code ` `arr ` `=` `[` `3` `, ` `3` `, ` `9` `, ` `9` `, ` `5` `] ` `n ` `=` `5` `m ` `=` `7` `print` `(maxSubarray(arr, n, m)) ` ` ` `# This code is contributed by ` `# Shubham Singh(SHUBHAMSINGH10) ` |

*chevron_right*

*filter_none*

**Output:**

6

**Reference: **

http://stackoverflow.com/questions/31113993/maximum-subarray-sum-modulo-m

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum length of subarray such that sum of the subarray is even
- Maximum length of subarray such that all elements are equal in the subarray
- Maximum modulo of all the pairs of array where arr[i] >= arr[j]
- Maximum frequency of a remainder modulo 2
^{i} - Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Maximum subarray sum in O(n) using prefix sum
- Find sum of modulo K of first N natural number
- Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
- Sum of two numbers modulo M
- Sum of the natural numbers (up to N) whose modulo with K yield R
- Maximum subarray size, such that all subarrays of that size have sum less than k
- Maximum subarray sum in an array created after repeated concatenation
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Maximum sum subarray such that start and end values are same
- Maximize the maximum subarray sum after removing atmost one element
- Minimum size Subarray with maximum sum in non-increasing order
- Maximum sum subarray of size range [L, R]
- Maximum Subarray Sum possible by replacing an Array element by its Square
- Compute n! under modulo p
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)