The Wayback Machine - https://web.archive.org/web/20211027122729/https://www.geeksforgeeks.org/maximize-array-sum-after-incrementing-at-most-k-elements-followed-by-dividing-array-by-x/
Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Maximize Array sum after incrementing at most K elements followed by dividing array by X

  • Last Updated : 27 Oct, 2021

Given an array, arr[] and two integers, K and X, the task is to maximize the sum of the array elements after at most K increments of any elements, then dividing all of the elements by X

Example:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr = {16, 15, 17}, K = 8, X = 10
Output: 5
Explanation: At most 8 increments are allowed. So, increment element at index 0 by 4 and element at index 2 by 3. So the modified array becomes {20/10, 15/10, 20/10} = {2, 1, 2}, therefore the sum is 2+1+2 = 5



Input: arr = {8, 13, 2, 4, 7}, K = 6, X = 5
Output: 7

 

Approach: The given problem can be solved by using a greedy approach. The idea is to sort the array by tweaking the comparator in such a way that the elements which need the least increment to become divisible by X are encountered first. Follow the steps below to solve the problem:

  • Sort the ArrayList in the order where elements having the greater remainder after dividing by X are encountered first
  • Iterate the ArrayList and at every iteration:
    • Find the remaining value rem to be added to arr[i], such that it becomes a multiple of X
      • If K < rem, then break the loop
      • Else increment arr[i] with rem and decrement K by rem
  • Initialize a variable sum to 0
  • Traverse the ArrayList and at every iteration:
    • Add arr[i] / X to the sum

Below is the implementation of the above approach:

Java




// Java implementation for the above approach
  
import java.util.*;
import java.io.*;
  
class GFG {
    public static int maxSumIncDiv(
        List<Integer> nums, int k, int x)
    {
  
        // Sort in such a way that the
        // numbers needing least increment
        // to become multiple of x are
        // encountered first
        Collections.sort(
            nums,
            (a, b) -> b % x - a % x);
  
        // Iterate the ArrayList
        for (int i = 0; i < nums.size(); i++) {
  
            int rem = nums.get(i) % x;
  
            // Find the value to increment
            // current element such that it
            // becomes a multiple of x
            rem = x - rem;
  
            // Incrementing any element with
            // the remaining value of k will
            // have no effect
            if (k < rem) {
  
                break;
            }
            else {
  
                // Increment element so that
                // it becomes multiple of x
                nums.set(i, nums.get(i) + rem);
  
                // Decrement k by the
                // remaining value
                k -= rem;
            }
        }
  
        // Initialize sum
        int sum = 0;
  
        // Traverse the ArrayList
        for (int i = 0; i < nums.size(); i++) {
  
            // calculate sum of elements
            // divided by x
            sum += nums.get(i) / x;
        }
  
        // Return the answer
        return sum;
    }
    public static void main(String[] args)
    {
  
        // Initializing list of nums
        List<Integer> nums = new ArrayList<>();
  
        int K = 6, X = 5;
        nums.add(8);
        nums.add(13);
        nums.add(2);
        nums.add(4);
        nums.add(7);
  
        // Call the function and
        // print the answer
        System.out.println(
            maxSumIncDiv(nums, K, X));
    }
}
Output
7

Time Complexity: O(N * log N)
Auxiliary Space: O(N)




My Personal Notes arrow_drop_up
Recommended Articles
Page :