New course! Every coder should learn Generative AI!
Try a free lesson+ 8
A simple example- Suppose you have to find the missing number in the series -
1, 2, 4, 5, 6.
Then take 'n' as 6 and do n(n+1)/2.
So 6(6+1)/2 = 42/2 = 21.
Now sum all elements 1+2+4+5+6=18
Now subtract 21-18 = 3, which is the missing number in the series.
+ 3
Have you tried it yourself?
+ 3
Hey i have found a really nice and efficient algorithm of this problem.
If you have been stuck anywhere , must watch this video . :slight_smile:
video link : https://youtu.be/uQ_YsvOuXRY
+ 2
Selin Genkur I know this task with an sorted array. But your right. For unsorted array get sum and compare it with expected sum is the easiest way.
+ 2
now, Avinesh , if you were in a language that doesn't have support for large numbers like python has, and instead of the numbers from 1 to 100 you had to find the missing number in an arbitrary range, and that range was close to the maximum value of the longest int type of that language so you can't sum or use a formula because it would overflow: you XOR your array with all the numbers in the range, and this way they would all cancel each other, except for the missing one 😉
+ 2
mylist = [3, 2, 4, 1, 6, 8, 9, 7, 10] # <-- list with missing number, increase the list as required.
for x in range(1, len(mylist)):
if x not in mylist:
print(x, "is missing")
break # <--remove this if you have more than one missing number
+ 2
It's very simple Dear...
List=[ ]
For I in range(101):
If I not in List:
print(I)
It will print all no between 1 to 100 which are not in List.
+ 1
Äva(Frozen)
What have you tried so far?
If you are familiar with loops then it should be not to difficult.
You can use a variable which starts at 1. Compare this with the first element. If its same, increment the variable and compare it with the second element and so on.
If its not equal -> you have found the missing number.
+ 1
@Selin Genkur
with my limited knowledge of programming...this is what I can up with (and it's helping me to learn)
There is a "break" statement (I commented out while I was testing it) when the missing number is found.
Hope this comment doesn't come across as "nasty"..I just wanted to point out the "break" in the code.
+ 1
lst = range(0, 100)
a = sorted(set(range(lst[0], lst[-1])) - set(lst))
print(a)
0
Denise Roßberg I don't think the numbers are in ascending order, or in any order. But it's still simple: you sum them, and you know what their sum should be and see what's missing.
0
rodwynnejones that's great because it works for more than one number, but I think the point of the exercise was to find an efficient way of finding just one number, because that can be optimized. If you do it like this, you didn't take advantage of the fact that there's only one number.
But then again, if you'd want speed efficiency, you wouldn't do it in python, I suppose :)
0
Have a look at sets....symmetric_difference.
0
integers=[]
#generate a list of 100 integers with this.
for i in range(100):
integers.append(i+1)
#run a double for loop with missing numbers and the generated loop you can use "in" to test against your list which returns a boolean
if you still cant get it tell me so I'd post the code.
0
In what programming language?
0
5788885