largest is initialized with the first element of the array (arr[0]) because there should be at least one element in the array. We assume the first element is the largest initially.1), the code compares each element to the current largest. If a larger element is found, the value of largest is updated.largest holds the largest value in the array, which is returned.Here’s the code:
class Solution {
public:
int largest(vector<int> &arr) {
int largest=arr[0];
for(int i=1;i<n;i++){
if(arr[i]>largest)
largest=arr[i];
}
return largest;
}
};