Commit 1e5f0c1
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
1 file changed
+35
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 3 | + | |
| 4 | + | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | | - | |
| 9 | + | |
12 | 10 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 11 | + | |
| 12 | + | |
17 | 13 | | |
18 | 14 | | |
19 | 15 | | |
20 | | - | |
21 | | - | |
| 16 | + | |
22 | 17 | | |
23 | 18 | | |
24 | | - | |
25 | | - | |
26 | | - | |
| 19 | + | |
27 | 20 | | |
28 | 21 | | |
29 | 22 | | |
| |||
33 | 26 | | |
34 | 27 | | |
35 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
36 | 51 | | |
37 | 52 | | |
38 | | - | |
39 | 53 | | |
| 54 | + | |
40 | 55 | | |
| 56 | + | |
41 | 57 | | |
42 | 58 | | |
43 | 59 | | |
| 60 | + | |
44 | 61 | | |
| 62 | + | |
45 | 63 | | |
46 | 64 | | |
47 | 65 | | |
48 | 66 | | |
49 | 67 | | |
| 68 | + | |
50 | 69 | | |
51 | 70 | | |
52 | 71 | | |
53 | 72 | | |
54 | | - | |
55 | | - | |
| 73 | + | |
0 commit comments