AfterAcademy Tech
•
04 Sep 2020

Difficulty: Easy
Problem Description
Given an array arr[] of size n, write a program to find the largest element in it.
Example 1:
Input: arr[] = [100, 50, 4]
Output: 100
Explanation: 100 is the largest element in the given array.
There are various ways to find the largest element. In each programming language, there is support for finding the max in the library. However, internally they work the same. Below are some discussed ways to do that.
You may try this problem here.
To find the largest element from the array, a simple way is to arrange the elements in ascending order. After sorting, the first element will represent the smallest element, the next element will be the second smallest, and going on, the last element will be the largest element of the array.
Solution Steps
Pseudo Code
int largestElement(int[] arr, int size) {
// sort the array in ascending order
arr.sort()
largest_element = arr[size-1]
return largest_element
}
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(1)
Critical Ideas To Think
Take a max variable and initialize it with the first element of the array. Now start iterating over the array and whenever a larger element encountered then update the max variable otherwise, move forward.
Solution Steps
max variable and initialize it with arr[0]idx to the last idx of the array:arr[idx] > max then update max with arr[idx]3. Return max
Pseudo Code
int largestElement(int[] arr, int size) {
int max = arr[0]
for(int i = 1 to i < size) {
if(max < arr[i]) {
max = arr[i]
}
}
return max
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Critical Ideas To Think
max with arr[0]?max with INT_MIN and start the loop from the 0th index. Will that approach would have worked?
Please comment down below if you have a better insight in the above approach.
Happy Coding, Enjoy Algorithms!
AfterAcademy Tech
Given an array A[] of size n, you need to find the next greater element for each element in the array. Return an array that consists of the next greater element of A[i] at index i.

AfterAcademy Tech
Given an array A[] of n elements and a positive integer K, find the Kth smallest element in the array. It is given that all array elements are distinct.

AfterAcademy Tech
You are given a sorted and infinite array A and an element K. You need to search for the element K in the array. If found, return the index of the element, else return -1.

AfterAcademy Tech
Given an array A[] of size n, find the most frequent element in the array, i.e. the element which occurs the most number of times.
