Given a 0-indexed integer array nums
of size n
, find the maximum difference between nums[i]
and nums[j]
(i.e., nums[j] - nums[i]
), such that 0 <= i < j < n
and nums[i] < nums[j]
.
Return the maximum difference. If no such i
and j
exists, return -1
.
Example 1:
Input: nums = [7,1,5,4] Output: 4 Explanation: The maximum difference occurs with i = 1 and j = 2, nums[j] - nums[i] = 5 - 1 = 4. Note that with i = 1 and j = 0, the difference nums[j] - nums[i] = 7 - 1 = 6, but i > j, so it is not valid.
Example 2:
Input: nums = [9,4,3,2] Output: -1 Explanation: There is no i and j such that i < j and nums[i] < nums[j].
Example 3:
Input: nums = [1,5,2,10] Output: 9 Explanation: The maximum difference occurs with i = 0 and j = 3, nums[j] - nums[i] = 10 - 1 = 9.
Constraints:
n == nums.length
2 <= n <= 1000
1 <= nums[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for finding the maximum difference between increasing numbers is straightforward. It involves checking every possible pair of numbers to find the largest difference where the second number is bigger than the first.
Here's how the algorithm would work step-by-step:
def maximum_difference_brute_force(numbers):
maximum_difference = -1
# Iterate through each number as a potential smaller number
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
# Check if the number at the later index is greater.
if numbers[j] > numbers[i]:
#We found a valid increasing pair, calculate the difference
current_difference = numbers[j] - numbers[i]
#Update maximum difference if needed
if current_difference > maximum_difference:
maximum_difference = current_difference
return maximum_difference
The goal is to find the biggest difference between two numbers where the larger number comes after the smaller number. The best approach is to keep track of the smallest number we've seen so far and compare it to each number as we go along to potentially update the largest difference found.
Here's how the algorithm would work step-by-step:
def maximum_difference_between_increasing_elements(numbers):
maximum_difference = 0
smallest_number_seen_so_far = numbers[0]
# Initialize smallest number to first element
for current_number in numbers:
# Update the smallest number if a smaller number is encountered
if current_number < smallest_number_seen_so_far:
smallest_number_seen_so_far = current_number
# If the current number is greater, calculate the difference
if current_number > smallest_number_seen_so_far:
difference = current_number - smallest_number_seen_so_far
# Update the maximum difference if a larger difference is found
if difference > maximum_difference:
maximum_difference = difference
return maximum_difference
Case | How to Handle |
---|---|
Empty or null input array | Return -1 immediately as no valid difference can exist. |
Input array with only one element | Return -1 as there's no pair to calculate the difference. |
Array with all elements in descending order | The algorithm should return -1 since no increasing pair exists. |
Array with all identical values | The algorithm should return -1 as no increasing pair exists. |
Array with negative numbers | The solution should correctly handle negative numbers in the array. |
Integer overflow when calculating the difference | Use a data type with a larger range like long to prevent overflow if the difference might exceed the range of int. |
Array with extreme large positive and negative numbers | Ensure the comparison and difference calculation handle large numbers correctly without causing overflow or unexpected behavior. |
Large input array to test for performance | The algorithm should have a time complexity of O(n) for optimal performance. |