Suppose there are 20 candies , 6 friends and value of k = 4 . Now , according to you 20%6 = 2 and since 2%4 is not = 0 , therefore , you say there is no possible distribution and output answer as -1 . But the correct answer is 2 . Suppose you throw k = 4 candies twice . So , now the candies remaining are = 12 , and this can be distributed equally among the 6 friends .