Question: Question 2 : Problem Statement: Given an array of integers, a , return the maximum difference of any pair of numbers such that the larger
Question : Problem Statement:
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 if you cannot find a pair that satisfies this condition.
Constraints:
AAiin
Input Format: The first line of the input is the number of elements in the array and then followed by elements each in a separate line.
Sample Input :
SampleOutput :
Explanation :
For the array given above, is the largest number in the array and is the smallest number in the array. However, the index of is lower than the lowest index that contains a so the condition of the problem is not satisfied. Using zerobased index notation, the correct answer is 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 :
SampleOutput :
Explanation : The value of maxDifference is
occurs at a and occurs at a This satisfies the condition that the larger number must have a higher index than the smaller number.
Please write code in Java.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
