Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Ninja’s Test

Moderate
0/80
Average time to solve is 20m
profile
Contributed by
7 upvotes
Asked in companies
FlipkartMathworks

Problem statement

FAANG is the best coding school in Ninjaland. The Ninja wants to get admission into FAANG. To get admission into FAANG, Ninja needs to clear its admission test, which is as follows:

Given an integer array ‘ARR’ of size ‘N’, find the maximum absolute difference between the nearest left and the right smaller element of every element in ‘ARR’. If there is no left smaller or right smaller element of any element then take 0 as the smaller element.

Can you help Ninja to clear the admission test?

For example:
You are given ‘ARR’ = [1, 2, 3, 1]. The difference between the nearest left and the right smaller element of ‘ARR[2],’ i.e., 2 - 1 = 1 is the maximum possible. Hence the answer is 1.
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= T <= 10 
1 <= N <= 5000
1 <= ARR[i] <= 10^6

Time limit: 1 sec
Note:
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
Sample Input 1:
2
4
1 2 3 1
3
3 4 1 
Sample Output 1:
 1
 2
Explanation:
For the first test case, the difference between the nearest left and the right smaller element of ‘ARR[2],’ i.e., 2 - 1 = 1 is maximum possible. Hence the answer is 1.

For the second test case, the difference between the nearest left and the right smaller element of ‘ARR[1],’ i.e., 3 - 1 = 2, is maximum possible. Hence the answer is 2.
Sample Input 2:
2
4
1 2 3 4
3
1 1 1 
Sample Output 2:
3
0
Full screen
Console