Question: Answer the following question using Java (2) Harry was digging a hole to find hidden treasure in his grandmother's backyard. There he discovered an old
Answer the following question using Java

(2) Harry was digging a hole to find hidden treasure in his grandmother's backyard. There he discovered an old piece of paper with N integers written on it. From the game of numbers, Harry has learnt that a trio of numbers is special if and only if their sum is divisible by a mythical constant M Harry tries to find out how many distinct triplets of numbers, from the piece of paper, have their sum divisible by M. Unfortunately, this problem is hard for him to crack and he needs your help Input Take two integers as input, N and M, representing the number of integers in the sequence and the mythical constant respectively. Next, take N integers as input (duplicate integers are allowed) Output The output should contain only one integer, the number of distinct triplets which have their sum divisible by M Sample Input 10 5 1 10 4 3 25 01 9 5 Sample Output 26 Expected Time Complexity: O(n) Explanation There are 26 special trios for the above sample input: (0, 1, 2); (0, 1, 8); (0, 2, 5); (0, 2, 6); For the first trio (0, 1, 2): 1+10+4 15 15 is divisible by 5 For the second trio (0, 1, 8): 1+10+9 20 20 is divisible by 5 Note: Here we only show the positions (in input) of numbers in each trio
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
