Overview
An array is given. The objective is to find the maximum difference between values at two indexes i and j such that
- j > i
- arr[j] > arr[i]
If no such indexes exist then return -1
Example 1
Input: intervals = [8, 2, 6, 5]
Output: 4
Explanation: 6-2 = 4
Example 2
Input: intervals = [8, 3, 2, 1]
Output: -1
Explanation: Condition is not satified
Program
Here is the program for the same.
package main
import "fmt"
func maximumDifference(nums []int) int {
lenNums := len(nums)
if lenNums == 0 || lenNums == 1 {
return -1
}
minElement := nums[0]
maxDifference := -1
for i := 1; i < lenNums; i++ {
diff := nums[i] - minElement
if diff > maxDifference && diff != 0 {
maxDifference = diff
}
if nums[i] < minElement {
minElement = nums[i]
}
}
return maxDifference
}
func main() {
output := maximumDifference([]int{8, 2, 6, 5})
fmt.Println(output)
output = maximumDifference([]int{8, 3, 2, 1})
fmt.Println(output)
}
Output
4
-1