Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5271 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4356 |

@sanjay05 | 3762 |

@mantu_singh | 3638 |

@gfgaccount | 3601 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 871 |

@akhileshkumar562002 | 862 |

@pradeeppatidar1999 | 840 |

@kanewilliamson123 | 837 |

@pd420786 | 813 |

@infilooop | 800 |

@bunnybug320 | 796 |

@smitabose7826 | 784 |

@virgat | 768 |

Complete Leaderboard |

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 10^{9}+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(10^{9}+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)*

**Expected Time Complexity:** O(nlog(n)).

**Expected Auxiliary Space:** O(1).

**Constraints:**

1 ≤ N ≤ 10^{7}

1 ≤ A_{i} ≤ N

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

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

...