Open In App

The Optimal Selection in an Array

Last Updated : 20 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There is an array given, the task is to select all of these integers in any order. For selecting every integer you get some points, and you need to maximize those points. For every integer you select, you get points equal to the value of the selected integer * the number of integers selected before the current integer, the task is to find the maximum points you can get provided you can select every integer exactly 1 time.

Examples:

Input: arr[ ] = {1, 2, 2, 4, 9}
Output: 54
Explanation: 

  • First he selects 1
    • Points : 1 * 0 (no integer selected before this), Total Points = 0
  • Then he selects 2
    • Points : 2 * 1 (1 selected before this)
    • Total Points = 2
  • Then he selects 2
    • Points : 2 * 2 (1, 2 selected before this)
    • Total Points = 6
  • Then he selects 4
    • Points : 4 * 3 (1, 2, 2 selected before this)
    • Total Points = 18
  • Then he selects 9
    • Points : 9 * 4 (1, 2, 2, 4 selected before this)
    • Total Points = 54

Input: arr[ ] = {2, 2, 2, 2} 
Output: 12

Approach: To solve the problem follow the below idea:

The easiest and simplest approach is to sort the array and count the elements before the current element which is simply the index of the current element. So multiply current element with its index and compute the sum of elements in each iteration and return the sum.

Follow the steps to solve the problem:

  • Sort the Array.
  • Traverse Array from the first index till the end.
  • Compute the sum of the array by multiplying each element with its index.
  • Return the sum.

Below is the implementation for the above approach:

C++




// C++ code for the above approach:
#include <algorithm>
#include <iostream>
using namespace std;
   
long selection(int arr[], int n)
{
   
    // Complete the function
    sort(arr, arr + n);
    long s = 0;
    for (int i = 1; i < n; i++) {
        s += (arr[i] * i);
    }
    return s;
}
   
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 4, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
   
    // Function call
    cout << "The optimal selection of array is: "
         << selection(arr, n);
    return 0;
}


C




#include <stdio.h>
#include <stdlib.h>
 
long selection(int arr[], int n) {
    qsort(arr, n, sizeof(int), cmpfunc);
    long s = 0;
    for (int i = 1; i < n; i++) {
        s += (arr[i] * i);
    }
    return s;
}
 
int cmpfunc(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}
 
//Driver code
int main() {
    int arr[] = {1, 2, 2, 4, 9};
    int n = sizeof(arr) / sizeof(arr[0]);
   
  //Function call
    printf("The optimal selection of array is : %ld", selection(arr, n));
    return 0;
}


Java




import java.io.*;
import java.util.*;
 
class GFG {
   
  //Driver code
    public static void main (String[] args) {
      int arr[] = {1, 2, 2, 4, 9};
      int n=arr.length;
       
        //Function call
        System.out.println("The optimal selection of array is : "+selection(arr,n));
    }
   static long selection (int arr[], int n) {
        //Complete the function
        Arrays.sort(arr);
        long s=0;
        for(int i=1;i<n;i++)
        {
            s+=(arr[i]*i);
        }
        return s;
    }
}


Python3




import numpy as np
 
def selection(arr, n):
    arr = np.sort(arr)
    s = 0
    for i in range(1, n):
        s += (arr[i]*i)
    return s
 
arr = np.array([1, 2, 2, 4, 9])
n = len(arr)
 
# Function call
print("The optimal selection of array is : ", selection(arr, n))


C#




// C# code for the above approach
using System;
 
public class GFG {
    static long Selection(int[] arr, int n)
    {
        // Complete the function
        Array.Sort(arr);
        long s = 0;
        for (int i = 1; i < n; i++) {
            s += (arr[i] * i);
        }
        return s;
    }
 
    public static void Main(string[] args)
    {
        int[] arr = { 1, 2, 2, 4, 9 };
        int n = arr.Length;
 
        // Function call
        Console.WriteLine(
            "The optimal selection of array is: "
            + Selection(arr, n));
    }
}
 
// This code is contributed by Susobhan Akhuli


Javascript




import * as np from 'numpy';
var arr, n;
 
function selection(arr, n) {
  var s;
  arr = np.sort(arr);
  s = 0;
 
  for (var i = 1, _pj_a = n; i < _pj_a; i += 1) {
    s += arr[i] * i;
  }
 
  return s;
}
 
arr = np.array([1, 2, 2, 4, 9]);
n = arr.length;
  
console.log("The optimal selection of array is : ", selection(arr, n));


Output

The optimal selection of array is: 54

Time Complexity: O(n*log(n)), where n is the size of the input array.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads