You are given an array nums
of positive integers. The array is either:
nums
is a positive integer
Since the array is very small (only 2 or 3 elements), the solution does not need to be optimized for performance. But thinking as if the array could be longer, the brute-force way is to scan the entire array and keep track of the maximum number and its index.
For this specific problem, we can simply compare the elements directly. If there are 2 elements, compare them and return the index of the larger one. If there are 3 elements, compare all three and return the index of the largest. This is a straightforward use of conditional statements or built-in functions.
While an optimized approach isn’t necessary, it’s good practice to write code that works for any small input size and handles all cases cleanly.
To solve this problem, follow these steps:
Alternatively, you can use built-in functions (like max()
in Python or Math.max()
in JavaScript) to find the largest value, and then use indexOf()
or a loop to get its index.
This approach works efficiently for small arrays and is easy to understand and implement.
Example 1:
Input: nums = [3, 9]
- Compare nums[0] = 3
and nums[1] = 9
.
- 9 > 3
, so the largest is at index 1.
Output: 1
Example 2:
Input: nums = [1, 5, 2]
- Start with index 0 (1
).
- Compare with index 1: 5 > 1
, so update index to 1.
- Compare with index 2: 2 < 5
, so keep index at 1.
Output: 1
Brute-force approach:
- Time Complexity: O(n)
, where n
is the length of the array (n
is at most 3 here).
- Space Complexity: O(1)
, as we only use a constant amount of extra space.
Optimized approach:
- In this problem, the optimized approach is the same as brute-force because the array is so small.
- If the array were much larger, the same O(n)
scan would be necessary to guarantee finding the largest value.
The problem is simple due to its small input size and the guarantee of a unique largest integer. The most reliable solution is to scan the array for the maximum value and return its index. This approach is efficient and easy to implement, making use of basic iteration and conditional checks. The guarantee of a unique answer and small array size means we can focus on clarity and correctness rather than optimization.
class Solution:
def largeIntegerIndex(self, nums):
max_value = nums[0]
max_index = 0
for i in range(1, len(nums)):
if nums[i] > max_value:
max_value = nums[i]
max_index = i
return max_index
class Solution {
public:
int largeIntegerIndex(vector<int>& nums) {
int maxValue = nums[0];
int maxIndex = 0;
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] > maxValue) {
maxValue = nums[i];
maxIndex = i;
}
}
return maxIndex;
}
};
class Solution {
public int largeIntegerIndex(int[] nums) {
int maxValue = nums[0];
int maxIndex = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > maxValue) {
maxValue = nums[i];
maxIndex = i;
}
}
return maxIndex;
}
}
function largeIntegerIndex(nums) {
let maxValue = nums[0];
let maxIndex = 0;
for (let i = 1; i < nums.length; i++) {
if (nums[i] > maxValue) {
maxValue = nums[i];
maxIndex = i;
}
}
return maxIndex;
}