FIND MISSING NUMBER IN AN ARRAY IN PYTHON
Vložit
- čas přidán 23. 01. 2020
- FIND MISSING NUMBER IN AN ARRAY(SUMMATION & X-OR) - PYTHON INTERVIEW QUESTION
GitHub Link:- github.com/netsetos/python_co...
~-~~-~~~-~~-~
Please watch: "LRU Cache (With Python Code) "
• LRU Cache Implementati...
~-~~-~~~-~~-~
def missing_num(s):
first=min(s)
last=max(s)
for i in range(first,last+1):
if i not in s:
print(i)
s=[1,2,4,5,6,7]
missing_num(s)
Sorry to say bit i think u have complicated it. I used the below:
a = [9,1,2,4,5,7,8]
def missing_nos(a):
a.sort()
ln_no = a[-1]
mss_no = []
for i in range(ln_no+1):
if i in a:
pass
else:
mss_no.append(i)
return mss_no
print(missing_nos(a))
But nice series.. Thanks!!
The que is using summation method
Could you please explain why the range (1,n+2)
n is 6 , but we want to a range from 1 to 7.
range(1,6+2) give a range from 1 to 7
Simpler Version
lst=[1,3,5,6,7,8,20]
def missing_no(lst):
list1=[]
for i in range(1,lst[-1]):
if i not in lst:
list1.append(i)
return (list1)
print(missing_no(lst))
I like it
It is correct but it will fail for less time complexity.
It looks simple but it is not ideal.
Time complexity can be very high if running large data.
can you please explain line -"14,15" why index is not been read , how come the value is reading there.
Sorry,I am not able to get your question
Can you post more algorithm based interview questions
Sure, I will
Awesome video
I am not getting(1, n+2 )
n means length of the list, the above n is 6
we need to iterate through 7 , so that n+2 => 6+2 = 8
**It will only possible when the missing count is 1.
And the xor method also only possible when the missing count is 1.
What if we are missing two numbers
a=[1,2,4,6,7,8,10,15]
n=a[-1]
for i in range (1,(n+1)):
if i not in a:
print(i,end=(","))
use function also if u need
arr= [1,2,5,6,7]
def missing_mul_num(arr):
all_num = set(range(1,max(arr)+1)) #7
given_arr = set(arr)
missing_numbers = sorted(all_num - given_arr)
return missing_numbers
missing_mul_num(arr)
n=a[-1] . How is it comes?
Basically, a = that array...[1,2,4,5,6,7]
And a[-1] means last item of that array....is 7
So a[-1]=n....which is 7
a[-1] = 7
How could you tell me how you have taken n=[-1] replay please
At 6:22 It is n=a[-1], In Python language, we take last number by this reference. Here n=7
My Solution:
def missing_num(list1):
missing_list=[]
for i in range(list1[0], list1[-1]+1):
if i not in list1:
missing_list.append(i)
return missing_list
list1=[1, 3, 4, 5, 6, 7]
missing_num(list1)
For xor method?
n=a[-1] this step is confusing
it will prints the last element of a list
Thanks
import itertools
a = [1,2,3,6,8]
b = [*range(1,max(a))]
print(set(a).symmetric_difference(b))
very irritating music
import numpy as np
a = np.array((1,2,4,5,6,7))
from functools import reduce
sum_a = reduce(lambda x,y: x+y, a)
sum_actual = reduce(lambda x,y : x+y, range(8))
print(sum_actual, sum_a)
print("The missing number is", sum_actual - sum_a)