-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay46.java
More file actions
29 lines (25 loc) · 749 Bytes
/
Copy pathDay46.java
File metadata and controls
29 lines (25 loc) · 749 Bytes
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
/*Largest in Array
Given an array arr[]. The task is to find the largest element and return it.
Examples:
Input: arr[] = [1, 8, 7, 56, 90]
Output: 90
Explanation: The largest element of the given array is 90.
Input: arr[] = [5, 5, 5, 5]
Output: 5
Explanation: The largest element of the given array is 5.
Input: arr[] = [10]
Output: 10
Explanation: There is only one element which is the largest. */
/*class Solution {
public int largest(int[] arr) {
int max = arr[0]; // assume first element is largest
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
*/
/*Optimal Approach (O(n) Time, O(1) Space) */