All Tracks Algorithms Sorting Quick Sort Problem

Max Power
Tag(s):

Algorithms, Easy, Quick Sort, Sorting

Problem
Editorial
Analytics

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\)

SAMPLE INPUT
2
2
9 10
4
2 3 4 1
SAMPLE OUTPUT
1
3
Explanation

In the first test case, we don't need to do any swaps, the max achievable power is 1.
In second test case we can swap \(A[3]\) and \(A[4]\) so the array will be 2 3 1 4 and the power will be 3.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

Lenskart

Challenge Name

Lenskart Java Developer Hiring Challenge

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications

?