Max Power
Practice
3.6 (10 votes)
Algorithms
Easy
Quick sort
Sorting
Problem
19% Success 4286 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given an array A having N distinct integers.

The power of the array is defined as:

  • \(max(A[i]-A[j]) \space where \space 2 \le i \le N\)
  • for each i, j is the largest index less than i such that \(A[j] < A[i]\).

Let's say the array is {1,2,5}, then the power of the array is \(max((2-1), (5-2))\) , which simplifies to \(max(1,3)\) which is equal to 3.

Operation Allowed:
If you are allowed to choose any two indices x and y and swap \(A[x]\) and \(A[y]\), find out the maximum power that can be achieved.

Note: You are allowed to perform the above operation at most once.

Input:
First line consists of a single integer, T, denoting the number of test cases.
First line of each test case consists of a single integer, denoting N.
Second line of each test case consists of N space separated integers denoting the array A.

Output:
For each test case, print the maximum achievable power on a new line.

Constraints:
\(1 \le T \le 10\)
\(2 \le N \le 10^5\)
\(1 \le A[i] \le 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
15 votes
Tags:
EasyQuick SortSorting
Points:20
11 votes
Tags:
AlgorithmsArraysBinary treeEasyHash MapsImplementationOne-dimensionalQuick SortSorting
Points:20
272 votes
Tags:
Binary SearchEasyOpenSorting