Skip to content

Commit 909147a

Browse files
Array Questions
Program to find largest element in an array using c++
1 parent 52147ae commit 909147a

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

CodeChef/array.cxx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//Program to find largest element in an array using c ++
2+
3+
// in arr[] of size n
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
 
10+
11+
int largest(int arr[], int n, int i)
12+
13+
{
14+
15+
    // last index
16+
17+
    // return the element
18+
19+
    if (i == n - 1) {
20+
21+
        return arr[i];
22+
23+
    }
24+
25+
26+
    // find the maximum from rest of the array
27+
28+
    int recMax = largest(arr, n, i + 1);
29+
30+
 
31+
32+
    // compare with i-th element and return
33+
34+
    return max(recMax, arr[i]);
35+
36+
}
37+
38+
 
39+
40+
// Driver Code
41+
42+
int main()
43+
44+
{
45+
46+
    int arr[] = { 10, 324, 45, 90, 9808 };
47+
48+
    int n = sizeof(arr) / sizeof(arr[0]);
49+
50+
    cout << "Largest in given array is "
51+
52+
         << largest(arr, n, 0);
53+
54+
    return 0;
55+
56+
}
57+

0 commit comments

Comments
 (0)