Latest

6/recent/ticker-posts

Find maximum sum i*arr[i] if only rotation of the array is allowed.


#include < bits/stdc++.h >
using namespace std;

int main()
{
        int n;
        cin >> n;
        int a[n];
        for(int i=0;i < n;i++)
          cin >> a[i];

        int value=0,sum=0;
        for(int i=0;i < n;i++)
        {
                  sum=sum+a[i];
                  value=value+i*a[i];
        }
        int ans=value;
        for(int i=1;i < n;i++)
        {
                  value=value+sum-n*a[n-i];
                  ans=max(ans,value);
        }
        cout << ans;
        return 0;

}








Post a Comment

0 Comments