Maximize Array sum by replacing any K elements by its modulo with any positive integer
Last Updated :
23 Jul, 2025
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, 3}, K = 4
Output: 41
Explanation: The replacement should be {5%3, 7%4, 18, 12, 11%6, 3%2}
Input: arr[] = {8, 2, 28, 12, 7, 9}, K = 4
Output: 55
Explanation: The replacement should be {8%5, 2%2, 28, 12, 7%4, 9}
Approach: For every element arr[i] in the array arr[], module it with (arr[i]/2 +1) which will give the highest possible value of arr[i] after the operation. Following are the steps to solve the problem
- Sort the array arr[].
- Iterate over the range [0, K) using the variable i and perform the following tasks:
- For every element arr[i], module it with (arr[i]/2 +1) and update the result.
- Find the sum of the updated array and output it.
Below is the implementation of the above approach.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum possible sum
int find(int arr[], int K, int N)
{
// Sorting the array
sort(arr, arr + N);
int sum = 0;
// Loop to take update K
for (int i = 0; i < K; i++) {
// Smallest number in array
arr[i] %= (arr[i] / 2) + 1;
}
// Loop to find sum
for (int i = 0; i < N; i++) {
sum += arr[i];
}
return sum;
}
// Driver Code
int main()
{
int arr[] = { 5, 7, 18, 12, 11, 3 };
int K = 4;
int N = sizeof(arr) / sizeof(arr[0]);
cout << find(arr, K, N);
return 0;
}
C
// C program for the above approach
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <limits.h>
void sort(int arr[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Function to find the maximum possible sum
int find(int arr[], int K, int N)
{
// Sorting the array
sort(arr, N);
int sum = 0;
// Loop to take update K
for (int i = 0; i < K; i++)
{
// Smallest number in array
arr[i] %= (arr[i] / 2) + 1;
}
// Loop to find sum
for (int i = 0; i < N; i++)
{
sum += arr[i];
}
return sum;
}
int main()
{
int arr[] = {5, 7, 18, 12, 11, 3};
int K = 4;
int N = sizeof(arr) / sizeof(arr[0]);
printf("%d", find(arr, K, N));
return 0;
}
// This code is contributed by abhinavprkash.
Java
// Java program for the above approach
import java.util.Arrays;
class GFG {
// Function to find the maximum possible sum
static int find(int arr[], int K, int N) {
// Sorting the array
Arrays.sort(arr);
int sum = 0;
// Loop to take update K
for (int i = 0; i < K; i++) {
// Smallest number in array
arr[i] %= (arr[i] / 2) + 1;
}
// Loop to find sum
for (int i = 0; i < N; i++) {
sum += arr[i];
}
return sum;
}
// Driver Code
public static void main(String args[]) {
int arr[] = { 5, 7, 18, 12, 11, 3 };
int K = 4;
int N = arr.length;
System.out.println(find(arr, K, N));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python3 program for the above approach
# Function to find the maximum possible sum
def find(arr, K, N):
# Sorting the array
arr.sort()
sum = 0
# Loop to take update K
for i in range(0, K):
# Smallest number in array
arr[i] %= (arr[i] // 2) + 1
# Loop to find sum
for i in range(0, N):
sum += arr[i]
return sum
# Driver Code
if __name__ == "__main__":
arr = [5, 7, 18, 12, 11, 3]
K = 4
N = len(arr)
print(find(arr, K, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the maximum possible sum
static int find(int[] arr, int K, int N)
{
// Sorting the array
Array.Sort(arr);
int sum = 0;
// Loop to take update K
for (int i = 0; i < K; i++) {
// Smallest number in array
arr[i] %= (arr[i] / 2) + 1;
}
// Loop to find sum
for (int i = 0; i < N; i++) {
sum += arr[i];
}
return sum;
}
// Driver Code
public static void Main()
{
int[] arr = { 5, 7, 18, 12, 11, 3 };
int K = 4;
int N = arr.Length;
Console.Write(find(arr, K, N));
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the maximum possible sum
function find(arr, K, N) {
// Sorting the array
arr.sort(function (a, b) { return a - b })
let sum = 0;
// Loop to take update K
for (let i = 0; i < K; i++) {
// Smallest number in array
arr[i] %= Math.floor(arr[i] / 2) + 1;
}
// Loop to find sum
for (let i = 0; i < N; i++) {
sum += arr[i];
}
return sum;
}
// Driver Code
let arr = [5, 7, 18, 12, 11, 3];
let K = 4;
let N = arr.length;
document.write(find(arr, K, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N * logN) where N is the size of the array
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem