Given an array A[] consisting of N positive integers, the task is to find the maximum possible value of:
F(M) = M % A[0] + M % A[1] + …. + M % A[N -1] where M can be any integer value
Examples:
Input: arr[] = {3, 4, 6}
Output: 10
Explanation:
The maximum sum occurs for M = 11.
(11 % 3) + (11 % 4) + (11 % 6) = 2 + 3 + 5 = 10Input: arr[] = {2, 5, 3}
Output:7
Explanation:
The maximum sum occurs for M = 29.
(29 % 2) + (29 % 5) + (29 % 3) = 1 + 4 + 2 = 7.
Approach:
Follow the steps below to solve the problem:
- Calcaulate the LCM of all array elements.
- If M is equal to the LCM of the array, then F(M) = 0 i.e. the minimum possible value of the F(M). This is because, M % a[i] will always be 0 for every ith index.
- For M = LCM of array elements – 1, F(M) is maximized. This is because, M % a[i] is equal to a[i] – 1 for every ith index, which is the maximum possible.
- Hence, the maximum possible value of F(M) can be Sum of array elements – N.
Below is the implementation of the above approach:
C++
// C++ program to find the // maximum sum of modulus // with every array element #include <bits/stdc++.h> using namespace std; // Function to return the // maximum sum of modulus // with every array element int maxModulosum(int a[], int n) { int sum = 0; // Sum of array elements for (int i = 0; i < n; i++) { sum += a[i]; } // Return the answer return sum - n; } // Driver Program int main() { int a[] = { 3, 4, 6 }; int n = sizeof(a) / sizeof(a[0]); cout << maxModulosum(a, n); return 0; } |
Java
// Java program to find the maximum // sum of modulus with every array // element import java.io.*; class GFG{ // Function to return the maximum // sum of modulus with every array // element static int maxModulosum(int a[], int n) { int sum = 0; // Sum of array elements for(int i = 0; i < n; i++) { sum += a[i]; } // Return the answer return sum - n; } // Driver Code public static void main (String[] args) { int a[] = new int[]{ 3, 4, 6 }; int n = a.length; System.out.println(maxModulosum(a, n)); } } // This code is contributed by Shubham Prakash |
Python3
# Python3 program to find the # maximum sum of modulus # with every array element # Function to return the # maximum sum of modulus # with every array element def maxModulosum(a, n): sum1 = 0; # Sum of array elements for i in range(0, n): sum1 += a[i]; # Return the answer return sum1 - n; # Driver Code a = [ 3, 4, 6 ]; n = len(a); print(maxModulosum(a, n)); # This code is contributed by Code_Mech |
C#
// C# program to find the maximum // sum of modulus with every array // element using System; class GFG{ // Function to return the maximum // sum of modulus with every array // element static int maxModulosum(int []a, int n) { int sum = 0; // Sum of array elements for(int i = 0; i < n; i++) { sum += a[i]; } // Return the answer return sum - n; } // Driver Code public static void Main(String[] args) { int []a = new int[]{ 3, 4, 6 }; int n = a.Length; Console.Write(maxModulosum(a, n)); } } // This code is contributed // by shivanisinghss2110 |
10
Time Complexity: O(N)
Auxiliary Space: O(1)
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:
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Finding 'k' such that its modulus with each array element is same
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Modulus of two float or double numbers
- Modulus of a Complex Number
- Maximize array sum by X increments when each element is divided by 10
- Maximize the number of indices such that element is greater than element to its left
- Replace every element in a circular array by sum of next K elements
- Maximize the sum of sum of the Array by removing end elements
- Divide every element of one array by other array elements
- Nearest prime number in the array of every array element
- Count all distinct pairs of repeating elements from the array for every array element
- Maximize the sum of array by multiplying prefix of array with -1
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Maximize the maximum subarray sum after removing atmost one element
- Remove an element to maximize the GCD of the given array
- Maximize the first element of the array such that average remains constant
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Maximize the last Array element as per the given conditions
- Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
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.

