Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java and a time complexity of O(n^2) (2) Harry was digging a hole to find hidden treasure in his grandmother's backyard. There he discovered

Using Java and a time complexity of O(n^2)

image text in transcribed

(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Creating A Database In Filemaker Pro Visual QuickProject Guide

Authors: Steven A. Schwartz

1st Edition

0321321219, 978-0321321213

More Books

Students also viewed these Databases questions