- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will discuss the problem of rearranging a given array of n numbers. Basically, we have to select elements from the array. For selecting each element, we get some points that will be evaluated by the value of the current element * a number of elements selected before the current element. You should select elements to get maximum points. For Example −

Input : arr[ ] = { 3, 1, 5, 6, 3 } If we select the elements in the way it is given, our points will be = 3 * 0 + 1 * 1 + 5 * 2 + 6 * 3 + 3 * 4 = 41 To maximize the points we have to select the elements in order { 1, 3, 3, 5, 6 } = 1 * 0 + 3 * 1 + 3 * 2 + 5 * 3 + 6 * 4 = 48(maximum) Output : 48 Input : arr[ ] = { 2, 4, 7, 1, 8 } Output : 63

Looking at the example, we got that to get the maximum points, and we need to select elements from smallest to largest. The approach to finding a solution is,

- Sort the given array in ascending order.
- Start picking elements from index 0 to end.
- Calculate the points you got from selecting each element.

#include <bits/stdc++.h> #include <iostream> using namespace std; int main () { int arr[] = { 2, 4, 7, 1, 8 }; int n = sizeof (arr) / sizeof (arr[0]); // sorting the array sort (arr, arr + n); int points = 0; // traverse the array and calculate the points for (int i = 0; i < n; i++) { points += arr[i] * i; } cout << "Maximum points: " << points; return 0; }

Maximum points: 63

This C++ code is easy to understand. First we are sorting the array and then traverse the array using a for loop and calculating the points gained by selecting each element from beginning to end.

In this article, we discuss the problem of selecting element in an array in order to get maximum points where points get calculated by i * arr[i]. We apply a greedy approach to solve this problem and get the maximum points. Also discuss C++ code to do the same, we can write this code in any other language like C, java, Python, etc. Hope you find this article helpful.

- Related Questions & Answers
- Rearrange an array such that arr[i] = i in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Maximize the sum of arr[i]*i in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i in C++
- Maximum value of arr[i] % arr[j] for a given array in C++
- Count of unique pairs (arr[i], arr[j]) such that i < j in C++

Advertisements