Maximize sum(arr[i]*i) of an Array | Practice | GeeksforGeeks

Given an array A of N integers. Your task is to write a program to find the maximum value of ∑arr[i]*i, where i = 0, 1, 2,...., n – 1.
You are allowed to rearrange the elements of the array.
Note: Since output could be large, hence module 109+7 and then print answer.
 
Example 1:
Input : Arr[] = {5, 3, 2, 4, 1}
Output : 40
Explanation:
If we arrange the array as 1 2 3 4 5 then 
we can see that the minimum index will multiply
with minimum number and maximum index will 
multiply with maximum number. 
So 1*0+2*1+3*2+4*3+5*4=0+2+6+12+20 = 40 mod(109+7) = 40


Example 2:
Input : Arr[] = {1, 2, 3}
Output : 8



Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function Maximize() that takes an array (arr), sizeOfArray (n), and return the maximum value of an array. The driver code takes care of the printing.
Expected Time Complexity: O(nlog(n)).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 107
1 ≤ Ai ≤ N
+1
harshscode5 days ago

         long long int sum=0;
         long long int prod=1;
        sort(a,a+n);
         long long int m=pow(10,9)+7;
        for(long long int i=0;i<n;i++)
        {
           prod=(a[i]*i);
           sum+=prod;
           
        }
       
        return sum%m;
0
tanashah1 week ago
+1
mestryruturaj2 weeks ago
Java
class Solution{
    
    long Maximize(int arr[], int n)
    {
        long res = 0;
        long mod = 1000000007;
        Arrays.sort(arr);
        
        for (int i = 0; i < n; i++) {
            res = (res + (arr[i] % mod  * i % mod)) % mod;
        }
        
        return res;
    }   
}
+1
sarthakgupta102 weeks ago
//Execution time=0.65
int Maximize(int a[],int n)
    {
        // Complete the function
        sort(a,a+n);
        long long int prod=1;
       long long int sum=0;
       long long int mod = pow(10,9)+7;

        for(long long int i=0;i<n;i++)
        {
            prod=(a[i]*i);
            sum+=prod;
            
            
        }
        //int ans=sum%1000000007;
        return sum%mod;
    }
0
zerefkhan2 weeks ago
C++ solution
Total Time Taken :       0.65/2.19
    int Maximize(int a[],int n)
    {
        sort(a,a+n); // First sort the array - increasing order
        
        int sum = 0;
        for(long long int i = 0; i < n; i++){
            sum = (sum+a[i]*i)%1000000007;
        }
        return sum%1000000007;
    }

0
951991g
This comment was deleted.
0
somawatpravin023 weeks ago
    def Maximize(self, a, n):        # Complete the function       sum=0       a.sort()       mod=(10**9)+7       for i in range(n):           sum+= a[i]*i       return sum%mod       
0
rahulstm081 month ago
//i got stuck at 600 test cases.why?
      sort(a,a+n);       int sum=0;       for(int i=0;i<n;i++)       {           sum+=a[i]*i;       }       return sum;
0
ajay7yadav951 month ago
//why its not pass after 600 test cases.. this will fine in VS
 
 Maximize(arr,n){      //code here      let sum = 0;      arr.sort((x,y)=>x-y);      for(let i=0; i<arr.length;i++){          sum += arr[i]*i;      }      return sum;   }
 
0
atif836141 month ago
JAVA SOLUTION :
class Solution{
    
    int Maximize(int arr[], int n)
    {
    long sum=0;
    int mod = (int)(Math.pow(10,3)+7);
    for(int i=0;i<n;i++){
    sum =sum+(long)arr[i]*i;
    }
    return (int)(sum%mod);
    }
    }
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.