Related Articles

# Maximize Modulo Sum possible from an array

• Difficulty Level : Hard
• Last Updated : 06 Jul, 2021

Given an array arr[] consisting of N positive integers, the task is to find the maximum value of ∑(M mod arr[i]), where arr[i] is any array element, for a non-negative integer M.

Examples:

Input: arr[] = {3, 4, 6}
Output: 10
Explanation: For M = 11, (11 mod 3) + (11 mod 4) + (11 mod 6) =10

Input: arr[]={7, 46, 11, 20, 11}
Output: 90

Approach: Follow the steps below to solve the problem:

• Since A mod B is the remainder when A divided by B, then the maximum value of the expression ∑(M mod arr[i]) is:

(M mod Arr) + (M mod Arr) +. . . + (M mod Arr[N-1]) = (Arr − 1) + (Arr − 1) + · · · + (Arr[N-1]− 1)

• Considering K = Arr × Arr × ···· × Arr[n – 1], then (K mod Arr[i]) = 0 for each i in range [0, N – 1]
• Therefore, ((K − 1) mod Arr[i]) = Arr[i] − 1. Therefore, for M = K – 1, the optimal result can be obtained.

Below is the implementation of the above approach :

## C++

 `// C++ Program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to calculate maximum modulo``// sum possible from a given array``int` `MaximumModuloSum(``int` `Arr[], ``int` `N)``{``    ``// Stores the sum``    ``int` `sum = 0;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {``        ``sum += Arr[i] - 1;``    ``}` `    ``return` `sum;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 3, 4, 6 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``cout << MaximumModuloSum(arr, N);` `    ``return` `0;``}`

## Java

 `// Java Program to implement``// the above approach` `import` `java.io.*;` `class` `GFG {``    ``public` `static` `int` `MaximumModuloSum(``int` `Arr[], ``int` `N)``    ``{``      ` `        ``// Stores the sum``        ``int` `sum = ``0``;` `        ``// Traverse the array``        ``for` `(``int` `i = ``0``; i < N; i++) {``            ``sum += Arr[i] - ``1``;``        ``}``        ``return` `sum;``    ``}``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = { ``3``, ``4``, ``6` `};``        ``int` `N = ``3``;``        ``System.out.println(MaximumModuloSum(arr, N));``    ``}``}` `// This code is contributed by aditya7409.`

## Python3

 `# Python 3 Program to implement``# the above approach` `# Function to calculate maximum modulo``# sum possible from a given array``def` `MaximumModuloSum( Arr, N):` `    ``# Stores the sum``    ``sum` `=` `0``;` `    ``# Traverse the array``    ``for` `i ``in` `range``( N ):``        ``sum` `+``=` `Arr[i] ``-` `1``;``    ` `    ``return` `sum``;` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``  ` `    ``arr ``=` `[ ``3``, ``4``, ``6` `];``    ``N ``=` `len``(arr)``    ``print``(MaximumModuloSum(arr, N))` `    ``# This code is contributed by chitranayal.`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG``{` `  ``public` `static` `int` `MaximumModuloSum(``int``[] Arr, ``int` `N)``  ``{` `    ``// Stores the sum``    ``int` `sum = 0;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {``      ``sum += Arr[i] - 1;``    ``}``    ``return` `sum;``  ``}` `  ``// Driver code``  ``static` `void` `Main()``  ``{``    ``int``[] arr = { 3, 4, 6 };``    ``int` `N = 3;``    ``Console.WriteLine(MaximumModuloSum(arr, N));``  ``}``}` `// This code is contributed by susmitakundugoaldanga.`

## Javascript

 ``
Output:
`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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up