FastPrepFastPrep
Problem Brief

Find Maximum Difference

INTERNNEW GRADOA
See Cisco online assessment and hiring insights

Given a list of integers, find the maximum difference of two elements where the larger number appears after the smaller number.

Write an algorithm to find the maximum difference of two elements where the larger number appears after the smaller number.

Input

The first line of input consists of an integer - inputArray_size, representing the size of the list (N).

The next line consists of N space-separated integers representing the elements of the list.

Output

Print an integer representing the maximum difference of the two elements where the larger number appears after the smaller number. If no such elements are found, print 0.

1Example 1

Input
n = 7, inputArray = [2, 3, 10, 6, 4, 8, 1]
Output
8
Explanation
From the given list, the maximum difference will be 8 (difference between 2 and 10 :), we won't consider (10 and 1), as the larger number should appear after the smaller one. So, the output is 8 🍻

2Example 2

Input
n = 3, inputArray = [4, 3, 1]
Output
0
Explanation
Will add once find reliable source :) Just like what we did for the example 1 above πŸ‘† See yall next time!
public int findMaximumDifference(int n, List<Integer> inputArray) {
  // write your code here
}
Input

n

7

inputArray

[2, 3, 10, 6, 4, 8, 1]

Output

8

Sign in to submit your solution.