Largest Element in Array | Practice | GeeksforGeeks

Given an array A[] of size n. The task is to find the largest element in it.
 
Example 1:
Input:
n = 5
A[] = {1, 8, 7, 56, 90}
Output:
90
Explanation:
The largest element of given array is 90.
 
Example 2:
Input:
n = 7
A[] = {1, 2, 0, 3, 2, 4, 5}
Output:
5
Explanation:
The largest element of given array is 5.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function largest() which takes the array A[] and its size n as inputs and returns the maximum element in the array.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= n<= 103
0 <= A[i] <= 103
Array may contain duplicate elements. 
0
user_oaliin 10 hours
CPP- SIMPLE SOLUTION
 int largest(vector<int> &arr, int n)
    {
       int max;
       max=arr[0];
       
       for(int i=0;i<n;i++)
       if(arr[i]>=max){
           max=arr[i];
       }
    return max;
    }
0
swatidubey9145 hours ago
Best solution in JAVA.
 
int largest(int arr[], int n) {   int max = 0;   for(int i = 0 ; i < n ; i++){       if(arr[i] > max){           max = arr[i];       }   }   return max;}
0
denishfuletra1 day ago
Simple and Sweet solution;
 
class Compute {      public int largest(int arr[], int n)   {       Arrays.sort(arr);       int lar = arr[n-1];       return lar;   }}
 
+1
monikjain20021 day ago
class Solution{public:   int largest(vector<int> &arr, int n)   {   int max=INT_MIN;              for(int i=0;i<n;i++){           if(arr[i]>max){               max=arr[i];           }       }       return max;      }};
0
evgenykalldx2 days ago
def largest( arr, n):   return max(arr)
0
badakakash1233 days ago
SMALL LOGIC  JAVA         Arrays.sort(arr);       return arr[n-1];      
0
arnadojgil4 days ago
public static void main(String[] args) {    int [] A = {1, 8, 7, 56, 90};    System.out.println(largest(A,5));}public static int largest(int arr[], int n) {    int high=0;    for (int i = 0; i < arr.length; i++) {        if(high < arr[i]){            high = arr[i];        }    }    return high;}
0
singhanurudh064 days ago
int large =arr[0];  for(int i=1;i<n;i++){      if(arr[i]>large){      large =arr[i];      }  }  return large; }
0
lukmaannadaf1 week ago
def largest( arr, n):   return max(arr)
0
mdarman012562 weeks ago
int largest(int arr[], int n) {   int largest =arr[0];   for(int i=1;i<n;i++)   {       if(arr[i]>largest)              {       largest =arr[i];                  }   }   return largest;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.