Open In App

Maximum Possible Rating of a Coding Contest

Given two arrays of positive integer Point[], Upvote[] of size N and a value K (1 <= K <= N). The task is to choose atleast K elements(Problems) such that the rating of the coding contest is maximum. 
Rating of contest: Rating of a contest is defined as total points of all problems in the contest multiplied by the minimum upvotes among all problems in the contest

Hence, Rating = sum of points of contest problems * minimum upvotes among contest problems.



Examples:

Input: Point[] = {2, 10, 3, 1, 5, 8}, Upvote[] = {5, 4, 3, 9, 7, 2}, K = 2 Output: 60 Explanation: Here we select 2nd and 5th problem to get the maximum rating of the contest. So maximum rating is (10 + 5) * min(4, 7) = 60 Input: Point[] = {2, 10, 3, 1, 5, 8}, Upvote[] = {5, 4, 3, 9, 7, 2}, K = 3 Output: 68 Explanation: Here we select 1st, 2nd and 5th problem to get the maximum rating of the contest. So maximum rating is (2 + 10 + 5) * min(5, 4, 7) = 68 Input: Point[] = {2, 20, 3, 1, 5, 8}, Upvote[] = {5, 10, 3, 9, 7, 2}, K = 4 Output: 200 Explanation: Here we select only 2nd problem to get maximum rating of the contest. A further selection of any problems decreases the rating. So maximum rating is 20 * 10 = 200



Approach :

Below is the implementation of the above approach. 




// C++ program to find the Maximum
// Possible Rating of a Coding Contest
#include <bits/stdc++.h>
using namespace std;
 
// Function to sort all problems
// descending to upvotes
bool Comparator(pair<int, int> p1,
                pair<int, int> p2)
{
    return p1.second > p2.second;
}
 
// Function to return maximum
// rating
int FindMaxRating(int N, int Point[],
                  int Upvote[], int K)
{
    // Declaring vector of pairs
    vector<pair<int, int> > vec;
 
    // Each pair represents a problem
    // with its points and upvotes
    for (int i = 0; i < N; i++)
    {
        vec.push_back(make_pair(Point[i],
                                Upvote[i]));
    }
 
    // Step (1) - Sort problems by their
    // upvotes value in decreasing order
    sort(vec.begin(), vec.end(), Comparator);
 
    // Declaring min_heap or priority queue
    // to track of the problem with
    // minimum points.
    priority_queue<int, vector<int>,
                  greater<int> > pq;
 
    int total_points = 0, max_rating = 0;
 
    // Step (2) - Loop for i = 0 to K - 1 and
    // do accordingly
    for (int i = 0; i < K; i++)
    {
        total_points = total_points
                      + vec[i].first;
        max_rating = max(max_rating,
                         total_points
                         * vec[i].second);
        pq.push(vec[i].first);
    }
 
    // Step (3) - Loop for i = K to N - 1
    // and do accordingly
    for (int i = K; i < N; i++)
    {
        if (pq.top() < vec[i].first)
        {
            total_points = total_points
                           - pq.top()
                           + vec[i].first;
            max_rating = max(max_rating,
                             total_points
                             * vec[i].second);
             
            pq.pop();
             
            pq.push(vec[i].first);
        }
    }
 
    return max_rating;
}
 
// Driver code
int main()
{
    int Point[] = { 2, 10, 3, 1, 5, 8 };
    int Upvote[] = { 5, 4, 3, 9, 7, 2 };
     
    int N = sizeof(Point) / sizeof(Point[0]);
    int K = 2;
 
    cout << "Maximum Rating of Coding Contest is: "
         << FindMaxRating(N, Point, Upvote, K);
 
    return 0;
}




/*package whatever //do not write package name here */
import java.util.*;
public class GFG {
 
  static class Pair<K,V>{
    K key;
    V value;
 
    Pair(){}
    Pair(K key,V value){
      this.key = key;
      this.value = value;
    }
  }
 
