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++
// 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)
Similar Reads
Minimize cost for reducing array by replacing two elements with sum at most K times for any index Given an array arr[] of size N and an integer K. The task is to find the minimum cost required to collect the sum of the array. The sum of the array is collected by picking any element and adding it to an element of any index in the array. The addition of elements at the same index is allowed for at
11 min read
Minimize maximum array element possible by at most K splits on the given array Given an array arr[] consisting of N positive integers and a positive integer K, the task is to minimize the maximum element present in the array by splitting at most K array elements into two numbers equal to their value. Examples: Input: arr[] = {2, 4, 8, 2}, K = 4Output: 2Explanation:Following se
9 min read
Maximize remainder of sum of a pair of array elements with different parity modulo K Given an array arr[] of size N, consisting of N / 2 even and odd integers each, and an integer K, the task is to find the maximum remainder of sum of a pair of array elements of different parity modulo K. Examples: Input: arr[] = {3, 2, 4, 11, 6, 7}, K = 7Output: 6Explanation: Sum of a pair of array
7 min read
Maximise occurrence of an element after K replacements within Array elements Given an array arr[] having N integers, and an integer K, the task is to find an array such that it contains a single elements maximum number of times possible after K replacements within array elements. Examples: Input: N = 7, arr[] = {1, 2, 1, 5, 1, 6, 7}, K = 3Output: {1, 1, 1, 1, 1, 1, 7}Explana
7 min read
Minimize maximum array element by splitting array elements into powers of two at most K times Given an array arr[] consisting of N positive integers and an integer K, the task is to minimize the maximum value of the array by splitting the array element into powers of 2 at most K times. Examples: Input: arr[] = {2, 4, 11, 2}, K = 2Output: 2Explanation:Below are the operations performed on arr
12 min read
Maximize MEX by adding or subtracting K from Array elements Given an arr[] of size N and an integer, K, the task is to find the maximum possible value of MEX by adding or subtracting K any number of times from the array elements. MEX is the minimum non-negative integer that is not present in the array Examples: Input: arr[]={1, 3, 4}, K = 2Output: 2Explanati
7 min read