Skip to content

Commit 1e5f0c1

Browse files
committed
feat: Find largest and second largest elements in array
🧠 Logic: - Initialize `max1` and `max2` with the first element of the array. - Traverse the array: - If current element > `max1`, update `max2 = max1` and `max1 = current element`. - Else if current element > `max2`, update `max2 = current element`. - Added a check for arrays with less than 2 elements (though placed inside the loop — logically should be outside). - Print the largest and second largest elements. Signed-off-by: Somesh diwan <[email protected]>
1 parent 5efe810 commit 1e5f0c1

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed
Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
import java.util.Scanner;
22

3-
public class LargestElementArray
4-
{
5-
public static void main(String[] args)
6-
{
3+
public class LargestElementArray {
4+
public static void main(String[] args) {
75
int A[]={3,9,7,8,12,6,15,5,4,10,1};
86

97
int max1,max2;
108

11-
max1=max2=A[0];
9+
max1=max2=A[0]; //initialize the 1st element in the array.
1210

13-
for(int i=0; i<A.length; i++)
14-
{
15-
if(A[i]>max1)
16-
{
11+
for(int i=0; i<A.length; i++) {
12+
if(A[i]>max1) {
1713
max2=max1;
1814
max1=A[i];
1915
}
20-
else if (A[i]>max2)
21-
{
16+
else if (A[i]>max2) {
2217
max2=A[i];
2318
}
24-
25-
if (A.length < 2)
26-
{
19+
if (A.length < 2) {
2720
System.out.println("Array must have at least two elements.");
2821
return;
2922
}
@@ -33,23 +26,48 @@ else if (A[i]>max2)
3326
}
3427
}
3528

29+
/*
30+
A swapping pattern that's commonly used when tracking two maximum values.
31+
32+
1. First line: The current maximum value () becomes the second maximum () `max2=max1;``max1``max2`
33+
2. Second line: The new largest value from the array () becomes the new maximum () `max1=A[i];``A[i]``max1`
34+
35+
This is a typical technique when you need to keep track of both the largest and second-largest values.
36+
37+
When a new maximum is found, the old maximum doesn't just get discarded - it becomes the second maximum.
38+
39+
For example,
40+
41+
If:
42+
- was 10 `max1`
43+
- was 8 `max2`
44+
- And we find is 15 `A[i]`
45+
46+
The execution would go:
47+
1. → becomes 10 `max2 = max1``max2`
48+
2. → becomes 15 `max1 = A[i]``max1`
49+
*/
50+
3651
/*
3752
Time Complexity
38-
Initialization:
3953
54+
Initialization:
4055
Assignments for max1 and max2: O(1).
56+
4157
Loop through the array (for loop):
4258
4359
The program iterates through the array exactly once.
60+
4461
In each iteration, it performs a constant amount of work (comparison and assignment).
62+
4563
Time complexity of this loop: O(n), where n is the size of the array.
4664
Thus, the overall time complexity is O(n).
4765
4866
Space Complexity
4967
The space used by the program includes:
68+
5069
The input array A[]: O(n), where n is the number of elements in the array.
5170
Variables max1 and max2: O(1) (constant space).
5271
Other local variables (like i in the loop): O(1).
5372
The program does not use any extra data structures; it operates directly on the input array.
54-
55-
*/
73+
*/

0 commit comments

Comments
 (0)