Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1533. Find the Index of the Large Integer - Leetcode Solution

Problem Description

You are given an array nums of positive integers. The array is either:

  • Of length 2, where one number is much larger than the other
  • Or of length 3, where one number is much larger than the others
Your task is to find the index of the largest integer in the array. The largest integer is guaranteed to be strictly greater than the other(s), so there is always exactly one valid answer. Return the index (0-based) of this largest integer.
Constraints:
  • 2 ≤ nums.length ≤ 3
  • Each element in nums is a positive integer
  • There is exactly one largest integer

Thought Process

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.

Solution Approach

To solve this problem, follow these steps:

  1. Initialize a variable to keep track of the index of the largest number. You can start with 0 (the first element).
  2. Iterate through the array (from index 1 onward):
    • For each element, check if it is greater than the current largest.
    • If it is, update your variable to the current index.
  3. After checking all elements, the variable will hold the index of the largest integer.

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 Walkthrough

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

Time and Space Complexity

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.

Summary

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.

Code Implementation

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;
}