Hey @swastikswarupmeher , the logi you are using is correct but you are changing the median inside the if and else condition itself . Also so much of conditions are not required . you only need to balance the heaps if the difference between them is 2 otherwise you just have to push it into one of the heaps .
Here is a more clear code to help you understand . I hope it helps !
Mark your doubt as resolved if you have no further queries .