Write a java program to provide the optimised solution for the below requirements.
- Program should have three input parameters:
- Total number of rooms
- Minimum rooms per building
- Maximum room per building.
- Should calculate the number of rooms per building between min-max number provided
- Choose a factor that gives minimum reminder
- If there are more than one factor giving minimum reminder, choose the highest factor.
package com.builder; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class BuildingOptimizer { /** * @param args */ public static void main(String[] args) { printOptimizedSolution(16, 4, 8); } public static void printOptimizedSolution(int totalNum, int minNum, int maxNum) { Map<Integer, Integer> reminderMap = new HashMap<Integer, Integer>(); StringBuffer output = new StringBuffer("Optimised Solution is : " ); for(int statNum = maxNum; statNum <= minNum; statNum--) { if(totalNum % statNum == 0) { reminderMap.put(0, statNum); break; } } if(reminderMap.size() == 0) { for(int statNum = minNum; statNum <= maxNum; statNum++) { reminderMap.put((totalNum % statNum), statNum); } } List<Integer> remindersList = new ArrayList<Integer>(reminderMap.keySet()); Collections.sort(remindersList); Integer devider = reminderMap.get(remindersList.get(0)); Integer divisions = totalNum / devider; for(int i = 1; i <= divisions; i++ ) { output.append(devider); if(i != divisions) { output.append(", "); // comma should not append at the end } } if(remindersList.get(0) != 0) { output.append(", ").append(remindersList.get(0)); } System.out.println(output); } }
No comments:
Post a Comment