-
Notifications
You must be signed in to change notification settings - Fork 0
/
A_X_Axis.java
37 lines (33 loc) · 1.01 KB
/
A_X_Axis.java
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
32
33
34
35
36
37
import java.util.*;
public class A_X_Axis {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt(); // Number of test cases
while (t-- > 0) {
int[] arr = new int[3];
for (int i = 0; i < 3; i++) { // x1,x2,x3
arr[i] = sc.nextInt();
}
System.out.println(findSmallest(arr));
}
}
private static int findSmallest(int[] nums) {
int n = nums.length;
int minDis = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
int currentdis = 0;
int left = i - 1;
while (left >= 0) {
currentdis += Math.abs(nums[left] - nums[i]);
left--;
}
int right = i + 1;
while (right < n) {
currentdis += Math.abs(nums[right] - nums[i]);
right++;
}
minDis = Math.min(minDis, currentdis);
}
return minDis;
}
}