N robots collisions Solution#7440
Conversation
prashantpiyush1111
left a comment
There was a problem hiding this comment.
No test file found. Please add NRobotsCollisionTest.java as required by contributing guidelines. Also add a Wikipedia or reference URL in the class Javadoc.
| @@ -0,0 +1,54 @@ | |||
| package com.thealgorithms.simulation; | |||
There was a problem hiding this comment.
Missing imports: java.util.ArrayList and java.util.List need to be added here. This is causing the build failure.
| * the direction of the corresponding robot | ||
| * @return the earliest collision time, or -1 if no collision occurs | ||
| */ | ||
| public static int earliestCollisionTime(int n, int[] positions, String directions) { |
There was a problem hiding this comment.
Return type should be double instead of int. Collision time can be fractional — e.g., if distance between two robots is 3, time = 1.5 but int division will incorrectly return 1.
| */ | ||
| public static int earliestCollisionTime(int n, int[] positions, String directions) { | ||
| List<Pair> sortedDirections = new ArrayList<>(); | ||
| for(int i = 0; i < n; i++){ |
There was a problem hiding this comment.
Clang format issue: Please add space after for → for (int i = 0; i < n; i++) {
| } | ||
| sortedDirections.sort((a, b) -> Integer.compare(a.first, b.first)); | ||
| int minTime = Integer.MAX_VALUE; | ||
| for(int i = 1; i < n; i++){ |
There was a problem hiding this comment.
Same clang format issue here as well.
| minTime = Math.min(minTime, sortedDirections.get(i).first - sortedDirections.get(i - 1).first); | ||
| } | ||
| } | ||
| return minTime == Integer.MAX_VALUE ? -1 : minTime / 2; |
There was a problem hiding this comment.
Bug: minTime / 2 does integer division. Change minTime to double or cast: (double) minTime / 2 to handle fractional collision times correctly.
|
Hi @NilayBadjatya! The build is failing due to these issues: 1. Missing imports (Build failure) 2. Clang format issues
3. Missing test file 4. Return type bug Fix these and all CI checks should pass! |
clang-format -i --style=file path/to/your/file.java