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
mdparvejalam687in 11 hours
Java Solution
 
int larger = arr[0];      for(int i=0; i<arr.length;i++){      if(larger<arr[i]){          larger= arr[i];      }      }      return larger;
0
cchaithanyata3 hours ago
#include<stdio.h>
#include<conio.h>
int main() 
{
int a[1000], i,n, max;
printf("enter size of the array:") ;
scanf("%d", &n) ;
printf("enter elements in an array:") ;
for(i=0;i<n;i++) 
{
scanf("%d", &a[i]) ;
}
max=a[0];
for(i=1;i<n;i++) 
{
if(max<a[i]) 
max=a[i];
}
printf("\n maximum of array is:%d", max) ;
return 0;
}
+1
rahilarahman1 week ago
simple python codedef largest( arr, n):   arr.sort()   return arr[n-1]
+1
mp96q2iqslxyjaqior18jrs0xwp7wz21845kzrfr1 week ago
Python
 
def largest( arr, n):   arr.sort(reverse=True)   return arr[0]
+1
harshscode1 week ago
 int m=0;       for(int i=0;i<n;i++)       {           if(a[i]>m)           m=a[i];       }       return m;
0
vikassinghwow1 week ago
class Compute {      public int largest(int arr[], int n)   {       int largest=arr[0];       for(int i=1;i<arr.length;i++)       {           if(largest<arr[i])           {               largest=arr[i];           }       }       return largest;   }}
0
payalchaudhary4422 weeks ago
//JAVA CODE:
                   
              class Compute {   public int largest(int arr[], int n)   {        int larger = arr[0];       for(int i=0; i<arr.length;i++){       if(larger<arr[i]){           larger= arr[i];       }       }       return larger;   }}
 
0
diagovenk2 weeks ago
Hello everyone. Most easy with just one loop and one if statement without using any inbuilt function. java solution.
public int largest(int arr[], int n)   {       int largest = 0;       for(int i=0; i<n; i++){           if(arr[i]>largest){               largest = arr[i];           }       }       return largest;   }
0
jeetsorathiya2 weeks ago
my solution in java
 
just use sort function
 
Arrays.sort(arr);   return arr[n-1];
+1
akash2420182 weeks ago
#python3 Code 
 
def largest( arr, n):   maxium=arr[0]   for i in range (n):       if(arr[i]>maxium):           maxium=arr[i]   return maxium
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.