Loading...
Loading...
Loading...
A hash table with B buckets files each student into bucket Math.abs(s.hashCode() % B), where hashCode() returns Objects.hash(roll, name). Write Student with a matching equals() and that hashCode(). Read n students and print how many land in each bucket, then the size of the fullest bucket.
Bucket <b>: <count> for b = 0 to B − 1, then Largest: <max>.Example
Input
4 4 7 Diya 7 Diya 12 Aarav 3 Kavya
Output
Bucket 0: 1 Bucket 1: 0 Bucket 2: 1 Bucket 3: 2 Largest: 2
The two Diya records are equal, so they have the same hash code (2131719) and land in the same bucket, 3.
Try the problem first. Open one hint at a time.
The % of a negative hash code is negative; Math.abs turns it into a bucket number.
Keep an int[] of size B and add 1 to the bucket of each student.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor