-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNthLargest.java
More file actions
60 lines (47 loc) · 1.2 KB
/
NthLargest.java
File metadata and controls
60 lines (47 loc) · 1.2 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
package com.synechron.aqr.nikhil;
public class NthLargest {
int array[] = { 10, 19, 2, 3, 1, 98, 75, 65, 8500, 850000 };
public static void main(String args[]) throws MyException {
new NthLargest(Integer.parseInt(args[0]));
}
public NthLargest(int n) throws MyException {
int skip[] = new int[n];
int i, j, k, l, skipIndex = 0, maxJ = 0;
int nthMaxValue = 0;
boolean skipLoop = false;
if (n > array.length)
throw new MyException("Out of scope to grab this number");
for (i = 0; i < array.length; i++) {
int val = array[i];
maxJ = i;
for (j = 0; j < array.length; j++) {
for (k = 0; k < skip.length; k++)
if (skip[k] == j)
skipLoop = true;
if (skipLoop) {
skipLoop = false;
continue;
}
if (val < array[j]) {
val = array[j];
maxJ = j;
}
}
if (skipIndex == n) {
nthMaxValue = array[skip[n - 1]];
break;
}
skip[skipIndex++] = maxJ;
}
System.out.println(n + " largest value is : " + nthMaxValue);
}
}
class MyException extends Exception {
String msg = "";
MyException(String msg) {
this.msg = msg;
}
public String getMessage() {
return msg;
}
}