Can You Detect the Missing Number? (LeetCode 268: Missing Number)

  Рет қаралды 2,031

Code With Zi

Code With Zi

Күн бұрын

Пікірлер: 14
@atharvarajadhyaksha4813
@atharvarajadhyaksha4813 Ай бұрын
Instead of running a loop for expectedSum, just use the formula n*(n+1)/2 where n is size of array. Significantly reduces the running time for larger arrays.
@CodeWithZi
@CodeWithZi Ай бұрын
You are correct that also works! Thanks for recommending it. However the time complexity is still O(n) in both cases
@atharvarajadhyaksha4813
@atharvarajadhyaksha4813 Ай бұрын
@@CodeWithZi True. You still need a loop for actualSum. But for competitive programming it will always be better to avoid having an extra loop.
@Madhava_P7
@Madhava_P7 Ай бұрын
set(range(n)-set(given))
@YashGondane-p3f
@YashGondane-p3f Ай бұрын
thank you so much for this cool approach
@CodeWithZi
@CodeWithZi Ай бұрын
You're very welcome!
@danielhod53
@danielhod53 Ай бұрын
I got a bit misleaded because the example array in the beginning is sorted so my first instict was to loop through the array and calculate (array[i+1]-array[i]) and if it's not equal to 1 then the number missing is (array[i+1]+array[i]) / 2 but by seeing the question in LeetCode you can't assume that the array is sorted so it won't work
@CodeWithZi
@CodeWithZi Ай бұрын
great approach!
@black_ice7822
@black_ice7822 Ай бұрын
That’s pretty intuitive. Guess you could still do that if you sort the array first
@heathens2867
@heathens2867 Ай бұрын
Can't we use i==arr[i] and if it's false, then i is the answer
@gnanaharish
@gnanaharish Ай бұрын
Works only if array is sorted , here array is not sorted , so it will not work
@atharvarajadhyaksha4813
@atharvarajadhyaksha4813 Ай бұрын
As the other comment said, you'll have the added burden of sorting the array and depending on what sort you use, you may end up with a worst case time complexity of O(n^2)
@abhikannor9301
@abhikannor9301 Ай бұрын
Sum of size of total number of array - total sum of array
@orderandchaos_at_work
@orderandchaos_at_work Ай бұрын
2
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 253 М.
Yay😃 Let's make a Cute Handbag for me 👜 #diycrafts #shorts
00:33
LearnToon - Learn & Play
Рет қаралды 117 МЛН
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 11 МЛН
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 3,5 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 186 МЛН
Reacting to Controversial Opinions of Software Engineers
9:18
Fireship
Рет қаралды 2,1 МЛН
I Didn’t Believe that AI is the Future of Coding. I Was Right.
6:55
Sabine Hossenfelder
Рет қаралды 592 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 740 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 151 М.
before you code, learn how computers work
7:05
Low Level
Рет қаралды 521 М.
LeetCode is a JOKE with This ONE WEIRD TRICK
4:54
AlgoMonster
Рет қаралды 90 М.
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
My Brain after 569 Leetcode Problems
7:50
NeetCode
Рет қаралды 2,7 МЛН
Yay😃 Let's make a Cute Handbag for me 👜 #diycrafts #shorts
00:33
LearnToon - Learn & Play
Рет қаралды 117 МЛН