This code is not executing for the given program . Can you help me find the error ? https://practice.geeksforgeeks.org/problems/max-sum-in-the-configuration/1/?track=amazon-arrays&batchId=192

class GfG
{
int max=Integer.MIN_VALUE;
int max_sum(int A[], int n)
{
int len=n;
int k=0;
while(len!=0){
rotate(A,n,1);
len–;
int s=sum(A,n);
max=Math.max(max,s);
}
return max;

}
int sum(int A[], int n)
{
    int s=0;
        for(int i=0;i<n;i++)
        {
            s+=i*A[i];
        }
        return s;
}

void rotate(int A[],int n,int d)
{
    int a[]=new int[n];
    if(d==0)
        return;
    int k=0;
    for(int i=d;i<n;i++)
    {
        a[k++]=A[i];
    }
    for(int i=0;i<d;i++)
    {
        a[k++]=A[i];
    }
    for(int i=0;i<n;i++)
    {
        A[i]=a[i];
    }
    
}

}

hey @sarveshbibhuty
code is fine
I have submitted this solution on GFG. I got full marks

thankyou . is there anyway to solve this in O(n) complexity ?

The idea is to compute the value of a rotation using values of previous rotation. When an array is rotated by one, following changes happen in sum of i*arr[i].

  1. Multiplier of arr[i-1] changes from 0 to n-1, i.e., arr[i-1] * (n-1) is added to current value.
  2. Multipliers of other terms is decremented by 1. i.e., (cum_sum – arr[i-1]) is subtracted from current value where cum_sum is sum of all numbers.