Saturday, October 11, 2025
HomeData Modelling & AIMaximize the sum of modulus with every Array element

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:
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. Calculate 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 <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)
 

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 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6719 POSTS0 COMMENTS
Nicole Veronica
11881 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS