How to solve Largest Number At Least Twice of Others Algorithm in Java
This is my solution to problem 747 on Leet Code.
In a given integer array nums
, there is always exactly one largest element.
Find whether the largest element in the array is at least twice as much as every other number in the array.
If it is, return the index of the largest element, otherwise return -1.
Example 1:
Input: nums = [3, 6, 1, 0]
Output: 1
Explanation: 6 is the largest integer, and for every other number in the array x,
6 is more than twice as big as x. The index of value 6 is 1, so we return 1.
Example 2:
Input: nums = [1, 2, 3, 4]
Output: -1
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.
Note:
nums
will have a length in the range[1, 50]
.- Every
nums[i]
will be an integer in the range[0, 99]
.
First, we check to see if the array is empty. If so, return -1. This is the first edge case.
Next, we check to see if there is only one element in the array. If so, we return 0 because the number at index zero would technically be twice as large as any other element in the array.
Now, we need a for loop to traverse through the array while comparing the numbers to each other. We want to find the largest number so we need to make a variable called largestNumber. The default position of largestNumber will be at index 0 so we can compare it to the other integers in the array.
We want to compare largestNumber to the other numbers. We also want to make sure that it is at least twice the value of every other number. This means it has to be at least twice the value as the second largest integer in the array. We can store this number in a variable called secondLargestNumber. Let’s make its initial value the smallest number an integer can be. This will be a placeholder until we give it a real value in the for loop. Since the integer array cannot contain a value smaller than Integer.MIN_VALUE, the code works across all edge cases. The default position of secondLargestNumber is index 1 so we can compare it to largestNumber at index 0.
The objective is to return the index with the largest number. We can track that index in a variable called dominantIndex. The default position of dominantIndex will be index 0.
Now we can start our for loop. Let’s call the variable of the loop currentIndex. The default position of currentIndex will be 1 so we can compare its value to largestNumber.
At the start of each loop, we want to save the value of currentIndex so we can compare it to largestNumber. We will store this value in a variable called currentNumber.
If the currentNumber is greater than the largestNumber:
- The largestNumber becomes the secondLargestNumber
- The currentNumber becomes the largestNumber
- The currentIndex becomes the dominantIndex
If the currentNumber is less than the largestNumber but is greater than the secondLargestNumber, the currentNumber becomes the secondLargestNumber.
When the for loop is finished with the array, return the dominant index only if the largestNumber is twice as much as the secondLargestNumber.
Finally, just return -1 if the largest number in the array is not twice the size as the second largest number.
Let’s see it in action on the first array example above.
First, we ask if the array is empty. It is not empty so we move on. Then we ask if the array contains one integer. The array contains 4 integers so we move on again.
The largestNumber integer is set to the number at index 0, which is 3.
The secondLargestNumber is set to the smallest possible integer.
The dominantIndex is set to 0 by default. Remember, this is the index that holds the largestNumber.
Now we are at the for loop. We start the loop at index 1. The currentIndex is 1.
We save the currentNumber as the value at the currentIndex. The currentIndex is 1 which makes the currentNumber 6.
If the currentNumber is greater than the largestNumber:
- The largestNumber becomes the secondLargestNumber
2. The currentNumber becomes the largestNumber
3. The currentIndex becomes the dominantIndex
We start the for loop over again. This time, we are at index 2.
The currentNumber is set to the value at the currentIndex. The currentIndex is 2. The number at index 2 is 1.
Next we ask if the currentNumber is greater than the largestNumber. It is not. The currentNumber is 1. The largestNumber is 6. We move on to the next if statement.
If the currentNumber is greater than the secondLargestNumber, the currentNumber becomes the secondLargestNumber. It’s not. The currentNumber is 1. The secondLargestNumber is 3. We go to the beginning of the for loop.
Back to the beginning of the for loop where we are now at index 3.
The currentNumber is set to the number at the currentIndex. The number at the currentIndex is 0.
Next, we ask if the currenNumber is greater than the largestNumber. It’s not. The currentNumber is 0. The largestNumber is 6. We move on to the next if-statement.
Then we ask if the currentNumber is greater than the secondLargestNumber. It’s not. We finished the for loop. Now, we move on to the final if statement.
If the largestNumber is twice as much as the secondLargestNumber, return the dominantIndex. It is. The largestNumber is 6. The secondlargestNumber is 3. The dominantIndex is 1. The answer is 1.
Complexity Analysis
Time Complexity: O(N) where N is the number of integers
Space Complexity: O(1), the space used by the int variables
You can find the source code on my github profile.
I’m raising $7k for my college tuition. Please help me out by donating and/or sharing my gofundme.