Bharathgopal / May2023

Learning C Programming
GNU General Public License v2.0
28 stars 173 forks source link

Need help in optimizing time complexity for a program on HackerEarth #8

Open logeshps02 opened 1 year ago

logeshps02 commented 1 year ago

I recently attempted a programming problem on HackerEarth, and I'm looking for some guidance to optimize the time complexity of my code. I would appreciate any help or suggestions to improve its efficiency.

The problem statement is as follows: Problem name : Favourite singer - https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/favourite-singer-a18e086a/

Bob has a playlist of N songs, each song has a singer associated with it (denoted by an integer) Favourite singer of Bob is the one whose songs are the most on the playlist Count the number of Favourite Singers of Bob

Here's my current code: fav singer 1.txt fav singer 2.txt

The code is working correctly, but it is not performing efficiently for larger input sizes. I'm specifically looking for ways to reduce the time complexity to improve its runtime.

If anyone could provide insights, suggestions, or alternative approaches to solve this problem more efficiently, I would be grateful.

Thank you in advance for your assistance.

AdityaSrivastava3107 commented 1 year ago

I am also having the same issue, time limit is getting exceeded in three test cases. Here is my current code-

include

int main() { int n; scanf("%d", &n); int songs[n]; int i, j; for (i = 0; i < n; i++) { scanf("%d", &songs[i]); } long long int count = 0; int maxCount = 0; int favoriteSingers = 0; for (i = 0; i < n; i++) { count = 0; for (j = i; j < n; j++) { if (songs[i] == songs[j]) { count++; } } if (count > maxCount) { maxCount = count; favoriteSingers = 1; } else if (count == maxCount) { favoriteSingers++; } } printf("%d\n", favoriteSingers); return 0; }

Brisingr001 commented 1 year ago

Here's my attempt at the code. Was able to clear 8 test cases out of 13. All the rest end up in Runtime error. Not really sure on what is the fix for this. It probably has to do with the fact that the edge cases have inputs which are 15 digits long

#include <stdio.h>
#define MAX_SIZE 200000
typedef long long int64;
int main(){
    int64 num, max_count=0, num_fav=0, finish=0;
    scanf("%lld", &num);
    int64 sing[MAX_SIZE]={0}, count[MAX_SIZE]={0};
    for (int64 i=1; i<=num; i++)
    {
        scanf("%lld", &sing[i]);
        count[sing[i]]++;
        if(count[sing[i]]>max_count)
            max_count=count[sing[i]];
    }
    for(int64 n =1; n<= 200000; n++)
    {
        if (max_count==count[n])
            finish++;
    }   
    printf("%lld",finish);
    }
saurabhbedre07 commented 3 months ago

my attempt at the code

include

include

define MAX_SIZE 200000

typedef long long int64;

int main() { int64 num, max_count = 0, finish = 0; std::cin >> num;

std::vector<int64> sing(num + 1, 0); // Array size based on the number of inputs
std::vector<int64> count(MAX_SIZE, 0); // Array to count occurrences

for (int64 i = 1; i <= num; i++) {
    std::cin >> sing[i];
    count[sing[i]]++;
    if (count[sing[i]] > max_count) {
        max_count = count[sing[i]];
    }
}

for (int64 n = 1; n <= 200000; n++) {
    if (max_count == count[n]) {
        finish++;
    }
}

std::cout << finish << std::endl;

return 0;

}