Question: Complete this Question In Java Given an array of integers, a , return the maximum difference of any pair of numbers such that the larger

Complete this Question In Java
Given an array of integers, a, return the maximum difference of any pair of numbers such that
the larger integer in the pair occurs at a higher index (in the array) than the smaller integer.
Return -1 if you cannot find a pair that satisfies this condition.
Constraints: 1N1,000,000
-1,000,000a[i]1,000,000AAiin[0,N-1]
Input Format: The first line of the input is N(the number of elements in the array), and then
followed by N elements each in a separate line.
Sample Input 0:
7
2
3
10
2
4
8
1
SampleOutput 0:
Explanation 0:
For the array {2,3,10,2,4,8,1} given above, 10 is the largest number in the array and 1 is the
smallest number in the array. However, the index of 10 is lower than the lowest index that
contains a 1 so the condition of the problem is not satisfied. Using zero-based index notation, the
correct answer is a[2]-a[0]=10-2=8. This satisfies the condition that the larger number in the pair
should be positioned at a higher index in the array than the smaller number.
Sample Input 1:795632
SampleOutput 1:
2
Explanation 1: The value of maxDifference is 9-7=2.
9 occurs at a[1] and 7 occurs at a[0]. This satisfies the condition that the larger number must
have a higher index than the smaller number.
 Complete this Question In Java Given an array of integers, a,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!