-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOTHDSA.java
More file actions
94 lines (80 loc) · 3.17 KB
/
OTHDSA.java
File metadata and controls
94 lines (80 loc) · 3.17 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.*;
class OTHDSA {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int leftHead = 0, rightHead = 200, numberOfRequests, i, j, temporaryVariable, x, y, seekDistance = 0;
int arrayOfRequests[] = new int[200];
System.out.println("Enter the Number of Track Requests\n");
numberOfRequests = scanner.nextInt();
System.out.println("Enter Track Positions\n");
for(i = 0; i < numberOfRequests; i++)
arrayOfRequests[i] = scanner.nextInt();
for(i = 0; i < numberOfRequests; i++)
for(j = i + 1; j < numberOfRequests; j++)
if(arrayOfRequests[i] > arrayOfRequests[j]){
temporaryVariable = arrayOfRequests[i];
arrayOfRequests[i] = arrayOfRequests[j];
arrayOfRequests[j] = temporaryVariable;
}
x = arrayOfRequests[0] - leftHead;
y = rightHead - arrayOfRequests[numberOfRequests - 1];
if(x <= y){
System.out.println("Left Head is Working\n");
i = 0;
while(arrayOfRequests[i] <= 100){
leftHead = arrayOfRequests[i];
System.out.println("Left Head moves to: "+leftHead);
i = i + 1;
}
x = arrayOfRequests[numberOfRequests-1] - arrayOfRequests[i-1];
y = 200 - arrayOfRequests[i];
if(x <= y){
System.out.println("Left Head continues\n");
while(i <= numberOfRequests - 1){
leftHead = arrayOfRequests[i];
System.out.println("Left Head moves to: "+leftHead);
i = i + 1;
}
}
else{
j = numberOfRequests - 1;
System.out.println("Right Head is Working\n");
while(j >= i){
rightHead = arrayOfRequests[j];
System.out.println("Right Head moves to: "+rightHead);
j = j - 1;
}
}
}
else{
System.out.println("Right Head is Working\n");
i = numberOfRequests - 1;
while(arrayOfRequests[i] >= 100){
rightHead = arrayOfRequests[i];
System.out.println("Right Head moves to: "+rightHead);
i = i - 1;
}
x = arrayOfRequests[i] - 0;
y = arrayOfRequests[i+1] - arrayOfRequests[0];
if(x < y){
System.out.println("Left Head is Working\n");
j = 0;
while(j <= i){
leftHead = arrayOfRequests[j];
System.out.println("Left Head moves to: "+leftHead);
j = j + 1;
}
}
else{
System.out.println("Right Head is Working\n");
while(i >= 0){
rightHead = arrayOfRequests[i];
System.out.println("Right Head moves to: "+rightHead);
i = i - 1;
}
}
}
seekDistance = leftHead + 200 - rightHead;
System.out.println("\nSeek Distance is: "+seekDistance);
}
}