Max sum in the configuration | Practice | GeeksforGeeks

Given an array(0-based indexing), you have to find the max sum of i*A[i] where A[i] is the element at index i in the array. The only operation allowed is to rotate(clock-wise or counter clock-wise) the array any number of times.
Example 1:
Input:
N = 4
A[] = {8,3,1,2}
Output: 29
Explanation: Above the configuration
possible by rotating elements are
3 1 2 8 here sum is 3*0+1*1+2*2+8*3 = 29
1 2 8 3 here sum is 1*0+2*1+8*2+3*3 = 27
2 8 3 1 here sum is 2*0+8*1+3*2+1*3 = 17
8 3 1 2 here sum is 8*0+3*1+1*2+2*3 = 11
Here the max sum is 29 

Your Task:
Your task is to complete the function max_sum which takes two arguments which is the array A [ ] and its size and returns an integer value denoting the required max sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1<=N<=104
1<=A[]<1000
0
tapanmanu20003 weeks ago
int max_sum(int arr[],int n){
    int sum = 0;
    int sum2 = 0;
    int maxsum = INT_MIN;
    for(int j = 0; j < n; j ++){
        sum += arr[j] * j;
        sum2 += arr[j];
    }
    for(int i = 0; i < n; i++){
        sum2 -= arr[i];
        sum = sum - sum2;
        sum = sum + (n-1)*arr[i];
        maxsum = max(sum,maxsum);
        sum2 += arr[i];
    }
    return maxsum;
}
0
geminicode3 weeks ago
Here is my approach in Python.
def max_sum(a,n):
    productsum = 0
    for i in range(n):
        productsum += a[i]*i
    sumi = sum(a)
    ans = productsum
    for  i in range(n):
        productsum = productsum + sumi - n*(a[n-i-1])
        if productsum > ans:
            ans = productsum
    return ans
+1
namastecoding3 weeks ago
int max_sum(int A[],int N){     int sum=0;     int s0=0;          for(int i=0;i<N;i++){          sum += A[i];          s0 += A[i]*i;     }     int max=s0;     int si=s0;          for(int i=0;i<N-1;i++){          int sip1=si + sum - N * A[N-i-1]; // Main Formula For Rotating and Maxify          // sip1 = S_i+1(subscript)           if(sip1>max)              max=sip1;                   si=sip1;     }     return max;}
-1
anujmagotra203 weeks ago
int max_sum(int arr[], int n)   {int max = 0;       int sum = 0;       for (int i = 0; i < arr.length; i++) {
           sum = sum + (arr[i] * i);       }       if (max < sum) {           max = sum;       }       for (int i = 0 ; i< arr.length-1;i++){            sum = 0;           rotate(arr);           for (int j = 0; j< arr.length; j++) {               sum = sum + (arr[j] * j);           }           if (max < sum) {               max = sum;           }       }
       return max;   }   static void rotate(int [] arr){       int last = arr[arr.length - 1];       for (int i = arr.length - 1; i > 0; i--) {           arr[i] = arr[i - 1];
       }       arr[0] = last;
   }
+1
sritesh8564 weeks ago
SIMPLE SOLUTION
int max_sum(int array[], int n){              int maxProd = 0; int maxSum = 0;  for(int i=1; i<array.length; i++)maxSum += array[i]; for(int i=0; i<array.length; i++)maxProd += array[i]*i;  int result = Integer.MIN_VALUE; result = Math.max(maxProd, result);  for(int i=1; i<array.length; i++) {  maxProd = maxProd - maxSum + (array[i-1]*(array.length-1));  maxSum = maxSum - array[i]+array[i-1];  result = Math.max(maxProd, result);   }  return result;      }
+1
taiphanvan24031 month ago
int max_sum(int a[],int n)
{
    int sumArr=0,kq=0;
    for(int i=0;i<n;i++){
        sumArr+=a[i];
        kq+=i*a[i];
    }
    int Max=kq;
    for(int i=1;i<n;i++){
        kq=kq-sumArr+n*a[i-1];
        if(kq>Max)
            Max=kq;
    }
    return Max;
}
0
sachin1810201 month ago
Help me optimize this Java code 
 
class GfG{   int max_sum(int A[], int n)   {// Your code here// Finding Solutionint[] ans = new int[n];for(int  i = 0; i< n; i++){    rotateby1(A, n);    ans[i] = sum(A,n);}Arrays.sort(ans);return ans[n-1];      }          int sum(int[] A,int n ){       int sum = 0;    for(int i =0; i< n; i++)    {            sum = sum + A[i]*i;    }     return sum;   }         int[] rotateby1(int[] A, int n )   {       int ans = A[0];       for( int i =0; i< n-1 ; i++)       {           A[i] = A[i+1];       }       A[n-1] = ans;       return A;   }} 
0
im_kira1 month ago
//Javascript o(n)
class Solution {    computeRotatedSum(e,circularSum,sum,a){        let maxCircularSum=circularSum;        while(e>0){        circularSum=circularSum+(sum-(a[e]*a.length));        maxCircularSum= Math.max(maxCircularSum,circularSum)        e--;     }     return maxCircularSum;   }   max_sum(a, n)   {     let circularSum=0;     let e=n-1;     let sum=a.reduce((el,acc)=>el+acc,0);     let i=0;     for(let i=0;i<n;i++){         circularSum+=a[i]*i;     }     return this.computeRotatedSum(e,circularSum,sum,a)
   }}
0
yuktaarya240420002 months ago
class GfG{   int max_sum(int A[], int n)   {// Your code hereint sum=0;int rot=1;for(int i=0;i<n;i++){    sum+=A[i]*i;}int max=sum;while(rot<=n-1){    int sum1=0;    int temp=A[0];    for(int i=1;i<=n-1;i++){        A[i-1]=A[i];        sum1+=A[i-1]*(i-1);    }    A[n-1]=temp;    sum1+=A[n-1]*(n-1);    if(sum1>max){        max=sum1;        rot++;    }else{        rot++;    }   }return max;   } }//shows TLE can u please suggest how to optimize this code?
+1
jayesh292 months ago
Easy Java Solution :-
    int max_sum(int arr[], int n){
        // regSum = Regular iterative summ of rray
	   //  pSum = Sum acc. to the pattern
	    int regSum = arr[0] ,  pSum = 0;
	    for(int i=1;i<n;i++){
	        regSum+=arr[i];
	        pSum += (i*arr[i]);
	    }
	    int maxSum = pSum;
	    for(int i=1;i<n;i++){
	        int x = pSum-regSum+arr[i-1]+((n-1)*arr[i-1]);
	        maxSum = Math.max(maxSum,x);
	        pSum = x;
	    }
	    return maxSum;
    }	
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.