Maximize Array sum by choosing pairs and dividing one part and multiplying other by K
Last Updated :
24 Jan, 2022
Given an array arr[] of size N and integer K, the task is to maximize the sum of the array by performing the following operations any number of times(possibly zero):
- Choose two indices, i and j where arr[i] must be a multiple of K.
- Make arr[i] = arr[i]/K and arr[j] = K*arr[j]
- After performing the above operations, the sum of the elements of the array must be maximum.
Examples:
Input: arr[] = {6, 6, 3}, N = 3, K = 3
Output: 57
Explanation: The optimal sequence for which sum of the array elements would be maximized is:
- Take i = 0 and j = 1, arr[0] = arr[0]/3 = 2, arr[1] = arr[1]*3 = 18, now the new array becomes, [2, 18, 3]
- Take i = 2 and j = 1, arr[2] = arr[2]/3 = 1, arr[1] = arr[1]*3 = 54, the array becomes [2, 54, 1]
Sum = 2 + 54 + 1 = 57
Input: arr[] = {1, 2, 3, 4, 5}, N = 5, K = 2
Output: 46
Explanation: The operation performed is:
Take i = 1 and j = 4, arr[1] = arr[1]/2 = 1, arr[4] = arr[4]*2 = 10. Now arr[] = {1, 1, 3, 4, 10}.
Take i = 3 and j = 4, arr[3] = arr[3]/2 = 2, arr[4] = arr[4]*2 = 20. Now arr[] = {1, 1, 3, 2, 20}.
Take i = 3 and j = 4, arr[3] = arr[3]/2 = 1, arr[4] = arr[4]*2 = 40. Now arr[] = {1, 1, 3, 1, 40}
Sum = 1 + 1+ 3 + 1 + 40 = 46.
Approach: The greedy approach is to divide all the array elements which are multiples of K by K and count the total number of division operations(say X) performed. Then sort the array and multiply the maximum element of the array X times by K i.e. arr[N-1] = arr[N-1]*KX. Follow the steps below to solve this problem:
- Create a variable total_division = 0, to store a total number of divisions performed.
- Iterate over the range [0, N) using the variable index and perform the following tasks:
- If arr[index] %K = 0, then find out how many times can arr[index] be divided by K and increment total_division that many times.
- Sort the array arr[].
- Run a while loop till total_division > 0 and make arr[N-1] = arr[N-1] * K and decrement total_division.
- Create a variable maximum_sum = 0.
- Iterate over the range [0, N) using the variable index and perform the following tasks:
- Update maximum_sum += arr[index].
- After performing the above steps, print the value of maximum_sum as the answer.
Below is the implementation for the above approach.
C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Utility function
int MaximumSumOperations(int arr[], int N,
int K)
{
if (N == 1) {
return arr[0];
}
// Variable to count total operations
int total_division = 0;
// Perform the division operation on
// the elements multiple of K
for (int index = 0; index < N;
index++) {
if (arr[index] % K == 0) {
while (arr[index] > 1
&& arr[index] % K == 0) {
arr[index] = arr[index] / K;
total_division++;
}
}
}
sort(arr, arr + N);
// Update maximum element and
// decrement total_division
while (total_division > 0) {
arr[N - 1] = K * arr[N - 1];
total_division--;
}
int maximum_sum = 0;
for (int index = 0; index < N;
index++) {
maximum_sum += arr[index];
}
// Return maximum_sum
return maximum_sum;
}
// Driver Code
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int N = 5, K = 2;
// Function called
cout << MaximumSumOperations(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Utility function
static int MaximumSumOperations(int arr[], int N,
int K)
{
if (N == 1) {
return arr[0];
}
// Variable to count total operations
int total_division = 0;
// Perform the division operation on
// the elements multiple of K
for (int index = 0; index < N;
index++) {
if (arr[index] % K == 0) {
while (arr[index] > 1
&& arr[index] % K == 0) {
arr[index] = arr[index] / K;
total_division++;
}
}
}
Arrays.sort(arr);
// Update maximum element and
// decrement total_division
while (total_division > 0) {
arr[N - 1] = K * arr[N - 1];
total_division--;
}
int maximum_sum = 0;
for (int index = 0; index < N;
index++) {
maximum_sum += arr[index];
}
// Return maximum_sum
return maximum_sum;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = 5, K = 2;
// Function called
System.out.println(MaximumSumOperations(arr, N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Utility function
def MaximumSumOperations(arr, N, K):
if (N == 1):
return arr[0];
# Variable to count total operations
total_division = 0;
# Perform the division operation on
# the elements multiple of K
for index in range(N):
if (arr[index] % K == 0):
while (arr[index] > 1 and arr[index] % K == 0):
arr[index] = arr[index] // K;
total_division += 1
arr.sort()
# Update maximum element and
# decrement total_division
while (total_division > 0):
arr[N - 1] = K * arr[N - 1];
total_division -= 1
maximum_sum = 0;
for index in range(N):
maximum_sum += arr[index];
# Return maximum_sum
return maximum_sum;
# Driver Code
arr = [1, 2, 3, 4, 5]
N = 5
K = 2
# Function called
print(MaximumSumOperations(arr, N, K));
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Utility function
static int MaximumSumOperations(int []arr, int N,
int K)
{
if (N == 1) {
return arr[0];
}
// Variable to count total operations
int total_division = 0;
// Perform the division operation on
// the elements multiple of K
for (int index = 0; index < N;
index++) {
if (arr[index] % K == 0) {
while (arr[index] > 1
&& arr[index] % K == 0) {
arr[index] = arr[index] / K;
total_division++;
}
}
}
Array.Sort(arr);
// Update maximum element and
// decrement total_division
while (total_division > 0) {
arr[N - 1] = K * arr[N - 1];
total_division--;
}
int maximum_sum = 0;
for (int index = 0; index < N;
index++) {
maximum_sum += arr[index];
}
// Return maximum_sum
return maximum_sum;
}
// Driver code
public static void Main()
{
int []arr = { 1, 2, 3, 4, 5 };
int N = 5, K = 2;
// Function called
Console.Write(MaximumSumOperations(arr, N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript code for the above approach
// Utility function
function MaximumSumOperations(arr, N, K)
{
if (N == 1)
{
return arr[0];
}
// Variable to count total operations
let total_division = 0;
// Perform the division operation on
// the elements multiple of K
for (let index = 0; index < N;
index++) {
if (arr[index] % K == 0) {
while (arr[index] > 1
&& arr[index] % K == 0) {
arr[index] = Math.floor(arr[index] / K);
total_division++;
}
}
}
arr.sort(function (a, b) { return a - b })
// Update maximum element and
// decrement total_division
while (total_division > 0) {
arr[N - 1] = K * arr[N - 1];
total_division--;
}
let maximum_sum = 0;
for (let index = 0; index < N;
index++) {
maximum_sum += arr[index];
}
// Return maximum_sum
return maximum_sum;
}
// Driver Code
let arr = [1, 2, 3, 4, 5];
let N = 5, K = 2;
// Function called
document.write(MaximumSumOperations(arr, N, K));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Similar Reads
Maximize the product of sum and least power by choosing at most K elements from given value and power Arrays Given two arrays arr[] and powr[] of size N and an integer K. Every element arr[i] has its respective power powr[i]. The task is to maximize the value of the given function by choosing at most K elements from the array. The function is defined as : f(N) = (arr[i1] + arr[i2] + arr[i3]+.....arr[in]) *
10 min read
Find the maximum cost of an array of pairs choosing at most K pairs Given an array of pairs arr[], the task is to find the maximum cost choosing at most K pairs. The cost of an array of pairs is defined as product of the sum of first elements of the selected pair and the minimum among the second elements of the selected pairs. For example, if the following pairs are
7 min read
Maximize Array sum by replacing any K elements by its modulo with any positive integer 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,
5 min read
Construct an Array of size N with sum divisible by K and array maximum is minimized Given integers N and K. The task is to construct an array of size N such that sum of all elements is divisible by K and the maximum element is as minimum as possible. Note: There can be many possible arrays. Printing any one of them is acceptable Examples: Input: N = 1, K = 5Output: 5Explanation: Su
8 min read
Maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array Given an array A[] of length N and an integer K, the task is to maximize the value of expression [i.j - K.(Ai | Aj)] over all pairs (i, j) in given Array, where (1 ⤠i < j ⤠N) and | denotes Bitwise OR operator. Examples: Input: A[] = {5, 20, 1, 0, 8, 11}, K = 10Output: 2Explanation: The maximum
6 min read
Maximize the sum of Array by formed by adding pair of elements Given an array a[] of 2*N integers, The task is to make the array a[] of size N i.e, reducing it to half size such that, a[i] = ?(a[j] + a[k]) / N?, 0 < j, k < 2*N - 1. and form the array so that the sum of all the elements of the array a[], will be maximum. Output the maximum sum. Examples: I
6 min read