-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.java
More file actions
31 lines (22 loc) · 784 Bytes
/
Copy pathEnemy.java
File metadata and controls
31 lines (22 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// "What is the difference between A hero 1 and the enemy 2"
import java.util.Arrays;
import java.lang.Math;
public class Enemy {
public static void main(String[] args) {
int arr[] = { 0, 0, 2, 0, 0, 0,1,2,0 };
int heroIndex = -1;
int minDistance = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 1) {
heroIndex = i;
break;
}
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 2) {
minDistance = Math.min(minDistance, Math.abs(heroIndex - i));
}
}
System.out.println(minDistance == Integer.MAX_VALUE ? "No enemy found!" : "Minimum distance: " + minDistance);
}
}