Skip to content

2779. Maximum Beauty of an Array After Applying Operation #941

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can utilize sorting and a sliding window approach.

Approach:

  1. Sort the array: Sorting simplifies identifying subsequences where the difference between the largest and smallest element does not exceed 2k.
  2. Sliding window technique: Maintain a window of indices [i, j] where the difference nums[j] - nums[i] <= 2k. Adjust i or j to maximize the window size.

Let's implement this solution in PHP: 2779. Maximum Beauty of an Array After Applying Operation

<?php
/**
 * @param Integer[] $nums
 * @param Integer $k
 * @return Integer
 */
function maximumBeauty($nums, $k) {
    // Sort the array
    sort($nums);
    
    $maxBeauty = 0; // To store the maximum beauty found
    $i = 0;         // Sl…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Dec 11, 2024
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants