Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Roy and Trending Topics problem solution

YASH PAL, 31 July 2024
In this HackerEarth Roy and Trending Topics problem solution, Roy is trying to develop a widget that shows Trending Topics (similar to Facebook) on the home page of HackerEarth Academy.
He has gathered a list of N Topics (their IDs) and their popularity score (say z-score) from the database. Now z-score change every day according to the following rules:
  1. When a topic is mentioned in a ‘Post’, its z-score is increased by 50.
  2. A ‘Like’ on such a Post, increases the z-score by 5.
  3. A ‘Comment’ increases the z-score by 10.
  4. A ‘Share’ causes an increment of 20.
Now the Trending Topics are decided according to the change in z-score. One with the highest increment comes on top and the list follows.
Roy seeks your help to write an algorithm to find the top 5 Trending Topics.
If a change in z-score for any two topics is the same, then rank them according to their ID (one with higher ID gets priority). It is guaranteed that IDs will be unique.
HackerEarth Roy and Trending Topics problem solution

HackerEarth Roy and Trending Topics problem solution.

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
using namespace std;
vector < pair<long long, long long> > topics;
map<long long,long long> id_zscore_map;
int main()
{
int n;
long long id, z, p, l, c, s;
scanf(" %d",&n);
for(int i=0;i<n;i++)
{
scanf(" %lld %lld %lld %lld %lld %lld",&id,&z,&p,&l,&c,&s);
topics.push_back(make_pair( (p*50 + l*5 + c*10 + s*20 - z) , id));
id_zscore_map[id] = z;
}
make_heap(topics.begin(), topics.end());
for(int i=0;i<5;i++)
{
printf("%lld %lldn",topics.front().second, id_zscore_map[topics.front().second] + topics.front().first);
pop_heap (topics.begin(),topics.end()); topics.pop_back();
}
return 0;
}
coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes