Comments

How many questions did you solve in the contest ?? Don't hate the contest . Some smart people put in a lot of efforts to make such contests. On the other hand, these cheaters dishonor the sport.

Can't we simply sort the array and apply 2 pointers from front and back ? If the elements are different we add 1 and move both the pointers towards the center. Else if they are same , then simply add (j-i+1) and break the loop.