Блог пользователя Proof_by_QED

Автор Proof_by_QED, история, 11 месяцев назад, По-английски

Thanks for participation! We hope you loved the contest.

2124A - Deranged Deletions

Problem Credits: Lilypad

Hint
Solution
Code
Rate the Problem

2124B - Minimise Sum

Problem Credits: cry

Special thanks to prvocislo for misreading problem G to the statement of this problem, nifeshe for solving it, and Dominater069 for suggesting we use this problem!

Hint
Solution
Code
Rate the Problem

2124C - Subset Multiplication

Problem Credits: Proof_by_QED

Hint 1
Hint 2
Solution
Code
Bonus
Rate the Problem

2124D - Make a Palindrome

Problem Credits: satyam343

Hint 1
Hint 2
Solution
Code
Rate the Problem

2124E - Make it Zero

Problem Credits: satyam343

Hint 0
Hint 1
Hint 2
Solution
Code
Rate the Problem

2124F1 - Appending Permutations (Easy Version) and 2124F2 - Appending Permutations (Hard Version)

Problem Credits: Proof_by_QED

Huge thanks to Benq for transforming my original problem (which is solvable using a sequence found in OEIS) to the current problem!

Original Problem
Hint 1
Hint 2
Solution
Code
Rate the Problem (F1)
Rate the Problem (F2)

2124G - Maximise Sum

Problem Credits: cry
Special thanks to Error_Yuan and Friedrich for solving this problem!

Hint 0
Hint 1
Hint 2
Solution
Code
Rate the Problem

2124H - Longest Good Subsequence

Problem Credits: Proof_by_QED

Special thanks to satyam343 for helping with improving the problem, and Dominater069 for the full solution!

Hint 1
Hint 2
Solution
Code
Rate the Problem

2124I - Lexicographic Partition

Problem Credits: Proof_by_QED

Hint 1
Hint 2
Solution
Code
Rate the Problem
  • Проголосовать: нравится
  • +309
  • Проголосовать: не нравится

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

OMG!! Lightning fast editorial :orz:

»
10 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +82 Проголосовать: не нравится

at most 17 operations...... was that part just to confuse participants ? I doubted myself for 10 minutes if 2 operations are actually enough.

Happy with + delta in the end

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Instant editorial

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

GreatProblemsForces orz !!

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

why it's showing 4 days ago?

fastest editorial i have ever seen

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Lightning Fast Editorial!

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

thank you for rating

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +76 Проголосовать: не нравится

Wow, didn't expect me misreading a statement to turn out to be a good thing :D

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Lovely Contest and Great problems

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +39 Проголосовать: не нравится

Thanks for the round! Hope to see more problems from you guys where the input is just an array of n numbers

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

