why is the two for loop solution faster than 89% of C submissions? are hindus and pakis really this dumb that they find a way to fuck up two sum in c???
why is the two for loop solution faster than 89% of C submissions? are hindus and pakis really this dumb that they find a way to fuck up two sum in c???
I wish something like this existed as a set of .c files with an edit_me.h rather than a shitty website that wants my email, name, and credit card details.
it should be easy enough to take the problem descriptions and write a main function that initializes some test cases and runs some asserts? not knocking your ability, just stating how i would solve what you're asking
but agreed, fuck this paki website
It might be an interesting scrape job, but I just wanted to be lazy and git clone gitlab/someproblems.git
Building up main from spec might work, but I'm also not sure if I have the patience to try and pound it out. Maybe over summer if I get bored
it wouldn't be worth it to make a 1 for 1 clone of the site anyway, probably 75 diverse problems is good enough
don't release it, itll just be appropriated by streetshitters and changs anyway 🙂
Maybe I'll make it available on some temp file host, show it off to LULZ or something.
I'm probably not the first guy to have the idea anyway
most people with this idea won't do it, only maybe as an exercise. because to have this idea you need to be literate enough to not give a fuck about these nigher sites anyway
why would you want to use this crap if you know how to program. this is for normie noobs trying to grind an interview to make a quick buck and change careers in 4 years when it crashes
Puzzles can be fun
I liked TIS-100 and Szehnzen I/O or however you spell it
TIS-100 is a game for retarded brainlets
It's a puzzle game with programming aesthetics.
Actual programming puzzles allow for more freedom which is fun
People do advent of code every year, this would just be more freeform and less time constraint
>he got filtered by TIS-100
the problems are on github just do them
because time complexity doesnt tell you how fast something is, just how the algorithm scales with huge inputs
an O(1) algorithm might be slower than an O(n) algorithm as long as n is small
n can be up to 10,000. you're saying that 89% percent of p*kis wrote a slow hash map?
... yes
pakis tend to copy-paste solutions
they copy the best time complexity because thats what they expect to be asked for in interviews
you can do 2sum in O(n)
Stupid question, but it's been a long time since I did a data structures and algorithms course and I'd love to get back into stuff like this - what site is this?
leetcode.com
Thanks!
yes it should be a upper/ lower triangle matrix. It is same as double loop with i<j
Programming has no soul. You know the answer instantly and the pain is to write it
>you know the answer instantly
this is a leetcode easy
>Programming has no soul.
Leetcode has no soul, it's just brownoids sharing answers and Western mental-brownoids copy pasting ChatGPT output.
>calling malloc in a hot loop
these are the retards on LULZ who complain about GC latency btw
Malloc is only called when the function returns, guaranteeing max 1 malloc per function call. So no, it's not a hot path.
>attempts premature optimization
>can't even intepret 4 lines of code correctly
Every single time
lmao absolutely btfo. the problem explicitly states my implementation must malloc and expect the caller to free. maybe he just wanted me to return a stack allocated local array :>)
Most of these slower submissions literally did the same thing you did, but the runtime is variable between the same solution so yours is luckier.
>2Sum
>6.21 MB
>Still beats ~80% of candidates
Bro... what the fuck?
brainlet here, what's the correct solution?
Most Leetcode users are absolute beginners.
The speed depends on your machine.
The speed can vary by things out of your control, it’s better to look at the distribution of speeds, to see what peak your solution lies in.
stop writing 'int* foo'. Write 'int *foo'. the former is misleading, because it looks like you should write 'int* foo, bar', but you should actually write 'int *foo, *bar'. The way C declarations work is that you write the type and then a declarator. *foo is a declarator. * is not part of the type.
also stop using camelCase. It doesn't really matter, but it just looks weird to experienced C programmers. otherwise your code looks fine.
> * is not part of the type.
Wrong
camelCase is fine as long as the code is consistent.
I will not kneel to arbitrary whitespace preferences, pythonista!
what about int * foo, * bar? :^)
int* foo = NULL;
int* bar = NULL;
I agree with the second part, although I may be biased always use snake_case in every single language.
Because most likely don't use i+1 in the second loop. It's a less obvious optimization.
I'm surprised this even worked. Most O(n^2) solutions I've tried just time out
If you want to further optimize, you should check target's parity. If target is even and i is even, j should be even. If target is even and j is odd, j should be odd.
If target is odd and i is even, j should be odd. If target is odd and i is odd, j should be even.
>O(n^2)
You are a retard Sar. No offence in name of Kali. But there is no difference reading the entries from upper diagonal matrix than reading the entries from a vector, that is resultant from vectorizing this said matrix.
Wrong guy retard
my O(1) solution runs in 2ms, but no paki would understand whats happening
I wonder if the compiler will optimize that hash map initialization into a memset.
thanks for pointing that out, i was thinking that looping would be inefficient. a syscall is likely much better and probably -O1 and above produce it, that's like their main thing right, basically inlining everything it can (especially since it's all -1).
I don't understand what's happening
the possible input values are between -1000000000 and 1000000000
there can be at most 10000 inputs
so what we need to do is reduce the range from 2 billion to 10000, and furthermore we need to shift them all to be positive because arrays cant have negative indices (e.g hash[-1])
so to do all that we use unsigned int and %
and then the rest of the solution is just normal "check if i've calculated the diff before and if i have then return that index and the current one too"
Why do you use a loop to initialize hash. Can't you just do it one line.
i want to stack allocate hash[] instead of mallocing and freeing. plus i need to make sure everything is set to -1 and not something positive
let's see paul allen's solution. let me guess, malloc and struct node? good luck getting a job offer at dorsia now you stupid bastard
Since size is defined can't you just do
int hash[size] = {-1};
based i didn't know that syntax, tyvm. it's like stack allocated calloc with a value that isnt 0 ^>^
So... it's just a nagger rigged hash table?
if by nagger rigged you mean performant, mr runtime polymorphism heap allocated fagbag
I wasn't being critical, I was just confused initially.
my bad sorry for ragging on you
how is this solution O(1)?
sorry i totally apologize, it's O(n) worst case. typing too fast
not too bad. but you can do a lot better: https://nullprogram.com/blog/2023/06/26/
>doesn't handle collisions
handling collisions with a linked list of nodes is orders of magnitudes slower than stack allocating 20000 int array goofy
ty for the link even though hes using (allah yaghfirli) g*
>sort
Alternatively...
>use a map
Did I get it right?
if the input is already sorted, two pointers is the best solution
"use a map" is the best in the unsorted case, but not if you use a pig disgusting OOP language of course
No one wants to write a fucking hashset from scratch in c
LMFAO IT'S LITERALLY 2 LINES OF CODE YOU BRAINLET L00000000L
see
Nice memory leak and using % as a hash fuction lmfao
wheres the memory leak homosexual?
I had the same reaction, did the first problem (or maybe it was the first problem in the neetcode list, determine if all entries are unique) for shits and giggles.
>check all against all O(n^2)
>can't even submit
>do the included sort, just check the next entry
>suddenly, faster than 90% of all entries
It MUST be getting spammed by thirdies.
plz ser my copy paste twosum solution is wery fastest plsz GIB H1B at yuor soonest convenience