Maximize the sum of modulus with every Array element

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 = 10
Input: 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:
- Calculate 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 elementint 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 Programint 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// elementimport java.io.*; class GFG{ // Function to return the maximum// sum of modulus with every array// elementstatic 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 elementdef 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 Codea = [ 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// elementusing System;class GFG{ // Function to return the maximum// sum of modulus with every array// elementstatic 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 |
Javascript
<script> // Javascript program to find the // maximum sum of modulus // with every array element // Function to return the // maximum sum of modulus // with every array element function maxModulosum(a, n) { let sum = 0; // Sum of array elements for (let i = 0; i < n; i++) { sum += a[i]; } // Return the answer return sum - n; } let a = [ 3, 4, 6 ]; let n = a.length; document.write(maxModulosum(a, n));</script> |
Output:
10
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



