Java Program to Group numbers by remainder with Explanation
Java
Medium
Collections
28 views
2 min read
229 words
This problem helps you practice core Java fundamentals in a practical way. It builds intuition around remainder, list, group. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Task: group numbers by (value % m) and return a map remainder -> list of numbers.
Input Format
An integer n (size of array)
Then n integers (array elements)
An integer m
Output Format
A map where:
• key → value % m (remainder)
• value → list of numbers having that remainder
Constraints
• n ≥ 0
• m > 0
• Maintain the original order of numbers inside each list
Concept Explanation
Each number is grouped based on its remainder when divided by m.
All numbers with the same remainder go into the same list.
Step-by-Step Logic
1.Read integer n and the array elements.
2.Read integer m.
3.Create an empty Map.
4.Loop through each number in the array:
• Compute rem = number % m.
5.If rem is not already a key in the map:
• Create a new empty list and put it in the map.
6.Add the current number to the list for key rem.
7.Continue for all numbers.
8.Return or print the map.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
static java.util.Map<Integer,java.util.List<Integer>> groupByRemainder(int[] a,int m){java.util.HashMap<Integer,java.util.List<Integer>> map=new java.util.HashMap<>();for(int x:a){int r=x%m; if(r<0) r+=m; map.computeIfAbsent(r,k->new java.util.ArrayList<>()).add(x);}return map;}
Output Example
Output:
0 -> [3, 6]
1 -> [1, 4]
2 -> [2, 5]
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Task: group numbers by (value % m) and return a map remainder -> list of numbers.
Input / Output
Input
An integer n (size of array)
Then n integers (array elements)
An integer m
Output
A map where:
• key → value % m (remainder)
• value → list of numbers having that remainder
Constraints
• n ≥ 0
• m > 0
• Maintain the original order of numbers inside each list
Examples
Output:
0 -> [3, 6]
1 -> [1, 4]
2 -> [2, 5]
Explanation
Concept Explanation
Each number is grouped based on its remainder when divided by m.
All numbers with the same remainder go into the same list.
Step-by-Step Explanation
1.Read integer n and the array elements.
2.Read integer m.
3.Create an empty Map.
4.Loop through each number in the array:
• Compute rem = number % m.
5.If rem is not already a key in the map:
• Create a new empty list and put it in the map.
6.Add the current number to the list for key rem.
7.Continue for all numbers.
8.Return or print the map.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
static java.util.Map<Integer,java.util.List<Integer>> groupByRemainder(int[] a,int m){java.util.HashMap<Integer,java.util.List<Integer>> map=new java.util.HashMap<>();for(int x:a){int r=x%m; if(r<0) r+=m; map.computeIfAbsent(r,k->new java.util.ArrayList<>()).add(x);}return map;}
Solutions (0)
No solutions submitted yet. Be the first!