  // Function to return maximum
  // rating
  static int FindMaxRating(int N, int Point[],int Upvote[], int K)
  {
     
    // Declaring vector of pairs
    ArrayList<Pair<Integer,Integer>> vec = new ArrayList<>();
 
    // Each pair represents a problem
    // with its points and upvotes
    for (int i = 0; i < N; i++)
    {
      vec.add(new Pair(Point[i],Upvote[i]));
    }
 
    // Step (1) - Sort problems by their
    // upvotes value in decreasing order
    Collections.sort(vec,(p1,p2)->p2.value-p1.value);
 
    // Declaring min_heap or priority queue
    // to track of the problem with
    // minimum points.
    PriorityQueue<Integer> pq = new PriorityQueue<>();
 
    int total_points = 0, max_rating = 0;
 
    // Step (2) - Loop for i = 0 to K - 1 and
    // do accordingly
    for (int i = 0; i < K; i++)
    {
      total_points = total_points
        + vec.get(i).key;
      max_rating = Math.max(max_rating,total_points * vec.get(i).value);
      pq.add(vec.get(i).key);
    }
 
    // Step (3) - Loop for i = K to N - 1
    // and do accordingly
    for (int i = K; i < N; i++)
    {
      if (pq.peek() < vec.get(i).key)
      {
        total_points = total_points - pq.peek() + vec.get(i).key;
        max_rating = Math.max(max_rating, total_points * vec.get(i).value);
 
        pq.poll();
 
        pq.add(vec.get(i).key);
      }
    }
 
    return max_rating;
  }
 
  public static void main (String[] args) {
    int Point[] = { 2, 10, 3, 1, 5, 8 };
    int Upvote[] = { 5, 4, 3, 9, 7, 2 };
 
    int N = Point.length;
    int K = 2;
 
    System.out.println("Maximum Rating of Coding Contest is: " + FindMaxRating(N, Point, Upvote, K));
  }
}
 
// This code is contributed by aadityaburujwale.




# Python3 program to find the Maximum
# Possible Rating of a Coding Contest
import heapq
 
# Function to sort all problems
# descending to upvotes
def Comparator(p1):
     
    return p1[1]
 
# Function to return maximum
# rating
def FindMaxRating(N, Point, Upvote, K):
     
    # Declaring vector of pairs
    vec = []
     
    # Each pair represents a problem
    # with its points and upvotes
    for i in range(N):
        vec.append([Point[i], Upvote[i]])
     
    # Step (1) - Sort problems by their
    # upvotes value in decreasing order
    vec.sort(reverse = True, key = Comparator)
     
    # Declaring min_heap or priority queue
    # to track of the problem with
    # minimum points.
    pq = []
    heapq.heapify(pq)
     
    total_points, max_rating = 0, 0
     
    # Step (2) - Loop for i = 0 to K - 1 and
    # do accordingly
    for i in range(K):
        total_points = (total_points +
                        vec[i][0])
         
        max_rating = max(max_rating,
                         total_points *
                         vec[i][1])
         
        heapq.heappush(pq, vec[i][0])
     
    # Step (3) - Loop for i = K to N - 1
    # and do accordingly
    for i in range(K, N):
        if pq[0] < vec[i][0]:
            total_points = (total_points -
                                   pq[0] +
                               vec[i][0])
             
            max_rating = max(max_rating,
                             total_points *
                             vec[i][1])
             
            heapq.heappop(pq)
            heapq.heappush(pq, vec[i][0])
             
    return max_rating
 
# Driver code
Point = [ 2, 10, 3, 1, 5, 8 ]
Upvote = [ 5, 4, 3, 9, 7, 2 ]
 
N = len(Point)
K = 2
 
print("Maximum Rating of Coding Contest is:",
       FindMaxRating(N, Point, Upvote, K))
 
# This code is contributed by stutipathak31jan




