Largest Element in the Array

Easy
0/40
Average time to solve is 10m
339 upvotes
Asked in companies
Morgan StanleyInfosysOracle

Problem statement

Given an array ‘arr’ of size ‘n’ find the largest element in the array.


Example:

Input: 'n' = 5, 'arr' = [1, 2, 3, 4, 5]

Output: 5

Explanation: From the array {1, 2, 3, 4, 5}, the largest element is 5.
Detailed explanation ( Input/output format, Notes, Images )

Input Format :

The first line will contain a single integer 'n', the size of the array ‘arr’ 

The second line will contain ‘n’ integers representing the array elements.

Output format :

Print the maximum of elements.

Note :

You don't need to print anything. It has already been taken care of. Just implement the given function.
Sample input 1:
6
4 7 8 6 7 6 
Sample output 1:
8
Explanation of sample input 1:
The answer is 8.
From {4 7 8 6 7 6}, 8 is the largest element.
Sample input 2:
8
5 9 3 4 8 4 3 10 
Sample output 2:
10
Expected Time Complexity:
O(n), Where ‘n’ is the size of an input array ‘arr’.
Constraints :
1 <= 'n' <= 10^5
1 <= 'arr[i]' <= 10^9

Time Limit: 1 sec
Hint

Think about how to iterate through the array and keep track of the largest element encountered so far.

Approaches (1)
Brute Force

We can create a temporary variable ‘temp’ and traverse the array and update the temporary variable ‘temp’ with the max of the elements.


 

The steps are as follows:-

  1. Create a temporary variable 'maxElement' and initialize it with 0.
  2. Traverse the array ‘arr’.
  3. Update ‘maxElement’ with the max of the current element of arr or 'maxElement'.
  4. Return 'maxElement'.
Time Complexity

O(n), Where ‘n’ is the size of an input array ‘arr’.
 

We are traversing the array of size ‘n’

.

Hence time complexity is O(n).

Space Complexity

O(1).

 

We are creating a temporary variable that takes constant space with respect to array size.

 

Hence space complexity is O(1).

Code Solution
(100% EXP penalty)
Largest Element in the Array
All tags
Sort by
Search icon

Interview problems

Solution By Java8

public class Solution {

 

    static int largestElement(int[] arr, int n) {

        

        return Arrays.stream(arr).max().getAsInt();

 

    }

}

12 views
0 replies
0 upvotes

Interview problems

Largest Element in the Array

import java.util.* ;

import java.io.*; 

 

public class Solution {

 

    static int largestElement(int[] arr, int n) {

        // Write your code here.

        int max = arr[0];

        for(int i=0; i<arr.length; i++){

            if(arr[i] > max){

                max = arr[i];

            }

        }

        return max;

    }

}

14 views
0 replies
1 upvote

Interview problems

Java Program

public class Solution {

 

    static int largestElement(int[] arr, int n) {

        // Write your code here.

        int max=arr[0];

        for(int i=1;i<n;i++){

            if(arr[i]>max)

            max=arr[i];

        }

return max;

    }

}

57 views
0 replies
0 upvotes

Interview problems

JAVA

import java.util.* ;

import java.io.*; 

 

public class Solution {

 

    static int largestElement(int[] arr, int n) {

        // Write your code here.

        int max=Integer.MIN_VALUE;

        int min=0;

        for(int i=0;i<arr.length;i++){

            if(arr[i]>max){

                max=arr[i];

               // min=max;

            }

        }

        return max;

 

    }

}

43 views
0 replies
0 upvotes

Interview problems

Largest Element in the Array using stream API

import java.util.* ;

import java.io.*; 

 

public class Solution {

 

    static int largestElement(int[] arr, int n) {

       

      return  Arrays.stream(arr).max().getAsInt();

 

    }

}

25 views
0 replies
0 upvotes

Interview problems

Easy c/c++ solution

int largestElement(vector<int> &arr, int n) {

    // Write your code here.

    int max = INT_MIN;

    for(int i=0;i<n;i++)

    {

        if(arr[i] >= max)

        {

            max = arr[i];

        }

    }

    return max;

}

beginners

programming

datastructures

85 views
0 replies
0 upvotes

Interview problems

Logic using C++ (Better than 100%)

#include <bits/stdc++.h> 

int largestElement(vector<int> &arr, int n) {

    int largest=0;

    for(int i=0;i<n;i++){

        if(arr[i]>largest){

            largest=arr[i];

        }

    }

    return largest;

    // Write your code here.

}

37 views
0 replies
2 upvotes

Interview problems

c++ solution

#include <bits/stdc++.h> 
int largestElement(vector<int> &arr, int n) {
    int max=0;
    // Write your code here.
   for (int i=0;i<n;i++)
   {
       if(arr[i]>max)
       {
           max=arr[i];
       }
   }
   return max;
}
40 views
2 replies
0 upvotes

Interview problems

Python EASY soln with O(n) complexity

def largestElement(arr: [], n: int) -> int:

max =arr[0]

for i in range (n):

if arr[i] > max :

max = arr[i]

 

return max

pass

 

51 views
0 replies
2 upvotes

Interview problems

Largest Element in the Array - JAVA

import java.util.* ;

import java.io.*; 

 

public class Solution {

 

    static int largestElement(int[] arr, int n) {

         int temp = 0;

       for(int i =0 ;i<n;i++){

           if(temp<arr[i]){

               temp = arr[i];

           }

       }

        return temp;

    }

}
58 views
0 replies
2 upvotes
Full screen
Console