Related Articles
Maximize the sum of modulus with every Array element
• Last Updated : 24 Mar, 2021

Given an array A[] consisting of N positive integers, the task is to find the maximum possible value of:

F(M) = M % A + M % A + …. + 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:
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:

1. Calcaulate the LCM of all array elements.
2. 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.
3. 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.
4. 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 ``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);``    ``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`

## Javascript

 ``
Output:
`10`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up