using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to sort all problems
  // descending to upvotes
 
  public static int Comparator(KeyValuePair<int, int> p1,
                               KeyValuePair<int, int> p2)
  {
    if (p1.Value > p2.Value)
      return -1;
    return 1;
  }
 
  // Function to return maximum
  // rating
  public static int FindMaxRating(int N, int[] Point,
                                  int[] Upvote, int K)
  {
    // Declaring vector of pairs
    List<KeyValuePair<int, int> > vec
      = new List<KeyValuePair<int, int> >();
    // Each pair represents a problem
    // with its points and upvotes
    for (int i = 0; i < N; i++) {
      vec.Add(new KeyValuePair<int, int>(Point[i],
                                         Upvote[i]));
    }
 
    // Step (1) - Sort problems by their
    // upvotes value in decreasing order
    vec.Sort(Comparator);
 
    // Declaring min_heap or priority queue
    // to track of the problem with
    // minimum points.
    SortedSet<int> pq = new SortedSet<int>();
 
    int total_points = 0, max_rating = 0;
 
    // Step (2) - Loop for i = 0 to K - 1 and
    // do accordingly
    for (int i = 0; i < K; i++) {
      total_points = total_points + vec[i].Key;
      max_rating = Math.Max(
        max_rating, total_points * vec[i].Value);
      pq.Add(vec[i].Key);
    }
 
    // Step (3) - Loop for i = K to N - 1
    // and do accordingly
    for (int i = K; i < N; i++) {
      if (pq.Min < vec[i].Key) {
        total_points
          = total_points - pq.Min + vec[i].Key;
        max_rating
          = Math.Max(max_rating,
                     total_points * vec[i].Value);
 
        pq.Remove(pq.Min);
 
        pq.Add(vec[i].Key);
      }
    }
 
    return max_rating;
  }
 
  static public void Main()
  {
 
    int[] Point = { 2, 10, 3, 1, 5, 8 };
    int[] Upvote = { 5, 4, 3, 9, 7, 2 };
 
    int N = Point.Length;
    int K = 2;
 
    Console.WriteLine(
      "Maximum Rating of Coding Contest is: "
      + FindMaxRating(N, Point, Upvote, K));
  }
}
 
// This code is contributed by akashish__




// JS program to find the Maximum
// Possible Rating of a Coding Contest
 
// Function to sort all problems
// descending to upvotes
function Comparator(p1, p2) {
    return (p1.second > p2.second) ? -1 : 1;
}
 
// Function to return maximum
// rating
function FindMaxRating(N, Point, Upvote, K) {
    // Declaring vector of pairs
    let vec = [];
 
    // Each pair represents a problem
    // with its points and upvotes
    for (let i = 0; i < N; i++) {
        vec.push({
            "first": Point[i],
            "second": Upvote[i]
        });
    }
 
    // Step (1) - Sort problems by their
    // upvotes value in decreasing order
    vec.sort(Comparator);
 
    // Declaring min_heap or priority queue
    // to track of the problem with
    // minimum points.
    let pq = [];
 
    let total_points = 0, max_rating = 0;
 
    // Step (2) - Loop for i = 0 to K - 1 and
    // do accordingly
    for (let i = 0; i < K; i++) {
        total_points = total_points
            + vec[i].first;
        max_rating = Math.max(max_rating,
            total_points
            * vec[i].second);
        pq.push(vec[i].first);
        pq.sort();
    }
 
    // Step (3) - Loop for i = K to N - 1
    // and do accordingly
    for (let i = K; i < N; i++) {
        if (pq[0] < vec[i].first) {
            total_points = total_points
                - pq[0]
                + vec[i].first;
            max_rating = Math.max(max_rating,
                total_points
                * vec[i].second);
 
            pq.shift();
 
            pq.push(vec[i].first);
            pq.sort();
        }
    }
 
    return max_rating;
}
 
// Driver code
let Point = [2, 10, 3, 1, 5, 8];
let Upvote = [5, 4, 3, 9, 7, 2];
 
let N = Point.length;
let K = 2;
 
console.log("Maximum Rating of Coding Contest is:"
    , FindMaxRating(N, Point, Upvote, K));
 
// This code is contributed by akashish__

Output:
Maximum Rating of Coding Contest is: 60

Time Complexity: O(N * logN) Auxiliary Space: O(N)


Article Tags :