Given an array **arr[]** consisting of positive numbers, the task is to find the maximum range [L, R] whose sum is divisible by **M**. If there is no range present return -1.

**Examples:**

Input:arr[] = {3, 7, 5, 2, 5, 10}, M = 3

Output:1 3

Explanation:Sum of numbers from 1 to 3 is 3+7+5 which is 15.

Input :

A[] = {4, 8, 12, 16, 20}

M = 11

Output :

-1

**Naive Approach:** A naive approach is that we can iterate through all possible L and R in the array and check if the sum is divisible. If it is, then store the length of the array.

**Efficient Approach: **The idea here is to use a prefix sum array.

- Initially, the prefix sum of the values is calculated.
- Once the prefix sum is calculated, every value is replaced by the value modulo
**M**. - The indices where the final value is equal is the range where the sum is perfectly divisible by M.
- The maximum range of the same values is found.

Below is the implementation of the above approach,

`// C++ implementation of the above approach. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the maximum range ` `// whose sum is divisible by M. ` `pair<` `int` `, ` `int` `> maxrange(` `int` `n, ` ` ` `int` `a[], ` `int` `m) ` `{ ` ` ` `int` `pre[n + 5]; ` ` ` `map<` `int` `, set<` `int` `> > mpp; ` ` ` `int` `value, l, r, lans = -1, ` ` ` `rans = -1, ans = 0; ` ` ` ` ` `// Calculate the prefix sum ` ` ` `pre[0] = a[0]; ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `pre[i] = a[i] + pre[i - 1]; ` ` ` `} ` ` ` ` ` `// Replacing the values with modulo M ` ` ` `// and inserting the indices into the map ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `pre[i] = pre[i] % m; ` ` ` `mpp[pre[i]].insert(i); ` ` ` `} ` ` ` ` ` `// Calculate the range [L, R] ` ` ` `for` `(` `auto` `it = mpp.begin(); it != mpp.end(); it++) { ` ` ` `if` `(it->first == 0) { ` ` ` `value = *((it->second).rbegin()) + 1; ` ` ` `l = 1; ` ` ` `r = value; ` ` ` `} ` ` ` `else` `{ ` ` ` `value = *((it->second).rbegin()) ` ` ` `- *((it->second.begin())); ` ` ` `l = *((it->second.begin())) + 2; ` ` ` `r = *((it->second).rbegin()) + 1; ` ` ` `} ` ` ` `if` `(value > ans && l <= r) { ` ` ` `ans = value; ` ` ` `lans = l; ` ` ` `rans = r; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `make_pair(lans, rans); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A[] = { 3, 7, 5, 2, 5, 10 }; ` ` ` `int` `N = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` `int` `M = 3; ` ` ` `pair<` `int` `, ` `int` `> value = maxrange(N, A, M); ` ` ` `if` `(value.first == -1) { ` ` ` `cout << -1 << ` `"\n"` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << value.first << ` `" "` ` ` `<< value.second << ` `"\n"` `; ` ` ` `} ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 3

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:

- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find a subarray whose sum is divisible by size of the array
- Find prime factors of Array elements whose sum of exponents is divisible by K
- Subsequences of size three in an array whose sum is divisible by m
- Count pairs in array whose sum is divisible by 4
- Count pairs in array whose sum is divisible by K
- Longest subsequence whose sum is divisible by a given number
- Length of longest subarray whose sum is not divisible by integer K
- Check if an array can be divided into pairs whose sum is divisible by k
- Maximum sum of two elements whose digit sum is equal
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Find the root of the sub-tree whose weighted sum XOR with X is maximum
- Find the node whose sum with X has maximum set bits
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Count sub-arrays whose product is divisible by k
- Count pairs in an array whose absolute difference is divisible by K

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.