guys I got the logic part but I m unable to implement it .it would be great and really appreciated if you kindly share your approach and your solution with little explaination so that I could get it .it is a problem from codechef may long challenge 2020 here is the link to the problem https://www.codechef.com/MAY20B/problems/CHANDF just copy it and paste in the URL section. thanks for your time.if anyone have any doubt you can ask about it too related to the above mentioned problem.