Loser_'s blog

By Loser_, 5 months ago,

I just completed CSES Sorting and Searching section problems. And I didn't find any editorials for this. So I am writing my own approach for the problems.Please do correct me if I made any mistakes and do share your approach for the problems.

My solutions are here

complete editorial for CSES coding platform of all 27 problems in Sorting and Searching section.

1.Distinct Numbers

Editorial
idea

2.Apartments

Editorial
idea

3.Ferris Wheel

Editorial
idea

4.Concert Tickets

Editorial
idea

5.Restaurant Customers

Editorial
idea

6.Movie Festival

Editorial
idea

7.Sum of Two Values

Editorial
idea

8.Maximum Subarray Sum

Editorial
idea

9.Stick Lengths

Editorial
idea

10.Playlist

Editorial
idea

11.Towers

Editorial
idea

12.Traffic Lights

Editorial
idea

13.Room Allocation

Editorial
idea

14.Factory Machines

Editorial
idea

Editorial
idea

Editorial
idea

17.Sum of Three Values

Editorial
idea

18.Sum of Four Values

Editorial
idea

19.Nearest Smaller Values

Editorial
idea

20.Subarray Sums I

Editorial
idea

21.Subarray Sums II

Editorial
idea

22.Subarray Divisibility

Editorial
idea

23.Array Division

Editorial
idea

24.Sliding Median

Editorial
idea

25.Sliding Cost

Editorial
idea

26.Movie Festival II

Editorial
idea

27.Maximum Subarray Sum II

Editorial
idea

• +4

 » 5 months ago, # |   0 Thank You so much
•  » » 5 months ago, # ^ |   0 Welcome
 » 5 months ago, # |   0 thanks
•  » » 5 months ago, # ^ |   0 welcome
 » 5 months ago, # | ← Rev. 2 →   0 .
 » 5 months ago, # |   0 Thanks a lot,man
 » 4 months ago, # |   0 too helpful, thanks
 » 4 months ago, # |   0 how is this working in 25th problem sliding costabs(p-a[i+m])-abs(P-a[i]) and if m is even we decrease the extra value p-Pcan someone explain why the common elements in the the windows are not considered for new cost ? Please explain the math behind it
•  » » 4 months ago, # ^ | ← Rev. 2 →   0 In this problem,everytime we check for a window of k elements.First we check for first k elements and store it's mid value in P(capital). Then with each iteration we erase the first value from window and add the next value from the array.See,if the initial set is 2 3 4 after iteration it's 3 4 5.After the iteration mid value is p(small).Now after the new value added to set the cost is abs(p-a[i+m]) and the previous cost is abs(P-a[i]).So the change of total cost d is simply the difference between these two.Now when it's come to even value ,Like this one- 8 4 2 4 3 5 8 1 2 1 Here the previous window is 2 3 4 5 and after 1st iteration 3 4 8 5. P(capital)=3, p(small)=4; Unlike the odd k ,even k has two mid value. So either 1st mid value gives you the min cost or the 2nd mid value. If you notice then P(capital) is 1st mid value, p(small) is 2nd mid value. That's the reason we simply erase the extra value(it's either add to total cost or decreases).I hope it makes sense.
•  » » » 4 months ago, # ^ |   +4 If you notice then P(capital) is 1st mid value, p(small) is 2nd mid value this statement is not always true.You just explained the code, I want some kind of mathematical proof of why this works ? Why changing median doesn't effect cumulative cost of the common elements is the windows ?
 » 4 months ago, # |   0 Thanks a lot!
 » 3 months ago, # |   0 I guess for question number 10 sliding window concept will be much more intuitive. Here is my ac solution, #include using namespace std; int main() { int n; cin>>n; vectork(n); for(int i=0;i>k[i]; setst; int ans = 0,j=0; for(int i=0;i
•  » » 3 weeks ago, # ^ |   0 Hi, can you please share with me the expected time complexity of your code and how exactly could you determine if your code could pass the given test cases? It seems to me that complexity would be O(n^2) since there is a while loop inside the outer for loop?Yet your code seems to pass all the test cases within the accepted time limit, HOW?
 » 6 weeks ago, # |   0 For 10. Playlist I used two-pointer technique with sliding window concept, It is much more intuitive.Keep on increasing window length, while we have subarray a with unique numbers. As soon as we find a duplicate, we delete from the array till the subarray does not contain duplicate Spoiler
•  » » 6 weeks ago, # ^ |   0 Main concept is — erase every $k$th element from the circular array(in I,$k=1$).Remember erase function is $O(n)$ so it definitely causes TLE.Use ordered set instead whose erase function complexity is $log(n)$.  int n,k;cin>>n>>k; ordered_set josep; for(int i=1;i<=n;++i)josep.insert(i); int pos=0; while(josep.size()>1) { pos=(pos+k)%(int)josep.size(); cout<<*(josep.find_by_order(pos))<<' '; josep.erase(*(josep.find_by_order(pos))); pos%=(int)josep.size(); } cout<<*(josep.find_by_order(0))<