Swap and Maximize | Practice | GeeksforGeeks

Given an array a[ ] of N elements. Consider array as a circular array i.e. element after an is a1. The task is to find maximum sum of the absolute difference between consecutive elements with rearrangement of array elements allowed i.e. after any rearrangement of array elements find |a1 – a2| + |a2 – a3| + ...... + |an-1 – an| + |an – a1|.
Example 1:
Input:
N = 4
a[] = {4, 2, 1, 8}
Output: 
18
Explanation: Rearrangement done is {1, 8, 
2, 4}. Sum of absolute difference between 
consecutive elements after rearrangement = 
|1 - 8| + |8 - 2| + |2 - 4| + |4 - 1| = 
7 + 6 + 2 + 3 = 18.
Example 2:
Input:
N = 2
a[] = {10, 12}
Output: 
4

Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function maxSum() that takes array a[ ] and its size N as input parameters and return the maximum sum using above method.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)
Constraints:
2 ≤ N ≤ 105
0
shaank18182 days ago
Java Solution

class GFG
{
    long maxSum(long arr[] ,int n)
    {
    
    //sort the array;
        Arrays.sort(arr);
        int i = 0 ;
        int j = n-1;
        
        //create a new array which has elements in fashon 1st smallest 1st largest 2nd smallest 2nd largest
        long brr[] = new long[n];
        
        long ans = 0;
        
     for(int k = 0 ; k<n;k++){
        
        if(k%2==0){
            brr[k] = arr[i++];
        }else{
            brr[k] = arr[j--];
        }
     }
        
    //take the absolute diff    
     for(int k = 0 ; k<n-1;k++){
         ans += Math.abs(brr[k]-brr[k+1]);
     }
        
        //take the absolute diff of 1st and last as array is circuler
        ans += Math.abs(brr[0]-brr[n-1]);
      
    
        return ans;
        
    }
}
0
arvind16yadav4 weeks ago
/*JAVA CODE : Time : 1.2*/
class GFG{   long maxSum(long arr[] ,int n)   {       long ans = 0;       Arrays.sort(arr);              int mid = n/2;              ans += Math.abs(arr[0]-arr[n-1]);       ans += Math.abs(arr[0]-arr[mid]);              if(n%2 == 0){           for(int i=1; i<mid; i++){               ans += Math.abs(arr[i]-arr[n-i]);               ans += Math.abs(arr[i]-arr[n-1-i]);            }        }       else{           for(int i=1; i<=mid; i++){               ans += Math.abs(arr[i]-arr[n-i]);               ans += Math.abs(arr[i]-arr[n-1-i]);            }        }                     return ans;   }}
0
mitradiptamoy1 month ago
long long int maxSum(int arr[], int n){   sort(arr,arr+n);   int sum=0;   int j=n-1;   int i=0;   while(i<n){       sum+=abs(arr[i]-arr[j]);       j--;       i++;   }   return sum;} 
0
gupta2411sumit1 month ago
long long int maxSum(int arr[], int n){   long long int sum = 0 ;      int temp[n] ;      sort( arr , arr+n ) ;   int p = 0 ;   int e = 0 ;   for( int i = 0 ; i<n ; i++)   {       if(i%2==0)       {           temp[i] = arr[e] ;           e++ ;       }       else{                     temp[i] = arr[n-1-p] ;            p++ ;       }   }        for( int i = 0 ; i<n-1 ; i++)   {       sum += abs( temp[i]-temp[i+1])  ;     //  cout<<sum<<" " ;   }      sum += abs(temp[n-1] - temp[0]) ;      return sum ;}
+2
gaurabhkumarjha271020011 month ago
// easy c++ approach
     sort (arr, arr+n);
     long long  int sum=0;
     int j= n-1;
     for (int i=0; i< n; i++){
      sum+= abs (arr[i] - arr[j--]);
    }
       return sum;
0
hasnainraza1998hr2 months ago
C++, 0.4
long long int maxSum(int arr[], int n){   sort(arr,arr+n);   vector<int> brr;   long long sum=0;   for(int i=0;i<n/2;i++){       brr.push_back(arr[i]);       brr.push_back(arr[n-1-i]);   }   if(n%2!=0)       brr.push_back(arr[n/2]);          for(int i=0;i<n-1;i++){       sum += abs(brr[i]-brr[i+1]);   }   sum += abs(brr[n-1]-brr[0]);   return sum;}
0
ashutoshrastogi2 months ago
Simple Java Code
 
class GFG
{
    long maxSum(long arr[] ,int n)
    {
        int i,j;
        Arrays.sort(arr);
        
        long sum = Math.abs(arr[0]-arr[n-1]);
        
        if(n==2)
            return 2*sum;
        else{
            i = 1;
            j = n-2;
            while(i<j){
                long t = arr[i];
                arr[i] = arr[j];
                arr[j] = t;
                
                i+=2;
                j-=2;
            }
            
            for(i=0;i<n-1;i++)
                sum += Math.abs(arr[i]-arr[i+1]);
            
        }
        
        return sum;
        
    }
}
0
pandemania2 months ago
long long int maxSum(int arr[], int n){
    sort(arr,arr+n);
    int i=0,j=n-1;
    int cnt = 0;
    vector<int> narr;
    while(i <= j && cnt < n){
        if(cnt%2==0){
            narr.push_back(arr[i]);
            i++;
        }
        else{
            narr.push_back(arr[j]);
            j--;
        } 
        cnt++;
    }
    long long sum = 0;
    for(int i=1;i<narr.size();i++){
        sum += abs(narr[i-1] - narr[i]);
    }
    sum += abs(narr[n-1] - narr[0]);
    return sum;
}
0
jhav16432 months ago
long long int maxSum(int arr[], int n){   if(n==0)   {       return 0;   }      sort(arr, arr+n);      long long int i = 1, j = n-2, sum = 0;      while(i<j)   {       swap(arr[i], arr[j]);       i+=2;       j-=2;   }      for(i = 0; i<n-1; i++)   {       sum+=(abs(arr[i+1]- arr[i]));   }      sum+=abs(arr[n-1]-arr[0]);      return sum;}
0
parasmovaliya43212 months ago
long long int maxSum(int arr[], int n)
{
   long long s=0,i,j=n-1,t=n-1;
   
   sort(arr,arr+n);
  while(j>0)
  {
      if(t-j+1<j)
      {
          s+=(arr[j]-arr[t-j])+(arr[j]-arr[t-j+1]);
      }
      else if(t-j+1==j)
      {
         s+=(arr[j]-arr[t-j])+(arr[j]-arr[0]);
         return s;
      }
      else
      {
          s+=(arr[j]-arr[0]);
          return s;
      }
      j--;
  }
  return 0;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.