#include<bits/stdc++.h>
using namespace std;
int main(){
    int T,N;
    cin>>T;
    while(T--){
        cin>>N;
        int arr[N];
        for(int i=0;i<N;i++){
            cin>>arr[i];
        }
    sort(arr, arr+N);
    int x=arr[0];
    int y=arr[N/2];
    int z=arr[N-1];
    int a=((abs(x-y)+abs(y-z)+abs(z-x)));
    cout<<a<<endl;
    }
return 0;
}
            Asked
            
        
        
            Active
            
        
            Viewed 61 times
        
    -3
            
            
         
    
    
        大陸北方網友
        
- 3,696
- 3
- 12
- 37
 
    
    
        Bhuvnesh Bansal
        
- 11
- 2
- 5
- 
                    5What's wrong? The unholy trinity of competetive programming: [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) [`#include`](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [VLAs](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) ;) – Lukas-T Feb 08 '21 at 08:41
- 
                    2Variable-length arrays are not part of the C++ standard, and are very likely to cause stack overflows. Use `std::vector`. – molbdnilo Feb 08 '21 at 08:46
- 
                    2Use `std::vector` instead of an array, and see if that helps – Aykhan Hagverdili Feb 08 '21 at 08:47
- 
                    https://www.codechef.com/FEB21C/problems/MAXFUN question link – Bhuvnesh Bansal Feb 08 '21 at 08:48
- 
                    1Replace `int arr[N];` with `std::vectorarr(n);` and give a try. VLA could be causing issue here – Wander3r Feb 08 '21 at 08:50
- 
                    The result can be as large as 4. 10^9 (with negative input values). Too large for an `int`. Besides, I don't think you need to sort the array. You only need the min and max. This implies that you don't need any array. – Damien Feb 08 '21 at 08:55
- 
                    You can use dynamic allocation and see if that helps https://gcc.godbolt.org/z/9zM8Yh – Aykhan Hagverdili Feb 08 '21 at 09:04
1 Answers
0
            
            
        As mentioned in the comments, you should avoid:
- #include<bits/stdc++.h>
- using namespace std;
- VLA
However, as this kind of site unfortunately promotes this way of writing C++ code, and as N = 10^5 is not so large, it is unlikely that these are the reasons of the run time error.
In practice, the result is equal to 2*(max - min). As the absolute values are less or equal to 10^9, this implies that the result can be as large as 4 10^9 (assuming negative and positive inputs). This is too large for an int.
Moreover, sorting is useless here. You can just calculate the min and max values when reading the A values.
Another consequence is that you don't need any array, and therefore any VLA!
 
    
    
        Damien
        
- 4,809
- 4
- 15
- 20
- 
                    Note: I submitted a code (just to check this answer) and I got a correct 100% validation, with a time of 0.02s, for a time limit of 1s. – Damien Feb 08 '21 at 10:24