
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rearrange an Array to Maximize i*arr[i] using C++
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
Approach to find The Solution
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.
Example
#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; }
Output
Maximum points: 63
Explanation of the above code
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.
Conclusion
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 Articles
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Rearrange an array such that arr[i] = i in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Rearrange an array such that ‘arr[j]’ becomes ‘i’ if ‘arr[i]’ is ‘j’ in C++
- Maximize arr[j] – arr[i] + arr[l] – arr[k], such that i < j < k < l in C++
- Maximize the sum of arr[i]*i in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Count pairs in an array that hold i*arr[i] > j*arr[j] in C++
- Rearrange an Array in Maximum Minimum Form using 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] for a given array in C++
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i in C++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
