Maximize Array sum by replacing any K elements by its modulo with any positive integer
Last Updated :
03 Jun, 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++
#include <bits/stdc++.h>
using namespace std;
int find( int arr[], int K, int N)
{
sort(arr, arr + N);
int sum = 0;
for ( int i = 0; i < K; i++) {
arr[i] %= (arr[i] / 2) + 1;
}
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]);
cout << find(arr, K, N);
return 0;
}
|
C
#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;
}
}
}
}
int find( int arr[], int K, int N)
{
sort(arr, N);
int sum = 0;
for ( int i = 0; i < K; i++)
{
arr[i] %= (arr[i] / 2) + 1;
}
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;
}
|
Java
import java.util.Arrays;
class GFG {
static int find( int arr[], int K, int N) {
Arrays.sort(arr);
int sum = 0 ;
for ( int i = 0 ; i < K; i++) {
arr[i] %= (arr[i] / 2 ) + 1 ;
}
for ( int i = 0 ; i < N; i++) {
sum += arr[i];
}
return sum;
}
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));
}
}
|
Python3
def find(arr, K, N):
arr.sort()
sum = 0
for i in range ( 0 , K):
arr[i] % = (arr[i] / / 2 ) + 1
for i in range ( 0 , N):
sum + = arr[i]
return sum
if __name__ = = "__main__" :
arr = [ 5 , 7 , 18 , 12 , 11 , 3 ]
K = 4
N = len (arr)
print (find(arr, K, N))
|
C#
using System;
class GFG {
static int find( int [] arr, int K, int N)
{
Array.Sort(arr);
int sum = 0;
for ( int i = 0; i < K; i++) {
arr[i] %= (arr[i] / 2) + 1;
}
for ( int i = 0; i < N; i++) {
sum += arr[i];
}
return sum;
}
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));
}
}
|
Javascript
<script>
function find(arr, K, N) {
arr.sort( function (a, b) { return a - b })
let sum = 0;
for (let i = 0; i < K; i++) {
arr[i] %= Math.floor(arr[i] / 2) + 1;
}
for (let i = 0; i < N; i++) {
sum += arr[i];
}
return sum;
}
let arr = [5, 7, 18, 12, 11, 3];
let K = 4;
let N = arr.length;
document.write(find(arr, K, N));
</script>
|
Time Complexity: O(N * logN) where N is the size of the array
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...