Difficulty: Easy
Understanding The Problem
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.
Solutions
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.
- Sorting: Sort the array in ascending order and the last element will be at the last index.
- Traverse: Traverse the array while updating the max value.
You may try this problem here.
1. Sorting
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
- Sort the array.
- Return the element at the last index of the array.
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
- Why did we return the last element?
- If we would have sorted the array in descending order, then which element we should have returned?
- Sorting the array arranges each and every element and that will help us to find the largest, 2nd largest, 3rd largest elements and so on in constant time. But is it okay to do that much computation here when we just have to find the largest element?
2. Traverse
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
- Create a
max
variable and initialize it witharr[0]
- Iterate from the first
idx
to the lastidx
of the array:
- If
arr[idx] > max
then updatemax
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
- Why did we initialize
max
witharr[0]
? - What if we initialize
max
withINT_MIN
and start the loop from the0th
index. Will that approach would have worked? - What changes do we have to do, when we have to find the 2nd largest element?
Comparison of Solutions
Suggested Problems To Solve
- Find the smallest element of the array
- Find the 2nd largest element of the array
- Find the Kth largest element of the array
Please comment down below if you have a better insight in the above approach.
Happy Coding, Enjoy Algorithms!
Given a sorted integer array, find the floor and ceil of a given number in it. The floor and ceil map the given number to the largest previous or the smallest following integer in the array.
More precisely, for a number x
, floor[x]
is the largest integer in the array less than or equal to x
, and ceil[x]
is the smallest integer in the array greater than or equal to x
. If the floor or ceil doesn’t exist, consider it to be -1. For example,
Input:
nums[] = [1, 4, 6, 8, 9]
Number: 0 to 10
Output:
Number 0 —> ceil is 1, floor is -1
Number 1 —> ceil is 1, floor is 1
Number 2 —> ceil is 4, floor is 1
Number 3 —> ceil is 4, floor is 1
Number 4 —> ceil is 4, floor is 4
Number 5 —> ceil is 6, floor is 4
Number 6 —> ceil is 6, floor is 6
Number 7 —> ceil is 8, floor is 6
Number 8
—> ceil is 8, floor is 8
Number 9 —> ceil is 9, floor is 9
Number 10 —> ceil is -1, floor is 9
Practice this problem
A simple solution would be to run a linear search on the array and find the largest integer in the array less than or equal to x
and the smallest integer in the array greater than or equal to x
. That would be
the floor and ceil of the number x
, respectively. The problem with this approach is that its worst-case time complexity is O[n], where n
is the size of the input.
We can easily solve this problem in O[log[n]] time by modifying the binary search algorithm. The basic idea remains the same. We find
the middle element and move to the left or right subarray based on comparison result with the given number. Following is the complete algorithm:
findCeil[nums[low, high], x]
Initialize ceil to -1 and iterate till our search space is exhausted.
- If
x
is equal to the middle element, it is the ceil. - If
x
is less than the middle element, the ceil exists in subarraynums[low…mid]
; update ceil to the middle value and reduce our search space to the left subarraynums[low…mid-1]
. - If
x
is more than the middle element, the ceil exists in the right subarraynums[mid+1…high]
.
findFloor[nums[low, high], x]
Initialize floor to -1 and iterate till our search space is exhausted.
- If
x
is equal to the middle element, it is the floor. - If
x
is less than the middle element, the floor exists in the left subarraynums[low…mid-1]
. - If
x
is more than the middle element, the floor exists in subarraynums[mid…high]
; update floor to the middle element and reduce our search space to the right subarraynums[mid+1…high]
.
The algorithm can be implemented as follows in C, Java, and Python:
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include // Function to find the ceil of `x` in a sorted array nums[0…n-1] // i.e., the smallest integer greater than or equal to `x` intgetCeil[int nums[],intn,int x] { // search space is nums[low…high] intlow =0,high=n- 1,mid; // initialize ceil to -1 int ceil=-1; // loop till the search space is exhausted while[low ceil is -1, floor is 9 The time complexity of the above solution is O[log[n]] and doesn’t require any extra space. Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding :] How do you find the number of elements greater smaller than an element in an array?The simplest trick is sort the array first and count the number of elements in the array. So for example if you have 10 elements then your first element is less than 9 and likewise the second element is smaller than 8 and so on.
How do you find the largest and smallest number in an array?Algorithm to find the smallest and largest numbers in an array. Input the array elements.. Initialize small = large = arr[0]. Repeat from i = 2 to n.. if[arr[i] > large]. large = arr[i]. if[arr[i] < small]. small = arr[i]. Print small and large.. How do you find the largest and smallest number in an unsorted array in C?Logic to find the largest and smallest element in the array. Create two intermediate variables small and large.. Initialize the small and large variable with arr[0].. Now traverse the array iteratively and keep track of the smallest and largest element until the end of the array.. Chủ Đề |