2779. Maximum Beauty of an Array After Applying Operation LeetCode Solution

In this guide, you will get 2779. Maximum Beauty of an Array After Applying Operation LeetCode Solution with the best time and space complexity. The solution to Maximum Beauty of an Array After Applying Operation problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Beauty of an Array After Applying Operation solution in C++
  4. Maximum Beauty of an Array After Applying Operation solution in Java
  5. Maximum Beauty of an Array After Applying Operation solution in Python
  6. Additional Resources
2779. Maximum Beauty of an Array After Applying Operation LeetCode Solution image

Problem Statement of Maximum Beauty of an Array After Applying Operation

You are given a 0-indexed array nums and a non-negative integer k.
In one operation, you can do the following:

Choose an index i that hasn’t been chosen before from the range [0, nums.length – 1].
Replace nums[i] with any integer from the range [nums[i] – k, nums[i] + k].

The beauty of the array is the length of the longest subsequence consisting of equal elements.
Return the maximum possible beauty of the array nums after applying the operation any number of times.
Note that you can apply the operation to each index only once.
A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.

Example 1:

Input: nums = [4,6,1,2], k = 2
Output: 3
Explanation: In this example, we apply the following operations:
– Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
– Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
It can be proven that 3 is the maximum possible length we can achieve.

See also  890. Find and Replace Pattern LeetCode Solution

Example 2:

Input: nums = [1,1,1,1], k = 10
Output: 4
Explanation: In this example we don’t have to apply any operations.
The beauty of the array nums is 4 (whole array).

Constraints:

1 <= nums.length <= 105
0 <= nums[i], k <= 105

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2779. Maximum Beauty of an Array After Applying Operation LeetCode Solution in C++

class Solution {
 public:
  int maximumBeauty(vector<int>& nums, int k) {
    int ans = 0;

    ranges::sort(nums);

    for (int l = 0, r = 0; r < nums.size(); ++r) {
      while (nums[r] - nums[l] > 2 * k)
        ++l;
      ans = max(ans, r - l + 1);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2779. Maximum Beauty of an Array After Applying Operation LeetCode Solution in Java

class Solution {
  public int maximumBeauty(int[] nums, int k) {
    int ans = 0;

    Arrays.sort(nums);

    for (int l = 0, r = 0; r < nums.length; ++r) {
      while (nums[r] - nums[l] > 2 * k)
        ++l;
      ans = Math.max(ans, r - l + 1);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2779. Maximum Beauty of an Array After Applying Operation LeetCode Solution in Python

class Solution:
  def maximumBeauty(self, nums: list[int], k: int) -> int:
    ans = 0

    nums.sort()

    l = 0
    for r in range(len(nums)):
      while nums[r] - nums[l] > 2 * k:
        l += 1
      ans = max(ans, r - l + 1)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.