Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximize the sum of arr[i]*i

  • Difficulty Level : Basic
  • Last Updated : 28 Apr, 2021

Given an array of N integers. You are allowed to rearrange the elements of the array. The task is to find the maximum value of Σarr[i]*i, where i = 0, 1, 2,…., n – 1.

Examples:  

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input : N = 4, arr[] = { 3, 5, 6, 1 }
Output : 31
If we arrange arr[] as { 1, 3, 5, 6 }. 
Sum of arr[i]*i is 1*0 + 3*1 + 5*2 + 6*3 
= 31, which is maximum

Input : N = 2, arr[] = { 19, 20 }
Output : 20

A simple solution is to generate all permutations of given array. For every permutation, compute the value of Σarr[i]*i and finally return the maximum value.
An efficient solution is based on the fact that the largest value should be scaled maximum and the smallest value should be scaled minimum. So we multiply the minimum value of i with the minimum value of arr[i]. So, sort the given array in increasing order and compute the sum of arr[i]*i, where i = 0 to n-1.
Below is the implementation of this approach:  

C++




// CPP program to find the maximum value
// of i*arr[i]
#include<bits/stdc++.h>
using namespace std;
 
int maxSum(int arr[], int n)
  // Sort the array
  sort(arr, arr + n);
 
  // Finding the sum of arr[i]*i
  int sum = 0;
  for (int i = 0; i < n; i++)
    sum += (arr[i]*i);
 
  return sum;
}
 
// Driven Program
int main()
{
  int arr[] = { 3, 5, 6, 1 };
  int n = sizeof(arr)/sizeof(arr[0]);
 
  cout << maxSum(arr, n) << endl;
  return 0;
}

Java




// Java program to find the
// maximum value of i*arr[i]
import java.util.*;
 
class GFG {
 
    static int maxSum(int arr[], int n)
    {   
    // Sort the array
    Arrays.sort(arr);
 
    // Finding the sum of arr[i]*i
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += (arr[i] * i);
 
    return sum;
    }
 
    // Driven Program
    public static void main(String[] args)
    {
    int arr[] = { 3, 5, 6, 1 };
    int n = arr.length;
 
    System.out.println(maxSum(arr, n));
 
    }
}
// This code is contributed by Prerna Saini

Python3




# Python program to find the
# maximum value of i*arr[i]
def maxSum(arr,n):
 
    #  Sort the array
    arr.sort()
 
    # Finding the sum of
    # arr[i]*i
    sum = 0
    for i in range(n):
        sum += arr[i] * i
         
    return sum
 
# Driver Program
arr = [3,5,6,1]
n = len(arr)
print(maxSum(arr,n))
 
# This code is contributed
# by Shrikant13

C#




// C# program to find the
// maximum value of i*arr[i]
using System;
 
class GFG {
     
    // Function to find the
    // maximum value of i*arr[i]
    static int maxSum(int[] arr, int n)
    {
         
        // Sort the array
        Array.Sort(arr);
     
        // Finding the sum of arr[i]*i
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += (arr[i] * i);
     
        return sum;
    }
 
    // Driver code
    static public void Main()
    {
        int[] arr = {3, 5, 6, 1};
        int n = arr.Length;
     
        Console.WriteLine(maxSum(arr, n));
 
    }
}
 
// This code is contributed by Ajit.

PHP




<?php
// PHP program to find the
// maximum value of i*arr[i]
 
// function returns the
// maximum value of i*arr[i]
function maxSum($arr, $n)
{
    // Sort the array
    sort($arr);
     
    // Finding the sum
    // of arr[i]*i
    $sum = 0;
    for ($i = 0; $i < $n; $i++)
        $sum += ($arr[$i] * $i);
     
    return $sum;
}
 
// Driver Code
$arr = array( 3, 5, 6, 1 );
$n = count($arr);
 
echo maxSum($arr, $n);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
// JavaScript program to find the
// maximum value of i*arr[i]
 
function maxSum(arr, n)
    {   
    // Sort the array
    arr.sort();
 
    // Finding the sum of arr[i]*i
    let sum = 0;
    for (let i = 0; i < n; i++)
        sum += (arr[i] * i);
 
    return sum;
    }
  
// Driver Code
 
    let arr = [ 3, 5, 6, 1 ];
    let n = arr.length;
 
    document.write(maxSum(arr, n));
         
</script>

Output:  

31

Time Complexity : O(n Log n)
This article is contributed by Anuj Chauhan (anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :