Question: *USING JAVA* Time Complexity O(n^2) Harry was digging a hole to find hidden treasure in his grandmothers backyard. There he discovered an old piece of

*USING JAVA* Time Complexity O(n^2)

Harry was digging a hole to find hidden treasure in his grandmothers 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 2 5 0 1 9 5

Sample Output

2 6

Expected Time Complexity: O(n^2)

Explanation

There are 26 special trios for the above sample input: (0, 1, 2); (0, 1, 8); (0, 2, 5); (0, 2, 6); (0, 2, 9); (0, 3, 7); (0, 5, 8); (0, 6, 8); (0, 8, 9); (1, 2, 7); (1, 3, 4); (1, 5, 6); (1, 5, 9); (1, 6, 9); (1, 7, 8); (2, 4, 8); (2, 5, 7); (2, 6, 7); (2, 7, 9); (3, 4, 5); (3, 4, 6); (3, 4, 9); (5, 6, 9); (5, 7, 8); (6, 7, 8); (7, 8, 9);

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!