

If ‘N’ = 5, ‘ARR’ = {2, 4, 10, 8, 6}
You can rearrange ‘ARR’ to now become {2, 4, 6, 8, 10} this is an arithmetic progression. Therefore we will print “true”.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’, denoting the number of elements in the array.
The second line of each test case contains N integers ‘ARR’, denoting the array elements.
For each test case, print “true” if the given array can be rearranged to form an A.P., else print “false”.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
3 ≤ N ≤ 5000
-10^9 ≤ ARR[i] ≤ 10^9
Time limit: 1 sec
An arithmetic sequence is either a non-increasing or a non-decreasing sequence, depending on the value of difference b/w two consecutive elements of AP. This means that simply sorting and then checking for AP sequence is sufficient. To check for the AP sequence in a sorted array, simply check the difference between adjacent elements is equal.
The steps are as follows :
We can solve this faster if we remove the need of sorting the input array, one way to do so is to find the largest and smallest element in the array, the difference them will be equal to (N - 1) * d, this way we can calculate the value of ‘d’ (difference between adjacent terms of AP). Now simply form an AP sequence in form of a Hash-Set or a Hash-Map as it allows insertions and deletions in constant time in an average case, this means we can get rid of the log(..) term that came as a result of using sorting. After forming a Hash-Set we can iterate the array elements and remove them one by one from the Hash-Set. Separately handle the case for an AP sequence with all terms equal to each other (d equal to 0).
The steps are as follows :