-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondMax.java
More file actions
46 lines (45 loc) · 1.06 KB
/
SecondMax.java
File metadata and controls
46 lines (45 loc) · 1.06 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
/*
Q5. Take an integer array, store it in a Vector, and determine the second largest number without sorting.
What you practice:
Max/second-max tracking
Using Vector get() method
Efficient single-pass logic
*/
import java.util.*;
public class SecondMax
{
public static void main(String x[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array: ");
int size = sc.nextInt();
int nums[] = new int[size];
System.out.print("Enter the values in array: ");
for(int i = 0; i < nums.length; i++)
{
nums[i] = sc.nextInt();
}
// copy values from array to vector
Vector vr = new Vector();
for(int i = 0; i < nums.length; i++)
{
vr.add(nums[i]);
}
System.out.println(vr);
int max = 0, secondMax = 0;
for(int i = 0; i < vr.size(); i++)
{
if((int)vr.get(i) > max)
{
secondMax = max;
max = (int)vr.get(i);
}
if((int)vr.get(i) > secondMax && (int)vr.get(i) < max)
{
secondMax = (int)vr.get(i);
}
}
System.out.println("The max is: "+max);
System.out.println("The second max is: "+secondMax);
}
}