Remove element

Heya, it's 12:31 midnight and I thought of blogging on this problem that I took in the afternoon and I had taken just 5 mins to get this pass all the Test Cases including the coding part and ideation phase. Well, the idea just popped into my head, you should really give it a try before moving to even the code part, just think about what you have to do to get the condition satisfied and try on your own. Well, you can try for a max of at least 45 mins to think about this in my opinion or you can even try and go for a day long thinking too, not restricting you for that, it's individual discretion. 
And yeah there was a very interesting corner case in this problem too that I considered in my mind just when the Test Cases were running for the First Time and just then it failed, then I made the changes and I knew that it must be because of that corner case only, well, obviously bcz the logic for the problem was so concrete that I personally didn't feel like there would be some problem with this to be honest, and it was that only, the only 1 corner case, when we have just 1 elem.
Well, the code and the explanation part is mentioned below, and I would be hitting the hay after this most probably or I should say hopefully...




Coding Implementation:-

for _ in range(int(input())):
    n,k=map(int,input().split())
    nums=list(map(int,input().split()))
    print("YES") if(len(nums)==1) else print("YES")
    if((min(nums)+max(nums))<=k) else print("NO")
   

Explanation:-
Simply, we have to understand the fact that here we need to get the elements removed un till there is a single element left and to do that we need to have pairs of elements whose sum would be less or equal to the number k, now to get that condition met we will be required to have a number( which should be most probably minimum so as to equalize the bigger numbers present in the array). So, just consider this array for example--> [1,2,3,4,5], now in this array( and in every array ) what we would do is that to get the summation less equal to k we will take a smaller elem and a bigger elem to get the bigger elem removed, like here smaller elem would be 1, so suppose k is 5, so we can pair 1 with 4,3,2 but with 5 the summation would become 6 so that's not possible, so the overall idea is that if we have to get just 1 element at the end then that elem is necessary to be the smallest one in the array, and this means that all other elems should be removed, so we always look for the greatest elems and if they cannot pair with smallest ones like here 1 & 5 exceed k(that is 5) so this is not possible. If array would have been-->[0,2,3,4,5] then it was possible bcz we would make pairs like-->(0,5),(0,4),(0,3),(0,2),(0,1). That's the overall logic of this code. For the implementation part, we just check firstly for a corner case when it's just 1 elem then k limit is of no use so just print yes and in normal condition compare the smallest elem with the biggest elem if they are satisfying the condition then yes else no. PEACE!

Comments

Popular Posts