Maximize the sum of modulus with every Array element Last Updated : 14 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: 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 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 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) Comment More infoAdvertise with us Next Article Maximize the sum of array by multiplying prefix of array with -1 A anay07 Follow Improve Article Tags : Greedy Mathematical Write From Home DSA Arrays Modular Arithmetic LCM +3 More Practice Tags : ArraysGreedyMathematicalModular Arithmetic Similar Reads Maximize the sum of sum of the Array by removing end elements Given an array arr of size N, the task is to maximize the sum of sum, of the remaining elements in the array, by removing the end elements.Example: Input: arr[] = {2, 3} Output: 3 Explanation: At first we will delete 2, then sum of remaining elements = 3. Then delete 3. Therefore sum of sum = 3 + 0 6 min read Maximize total set bits of elements in N sized Array with sum M Given two integers N and M denoting the size of an array and the sum of the elements of the array, the task is to find the maximum possible count of total set bits of all the elements of the array such that the sum of the elements is M. Examples: Input: N = 1, M = 15Output: 4Explanation: Since N =1, 8 min read Maximize the sum of Array by formed by adding pair of elements Given an array a[] of 2*N integers, The task is to make the array a[] of size N i.e, reducing it to half size such that, a[i] = ?(a[j] + a[k]) / N?, 0 < j, k < 2*N - 1. and form the array so that the sum of all the elements of the array a[], will be maximum. Output the maximum sum. Examples: I 6 min read Maximum Sum of Array with given MEX Given 3 integers N, K, and X, the task is to construct an array arr[] with the below conditions: Size of the array = NMEX of the array = KAll array elements should be at most XAmong all the array that follows the above condition print the one having the maximum sum of its elements or print -1 if no 7 min read Maximize the sum of array by multiplying prefix of array with -1 Given an array of elements 'arr', the task is to maximize the sum of the elements of this array after performing the following operation: You can take any prefix of 'arr' and multiply each element of the prefix with '-1'. In the first line, print the maximized sum than in the next line, print the in 7 min read Maximize Array sum by replacing any K elements by its modulo with any positive integer Given an array of positive integer arr[], and a number K. the task is to maximize the sum of the array by replacing any K elements of the array by taking modulus with any positive integer which is less than arr[i] i.e, (arr[i] = arr[i]%X where X ⤠arr[i]). Examples: Input: arr[] = {5, 7, 18, 12, 11, 5 min read Like