Given an array of integers nums, return the value of the largest element in the array
Input: nums = [3, 3, 6, 1]
Output: 6
Explanation: The largest element in array is 6
Input: nums = [3, 3, 0, 99, -40]
Output: 99
Explanation: The largest element in array is 99
Input: nums = [-4, -3, 0, 1, -8]
Imagine being a teacher and having a list of marks obtained by your students in a recent test. The task is to find out which student scored the highest marks. One simple way to do this is by sorting the marks in ascending order and then taking the last element from the sorted list.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
static int largestElement(std::vector<int>& nums) {
// Sort the array
std::sort(nums.begin(), nums.end());
// Largest element will be at the last index of the array.
int largest = nums[nums.size() - 1];
// Return the largest element in array.
return largest;
}
};
int main() {
std::vector<int> nums = {3, 2, 1, 5, 2};
// Create an instance of the Solution class
Solution sol;
int largest = sol.largestElement(nums);
// Print the largest element.
std::cout << largest << std::endl;
return 0;
}
import java.util.*;
class Solution {
public static int largestElement(int[] nums) {
// Sort array
Arrays.sort(nums);
/*Largest element will be at
the last index of the array.*/
int largest = nums[nums.length - 1];
//Return the largest element in array.
return largest;
}
public static void main(String[] args) {
int[] nums = {3,2,1,5,2};
//Make an intance of the Solution class
Solution sol = new Solution();
int largest = sol.largestElement(nums);
// Print the largest element.
System.out.print(largest);
}
}
from typing import List
class Solution:
def largestElement(self, nums):
# Sort the list
nums.sort()
# Largest element will be
# at the last index of the list
largest = nums[-1]
# Return the largest element
return largest
# Main function
if __name__ == "__main__":
nums = [3, 2, 1, 5, 2]
# Create an instance of the Solution class
sol = Solution()
largest = sol.largestElement(nums)
# Print the largest element
print(largest)
class Solution {
largestElement(nums) {
// Sort array
nums.sort((a, b) => a - b);
/** The largest element will be at the
last index of the array*/
const largest = nums[nums.length - 1];
// Return the largest element
return largest;
}
}
const nums = [3, 2, 1, 5, 2];
// Create an instance of the Solution class
const sol = new Solution();
const largest = sol.largestElement(nums);
// Print the largest element
console.log(largest);
Imagine being a teacher having a list of marks obtained by your students in a recent test. The ask is to find out which student scored the highest marks.
Now to find the highest marks in the list, keep track of the marks encountered so far in a variable, let's say maxi
. Starting value of maxi
will be the first marksheet's marks as initially that will the maximum.
Then go checking each marksheet one by one and keep comparing and updating maxi as and when a marks greater than current marks stored in maxi is found. This will ensure that once we have all the marksheets checked we will have maximum marks stored in the variable maxi.
max
.#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int largestElement(vector<int>& nums) {
// Initialize max as the first element
int max = nums[0];
// Traverse the entire array
for (int i = 1; i < nums.size(); i++) {
/* If current element is greater
than max, update max*/
if (nums[i] > max) {
max = nums[i];
}
}
// Return the largest element found
return max;
}
};
int main() {
vector nums = {3, 2, 1, 5, 2};
// Create an instance of the Solution class
Solution sol;
int largest = sol.largestElement(nums);
// Print the largest element in the array.
cout << largest << endl;
return 0;
}
import java.util.*;
class Solution {
public int largestElement(int[] nums) {
// Initialize max as the first element
int max = nums[0];
// Traverse the entire array
for (int i = 1; i < nums.length; i++) {
/* If current element is greater
than max, update max*/
if (nums[i] > max) {
max = nums[i];
}
}
// Return the largest element found
return max;
}
public static void main(String[] args) {
int[] nums = {3,2,1,5,2};
// Create an instance of the Solution class
Solution sol = new Solution();
// Find and print the largest element in the array
System.out.println("The largest element in the array is: " + sol.largestElement(nums));
}
}
from typing import List
class Solution:
def largestElement(self, nums):
# Initialize max_val as the first element
max_val = nums[0]
# Traverse the entire list
for num in nums[1:]:
"""
If the current element is greater
than max_val, update max_val
"""
if num > max_val:
max_val = num
# Return the largest element found
return max_val
# Main function
if __name__ == "__main__":
nums = [3, 2, 1, 5, 2]
# Create an instance of the Solution class
sol = Solution()
# Find and print the largest element in the list
print("The largest element in the list is:", sol.largestNumber(nums))
class Solution {
largestElement(nums) {
// Initialize max as the first element
let max = nums[0];
// Traverse the entire array
for (let i = 1; i < nums.length; i++) {
/*If current element is greater
than max, update max*/
if (nums[i] > max) {
max = nums[i];
}
}
// Return the largest element found
return max;
}
}
const nums = [3, 2, 1, 5, 2];
// Create an instance of the Solution class
const sol = new Solution();
// Find and print the largest element in the array
console.log("The largest element in the array is: " + sol.largestElement(nums));
Q: How does the algorithm behave if there are multiple occurrences of the largest element?
A: The algorithm still works correctly. It will identify the first occurrence of the largest element, but since the value remains the same, it doesn't matter which occurrence is tracked. Example: Input: nums = [3, 5, 2, 5, 1] Output: 5
Q: How does the algorithm handle arrays with all negative numbers?
A: The algorithm works the same way with negative numbers. It starts with the first element as the largest and updates it whenever a larger (less negative) number is found. Example: Input: nums = [-3, -1, -7, -2] Output: -1
Q: How would you handle an empty array or invalid input?
A: Check if the array is empty at the beginning. If it is, return a specific value (e.g., None) or raise an error. Example: Input: nums = [] Output: None or raise a "ValueError: Array is empty"
Q: How would you modify the algorithm to return both the largest element and its index?
A: Use a loop to track both the value and the index of the largest element. Update the index whenever the largest value is updated. Example: Input: nums = [1, 3, 5, 2] Output: (5, 2) (Value = 5, Index = 2)