# Maximize Array sum by replacing any K elements by its modulo with any positive integer

• Last Updated : 21 Jan, 2022

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 ``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;``}`

## 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

 `  ```

Output
`41`

Time Complexity: O(N * logN) where N is the size of the array
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up