Problem 1: Largest element in an array
Given an array of N integers, find and return the largest element present in the array.
The array may contain positive numbers, negative numbers, or zero. Your task is to identify the maximum value among all elements.
Example 1
Input
5
12 45 7 89 23
Output
89
Explanation
Among the elements 12, 45, 7, 89, 23, the largest element is 89.
Example 2
Input
6
-5 -2 -10 -1 -8 -3
Output
-1
Explanation
Even though all elements are negative, -1 is the largest value in the array.Brute Force: Using Sorting
function largestElement(nums){
nums.sort((a, b) => a - b);
return nums[nums.length - 1];
}Better Approach: Linear Iteration
function largestElement(nums){
let largest = nums[0];
for(let i = 1; i<nums.length; i++){
if(nums[i] > largest){
largest = nums[i];
}
}
return largest;
}Problem 2: Second largest element in an array
Given an array of N integers, find and return the second largest distinct element present in the array.
If the array does not contain a second distinct largest element (for example, all elements are the same), return -1.
Example 1
Input
5
12 45 7 89 23
Output
45
Explanation
The largest element is 89, and the second largest distinct element is 45.
Example 2
Input
6
10 20 20 30 30 15
Output
20
Explanation
The distinct elements are 10, 15, 20, 30. The second largest among them is 20.
Example 3
Input
4
10 10 10 10
Output
-1
Explanation
There is no second largest distinct element because all elements are the same.Brute Force: Using Sorting
function secondLargestElement(nums){
nums.sort((a, b) => a - b);
let largest = nums[nums.length - 1];
for(let i = nums.length - 2; i>= 0; i--){
if(nums[i] !== largest){
return nums[i];
}
}
return -1;
}Better Approach: Using Two Variables
function secondLargestElement(nums){
let largest = -Infinity;
let secondLargest = -Infinity;
for(let i = 0; i<nums.length; i++){
if(nums[i] > largest){
secondLargest = largest;
largest = nums[i];
}else if(nums[i] > secondLargest && nums[i] !== largest){
secondLargest = nums[i];
}
}
return (secondLargest === -Infinity) ? -1 : secondLargest;
}Note: two fixes from the original — a leftover inner function secondLargestElement(nums){ line was removed (it left the function unclosed), and the final check now compares with -Infinity instead of Infinity, so an all-equal array returns -1.
Problem 3: Smallest element in an array
Given an array of N integers, find and return the smallest element present in the array.
The array may contain positive numbers, negative numbers, or zero. Your task is to identify the minimum value among all elements.
Example 1
Input
5
20 30 50 10 30
Output
10
Explanation
Among the elements 20, 30, 50, 10, 30, the smallest element is 10.
Example 2
Input
6
-5 -2 -10 -1 -8 -3
Output
-10
Explanation
Even though all elements are negative, -10 is the smallest value in the array.Brute Force: Using Sorting
function smallestElement(nums){
nums.sort((a, b) => a - b);
return nums[0];
}Better Approach: Linear Iteration
function smallestElement(nums){
let smallest = nums[0];
for(let i = 1; i<nums.length; i++){
if(nums[i] < smallest){
smallest = nums[i];
}
}
return smallest;
}Note: in the original, return nums[0]; sat outside the brute-force function and the linear version had no return smallest;. Both returns are now inside their functions.
Problem 4: Second smallest element in an array
Given an array of N integers, find and return the second smallest distinct element present in the array.
If the array does not contain a second distinct smallest element (for example, all elements are the same), return -1.
Example 1
Input
5
12 45 7 89 23
Output
12
Explanation
The smallest element is 7, and the second smallest distinct element is 12.
Example 2
Input
6
10 20 20 30 30 15
Output
15
Explanation
The distinct elements are 10, 15, 20, 30. The second smallest among them is 15.
Example 3
Input
4
10 10 10 10
Output
-1
Explanation
There is no second smallest distinct element because all elements are the same.Brute Force: Using Sorting
function secondSmallest(nums){
nums.sort((a, b) => a - b);
let smallest = nums[0];
for(let i = 1; i<nums.length; i++){
if(nums[i] !== smallest){
return nums[i];
}
}
return -1;
}Better Approach: Using Two Variables
function secondSmallest(nums){
let smallest = Infinity;
let secondSmallest = Infinity;
for(let i = 0; i<nums.length; i++){
if(nums[i] < smallest){
secondSmallest = smallest;
smallest = nums[i];
}else if(nums[i] < secondSmallest && nums[i] !== smallest){
secondSmallest = nums[i];
}
}
return secondSmallest === Infinity ? -1 : secondSmallest;
}Note: the original compared with > (nums[i] > smallest, nums[i] > secondSmallest). Nothing is greater than Infinity, so it always returned -1. Both comparisons are now <.
Problem 5: Check if an array is sorted
Given an array of N integers, determine whether the array is sorted in non-decreasing order (ascending order with duplicates allowed).
Return true if the array is sorted; otherwise, return false.
Example 1
Input
5
1 2 3 4 5
Output
true
Explanation
Every element is greater than or equal to the previous element.
Example 2
Input
5
1 2 5 4 6
Output
false
Explanation
The element 4 is smaller than 5, so the array is not sorted.Brute Force: Nested Loops
function checkArrayIsSorted(nums){
for(let i = 0; i<nums.length; i++){
for(let j = i + 1; j<nums.length; j++){
if(nums[j] < nums[i]){
return false;
}
}
}
return true;
}Better Approach: Compare Adjacent Elements
function checkArrayIsSorted(nums){
for(let i = 1; i<nums.length; i++){
if(nums[i-1] > nums[i]){
return false;
}
}
return true;
}