Maximal Score After Applying K Operations

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0.
In one operation:
choose an index
isuch that0 <= i < nums.length,increase your score by
nums[i], andreplace
nums[i]withceil(nums[i] / 3).
Return the maximum possible score you can attain after applying exactly k operations.
The ceiling function ceil(val) is the least integer greater than or equal to val.
LeetCode Problem - 2530
class Solution {
public long maxKelements(int[] nums, int k) {
long answer = 0; // Variable to store the final answer (sum of top k elements)
// Priority queue to store elements in descending order (max heap)
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
// Add all elements of the array to the priority queue
for (int num : nums) {
pq.add(num);
}
// Perform k iterations to find the maximum sum
for (int i = 0; i < k; i++) {
// Poll the maximum element from the priority queue
int peek = pq.poll();
// Add the maximum element to the result
answer += (long) peek;
// Calculate the new value by dividing the current element by 3 and taking the ceiling
int ceilValue = (int) Math.ceil((double) peek / 3);
// Add the modified value back into the priority queue
pq.add(ceilValue);
}
// Return the accumulated answer after k operations
return answer;
}
}




