Skip to content

Commit 70d3580

Browse files
authored
Create linersearch.py
1 parent e2b7bd3 commit 70d3580

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

linersearch.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Java code to find largest three elements
2+
// in an array
3+
4+
class PrintLargest {
5+
/* Function to print three largest elements */
6+
static void print3largest(int arr[], int arr_size)
7+
{
8+
int i, first, second, third;
9+
10+
/* There should be atleast three elements */
11+
if (arr_size < 3) {
12+
System.out.print(" Invalid Input ");
13+
return;
14+
}
15+
16+
third = first = second = Integer.MIN_VALUE;
17+
for (i = 0; i < arr_size; i++) {
18+
/* If current element is greater than
19+
first*/
20+
if (arr[i] > first) {
21+
third = second;
22+
second = first;
23+
first = arr[i];
24+
}
25+
26+
/* If arr[i] is in between first and
27+
second then update second */
28+
else if (arr[i] > second) {
29+
third = second;
30+
second = arr[i];
31+
}
32+
33+
else if (arr[i] > third)
34+
third = arr[i];
35+
}
36+
37+
System.out.println("Three largest elements are " + first + " " + second + " " + third);
38+
}
39+
40+
/* Driver program to test above function*/
41+
public static void main(String[] args)
42+
{
43+
int arr[] = { 12, 13, 1, 10, 34, 1 };
44+
int n = arr.length;
45+
print3largest(arr, n);
46+
}
47+
}
48+
/*This code is contributed by Prakriti Gupta
49+
and edited by Ayush Singla(@ayusin51)*/

0 commit comments

Comments
 (0)