thanks for fast editorial

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Great problemset, but had my worst performance :(

Can anyone clarify why in 327782150, the fourth given testcase doesnt accept 3 as a solution?

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

oh wow.. wish I could upvote more than once for superfast editorial.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why was the constraint a[i]<=n mentioned in problem B?

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I am so angry right now with myself for not solving D.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +39 Проголосовать: не нравится

satyam343 is your birthday coming up on 17th July? Man, there are better ways to tell us that.

»
10 месяцев назад, скрыть # |
Rev. 6  
Проголосовать: нравится +3 Проголосовать: не нравится

I solved D with a different approach.First try to imagine you are creating a new array lets say its name is ANS with at least k elements and set the limit to the k-th smallest value in A.Then find the values under the limit and save their indexes in a new array lets say this array is B.Then make a prefix sum array with this: pref[i]=pref[i-1]+(a[i]==limit); then look how many values with limit number between every two adjacent indexes in B (using the prefix array I mentioned above) then try to add many integer to ANS.We need to maximize the number of values we take but our ANS array should be palindrome so we will take same amount of integer from the opposite.so we should use ANS+=min(diff[i],diff[n-i-1])

Implementation:https://mirror.codeforces.com/contest/2124/submission/327813510

Note:Sorry for my poor english and I cant explain myself very well :(

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I appeared for the very first time in div 1+2, great contest to be honest. Overestimated B and wrote a very complicated code just because of div 1 in name of the round :( . And D , why was 17 operations mentioned in the question, it just threw me off the scent. Anyways, enjoyed the round and nice questions.

»
10 месяцев назад, скрыть # |
Rev. 4  
Проголосовать: нравится +8 Проголосовать: не нравится

Minor corrections in the editorial for E: the example array [3, 4, 3] (I see what you did there!) should be reduced by [3, 2, 1] to become [0, 2, 2]. Transforming it to [1, 2, 0] violates the problem constraints

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Great contest, many thanks!

In the problem D you claim "Claim as long as the k-1 smallest elements remain in the array, we can erase other elements in any way we want."

Is "any way" part of the claim correct? For example, if k=3 and array is 1,2,3,8,8,8,8,8,7,8,8,8,8 then there is no way you can erase the 7 only. In the next paragraph you write you remove everything larger than x, so this is not important.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In D problem, can someone explain more clearly, two pointer part when b[l] != b[r]

  • »
    »
    10 месяцев назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    You make an array of only elements <= value of kth element, in the order they appear in the array. Now, you basically do a palindrome check on this new array using l and r (two pointer) from ends of array. When both elements are equal, it's cool.

    If they aren't equal, there's two possibilities ---> atleast one of those elements is the kth smallest element, in which case you increase a variable cnt and increment/decrement that index (basically you imagine you removed it). Or both elements are not equal to kth smallest element, in which case the answer is straight NO. Do this till l<r holds.

    Now, this cnt variable signifies the number of removals of kth smallest value you had to do to make this array a palindrome. If the number of elements remaining after such removals (size of this array — cnt) >= k-1, then it is valid. Else not

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

Could someone please explain D? Cannot understand the logic even after reading the tutorial..

  • »
    »
    10 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +1 Проголосовать: не нравится

    Okay so I didn't solve the question but I understood what he wants to say, the only elements we can't erase by making it the kth smallest are the k-1 smallest elements, every other element can be erased by some manipulation of l and r. Let's take an example:

    1 2 2 3 3 5 5 6 7

    let k be 3

    I want to erase 7. Notice that I cannot erase 1, one occurence of 2 whatever I do. to erase 7 i just take the last 3 elements, to erase 6 i can take the last — 1th 3 elements and so on. Try this for any array and convince yourself that this works.

    So effectively, all you need for checking the palindrome is the numbers upto the face value of the kth smallest number. Also notice that if I am able to make a palindrome using 3 different numbers, even after removing some numbers completely I will be able to make a palindrome.

    Eg: Suppose 1 5 3 4 4 6 3 3 1 3 and I want to see with k = 5

    I am saying that if you can make a palindrome 1 3 4 4 3 3 1, you could just remove 4, 3 and still have 1 1 as a palindrome. For k = 5, keep only the numbers lesser than or equal to the k — 1th smallest number, in this case I keep numbers <= 3 So 1 3 3 3 1 3

    Now use a normal 2 pointer approach to check palindrome but make sure you do not remove so many elements that you have less than k — 1 elements left. 1 != 3, remove that 3

    size = 5, which is greater than 4. Proceed, if size < 4 then stop(You can remove only 3s not 1s).

    I tried to explain it with examples, hopefully it is helpful.

»
10 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

In problem D, Claim: as long as the k−1 smallest elements remain in the array, we can erase other elements in any way we want.

Let say, k=3 and a= [1,2,3,4,5,4,3,2]

How to erase 5? Can we erase 5 in any order that we want?

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Feeling good that I did till D. But also little bit sad for not getting E.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

D can be solved in O(n) as a[i]<=n

»
10 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

In problem $$$C$$$, when $$$b_i = a_i . x$$$ and $$$b_{i + 1} = a_{i + 1}$$$, how are we sure that the final $$$x$$$ we get will divide $$$b_i$$$? Any multiple should be okay right? Why only take the lowest multiple?

  • »
    »
    10 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Let $$$x_{'}$$$ be the actual answer used by Bob.

    For this case, we know these statements are true:

    1. $$$x_{'}$$$ divides $$$b_i$$$.
    2. $$$b_i$$$ divides $$$b_{i+1} \cdot x_{'}$$$.
    3. From 2, $$$x_{'}$$$ must be a multiple of $$$b_i / \text{gcd}(b_i, b_{i+1})$$$ for all $$$i$$$ where $$$b_i$$$ doesn't divide $$$b_{i+1}$$$. This is a classic use case for LCM. Let the LCM be $$$x$$$.
    4. From 3, $$$x$$$ must divide $$$x_{'}$$$.

    Using the property, if $$$p$$$ divides $$$q$$$ and $$$q$$$ divides $$$r$$$, then $$$p$$$ divides $$$r$$$: From 1 and 4, we can conclude $$$x$$$ divides $$$b_i$$$.

    The main statement was this: "It is guaranteed the array $$$b$$$ can be obtained from some beautiful array $$$a$$$ and some positive integer $$$x$$$ as described in the statements."

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Umm, i want to ask, this contest rated? Or?

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

When will ratings come

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

This is the first contest of div2(+div1) where I managed to solve 4 problem :)

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

Codeforces participants when a question involves basic maths :( [Round 1035 B]

Codeforces participants when the entire contest is arrays :)

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

any idea about rating of problem C and D

»
10 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Great

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in A i used the approach, i.e. sort the input array and then compare it to the original ones and print the non similar elements, much easier and intuitive than the approach given in editorial.

»
10 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Problem D

8 5

4 7 1 2 3 1 3 4

why the output of this test case is "NO" ?

After sorting this we get 1 1 2 3 3 4 4 7 and we can always delete all elements >= 5th largest element in the array. so we can delete 7,3,3 and get 4 1 2 1 4 which is a valid Palindrome.

My solution-

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int INF = 1e17;
const int MOD = 1e9 + 7;
typedef pair<int,int> pi;
typedef pair<int,pair<int,int>> ppi;

void sol() {
    // your logic here
    int n,k;

    cin>>n>>k;
    vector<int> a(n);
    for(auto &i:a)cin>>i;
    if(k == 1){
        cout<<"YES"<<endl;
        return;
    }
    vector<int> b = a;
    sort(b.begin(),b.end());
    //we cannot delete first k-1 elements
    int l = 0;
    int r = a.size()-1;
    int left = a.size() - k + 1;
    while(l < r){
        if(a[l] != a[r]){
            if(left == 0){
                cout<<"NO"<<endl;
                return;
            }

            if(a[l] >= b[k-1]){
                l++;
                left--;
            }else if(a[r] >= b[k-1]){
                r--;
                left--;
            }else{
                cout<<"NO"<<endl;
                return;
            }
            continue;
        }
        l++;
        r--;
    }

    cout<<"YES"<<endl;

}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    //precompute_factorials();
    int t;
    cin >> t;
    while(t--) sol();
    return 0;
}
»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

In C, the editorial says to take the LCM of b_i/gcd(b_i,b_i+1), but in the code theyre taking the LCM of b_i/gcd(b_i, b_i+1, .... b_n). Can anyone please explain how they do the same thing?

»
10 месяцев назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

could someone help me out with what tc this fails for E ?:

t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int, input().split()))
    if sum(a)%2==1 or max(a)>sum(a)//2:
        print(-1)
    else:
        w=sum(a)//2
        z=0
        i=0
        while z<w:
            z+=a[i]
            i+=1
        if z==w:
            print(1)
            print(*a)
        else:
            x=[i-1,i-2]
            t=[]
            u=[]
            for j in range(i-1):
                t.append(a[j])
            t.append(w-sum(t))
            for j in range(i,n-1):
                u.append(a[j])
            u.append(w-sum(u))
            v=t+u
            c=[0]*n
            r=v[-1]-a[-1]
            v[-1]-=r
            temp=r
            for k in range(i-1):
                if v[k]>r:
                    y=min(v[0],temp)
                    v[k]-=min(v[0],temp)
                    temp-=y
                    if temp==0:
                        break
            for i in range(n):
                c[i]=a[i]-v[i]
            print(2)
            print(*v)
            print(*c)
»
10 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

about problem E

so can in be solved in binary ?

my idea is :

let sum(l, mid) = sl, sum(r, mid) = sr

if sl == sr return

else if sl > sr [l, r] -> [l, mid]

else [l, r] -> [mid + 1, r]

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

About Problem F1

In the case 1234123,I chose the order 123/4123,not 1234/123.but it is wrong.

My submission https://mirror.codeforces.com/contest/2124/submission/327919549

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

observationforces

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone suggest how I should start solving a problem after reading it? I'm unable to understand how to begin approaching it.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For D, There is an $$$O(n)$$$ approach to find the kth element in a vector, see LeetCode link

A way to implement it
  • »
    »
    10 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Its a small and good approach but

    O(n) is average, worst case it takes O(n^2), although extremely rare but still its possible. Its like people just ignore the worst case and only consider the average, like many people say unordered_map is O(1) but it has O(n) worst case as well.

    And I think in O(nlog(n)) and O(n) both are almost equally same, some times if too many collisions happen or constant factor are very far apart, O(nlog(n)) is better and the main thing is we know it will definitely not fail in any case while O(n) is can.

    “Although very rare”

    • »
      »
      »
      10 месяцев назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      First, the worst complexity of std::nth_element is $$$O(n\log(n))$$$ , and still has a constant that is far less than std::sort

      Next, if you are REALLY worried about the potential that the codeforcers would hack std::nth_element, we can implement it by ourselves

      Another way to implement it

      Based on a random pivot, it is impossible to make the process degrade to $$$O(n^2)$$$ or $$$O(n\log(n))$$$

      Then, I don't know if you have heard a term names "卡常", which means that the time limit is very tight, and we need to optimize EVERYTHING that we can to avoid it getting TLE. For example, instead of scanf or std::cin, we might use:

      QRead

      to read integer datas, and use a C-style array to simulate a queue instead of std::queue. Tens of milliseconds as those optimization could, they might save a correct code from TLE. So it is important to at least know these optimizations.

      Finally, "not fail" is only for this problem, LEARNING is the goal that we solve problems and read comments on Codeforces. The idea behind std::nth_element is quick select, which is a grand performance of divide-and-conquer, and is definitely worthy for a deeper insight.

      • »
        »
        »
        »
        10 месяцев назад, скрыть # ^ |
         
        Проголосовать: нравится +1 Проголосовать: не нравится

        Thanks that's quite useful info's. Many people including me only knows some of it. If you don't mind where do you learn this things because now a days everyone only wants to know the basic and not dive deep like this. Because like I can't like find the std::nth_element being used, but as u described its useful. I used ordered_set but I could have used this instead.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hey guys for question C :

I iterated from right to left comparing adjacent elements, if right element is not an multiple of left element I add it to a set by value (left_element/hcf(left__elemenet,right_element)), the final output is the LCM of the collected hcfs set, It is losing at around 5000 testcase, can u guys tell me what is wrong

My Code

»
10 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +16 Проголосовать: не нравится

I had a hard time figuring out the editorial solution for Problem F2 — Appending Permutations (Hard Version). So I'm sharing my solution which slightly deviates from that.

We will still need the $$$chain(i, j)$$$ using the same definition as given in the editorial. We will also require:

  1. $$$dp(i, j)$$$ = Number of ways to fill indices $$$i, ..., n$$$ where $$$a[i] = j$$$.
  2. $$$dp_2(i)$$$ = Sum over $$$dp(i, j)$$$ for all $$$1 \leq j \leq n$$$.
  3. $$$close(i,j)$$$ = Number of ways to fill indices $$$i, ..., n$$$ where we begin with putting $$$a[i] = 1, a[i+1] = 2, ..., a[i+j-1] = j$$$ (start with an identity permutation of length $$$j$$$, while maintaining the restriction conditions).

Now, we calculate the dp values as follows:

$$$\large{close(i,j) = [ chain(i, 1) \geq j ] \times dp_2(i + j)}$$$

The tricky case which was explained in the editorial:

$$$\large{dp(i, 1) = \sum_{k = 1}^{chain(i, 1)} dp_2(i+k) - dp(i+k, k+1)}$$$

Finally, for $$$j \gt 1$$$,

$$$\large{dp(i, j) = \sum_{k = 1}^{chain(i,j)} close(i+k, j-1) }$$$

We can calculate $$$dp(i,1)$$$ in $$$O(n)$$$ time, and by maintaining suffix sums over the $$$close(i, j)$$$ array, (i.e. $$$sufsum(i,x) = close(i,x) + close(i+1,x) + \ldots + close(n,x)$$$) we can calculate each $$$dp(i, j)$$$ in $$$O(1)$$$ for $$$j \gt 1$$$.

Final complexity $$$O(n^2)$$$. Here is my AC submission: 328032797

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится -28 Проголосовать: не нравится

Did anyone notice that the problem H have insane and severe time limitation???????

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

B was much easier than I thought I had the correct idea at first but I doubted myself and over complicated it but still AC

»
10 месяцев назад, скрыть # |
Rev. 4  
Проголосовать: нравится +8 Проголосовать: не нравится

Alternative way to think about H:

First, subtract $$$1$$$ from all elements of $$$a$$$. Now, $$$b_i$$$ would be the largest integer such that $$$p_{b_i} \lt p_i$$$ (or $$$b_i = 0$$$ if $$$i$$$ is a prefix min). Let's look at some sequence $$$b$$$. Imagine the tree where the parent of vertex $$$i$$$ is $$$b_i$$$ (with an auxiliary node $$$0$$$ as the root of the tree).

Claim: $$$b$$$ is good iff $$$0, 1, 2, \ldots, |b|$$$ is a valid dfs pre-order traversal of the tree. This condition is equivalent to every subtree having vertices whose indices form a contiguous interval of integers.

Now, a more intuitive way to understand the DP is that $$$dp_{i,j}$$$ is the biggest vertex you can put down when assembling the subtree of $$$a_i$$$ (imagine your dfs pre-order traversal has already gone through $$$1, 2, ... a_i$$$, and now you are underneath vertex $$$a_i$$$), and only using elements in the subarray $$$a_i, a_{i+1}, \ldots, a_j$$$.

Btw, I really enjoyed this problem. Kudos to the author!

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

The problem 2124A — Deranged Deletions has all the below tags. Isn't that incorrect and if it's incorrect, how can it be fixed?

2-sat; chinese remainder theorem; divide and conquer; expression parsing; fft; flows; geometry; implementation; interactive; matrices; probabilities; schedules; sortings; string suffix structures; ternary search

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Actually the optimal time complexity for D is O(n), because you only sort to find kth element

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Great tutorial! But why does problem H exist "Uisng the above"?

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem F1, i dont actually understand the way to counter the overlap when dp, can someone explain it for me.(●'◡'●)

»
9 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

in problem C, can we say that $$$\frac{b_i}{gcd(b_i,\phantom{1}b_{i + 1})}$$$ always equals either $$$1$$$ or $$$x$$$ for each $$${1}\le{i}\lt{n}$$$?

»
7 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

3rd problem the Bonus part, we don't find an x, for 50,70,100, we get multiple values, this is happening maybe because b1/gcd(b1,b2),b2/gcd(b1,b2) are coprimes?

»
4 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I was missing explanation in D on how to construct array that any element >= k-1 smallest could be deleted. There are several ways, as mentioned, two pointer technique is one of those (l=0, r=n-1). You track rank of the element which you'd like to delete. If it's larger than k, remove any side l/r bordering element (if it's not the element itself) and adjust rank accordingly: if selected l (let's say) is >= than the element, reduce rank by 1, otherwise keep it unchanged (larger element is removed). Other way is to create sorted array of indices of elements <= selected element (including itself), and pick range start as [max(0, element index in that array — k + 1), start + k — 1].

Also this remark wasn't obvious for me "Note there is no point in keeping any elements in the third group"