1313Output: 3
1414
1515=========================================
16- Play with indicies and mark them, a marked index means that the number equals to that index exist in the array.
16+ Move all values to their positions (val position = val - 1), in the end find the first
17+ position which doesn't have the needed value.
18+ Time Complexity: O(N) , maybe nested loops look like O(N^2) but that not true
19+ Space Complexity: O(1)
20+ Play with indicies and mark them (make it negative),
21+ a marked index means that the number equals to that index exist in the array.
1722 Time Complexity: O(N)
1823 Space Complexity: O(1)
1924'''
2025
2126
22- ############
23- # Solution #
24- ############
27+ ##############
28+ # Solution 1 #
29+ ##############
30+
31+ def find_first_missing_1 (a ):
32+ n = len (a )
33+
34+ for i in range (n ):
35+ while (a [i ] > 0 ) and (a [i ] <= n ):
36+ swap = a [i ] - 1
37+ if a [i ] == a [swap ]:
38+ break
39+
40+ # swap elements
41+ temp = a [i ]
42+ a [i ] = a [swap ]
43+ a [swap ] = temp
44+
45+ for i in range (n ):
46+ if a [i ] - 1 != i :
47+ return i + 1
48+
49+ return n + 1
50+
2551
26- def find_first_missing (a ):
52+ ##############
53+ # Solution 2 #
54+ ##############
55+
56+ def find_first_missing_2 (a ):
2757 n = len (a )
2858
2959 # eliminate all zeros and all negative numbers
3060 for i in range (n ):
31- if a [i ] < 1 :
32- a [i ] = n + 1 # those elements aren't used later
61+ if a [i ] <= 0 :
62+ a [i ] = n + 1 # those values won't be used later
3363
3464 # find all numbers in the range and mark all numbers at those positions as negative numbers
35- for i in range (len ( a ) ):
65+ for i in range (n ):
3666 idx = abs (a [i ]) - 1
3767 if idx >= n :
3868 continue
39- val = a [idx ]
4069
41- if val > 0 :
42- # mark element as found for the first time
43- a [idx ] = - val
70+ # mark the element as found
71+ a [idx ] = - abs (a [idx ])
4472
4573 # find the first non-negative position
4674 for i in range (n ):
@@ -57,34 +85,47 @@ def find_first_missing(a):
5785# Test 1
5886# Correct result => 1
5987test = [- 1 , 2 , 3 ]
60- print (find_first_missing (test ))
88+ print (find_first_missing_1 (list (test ))) # make a copy, the list will be changed inside the function
89+ print (find_first_missing_2 (list (test )))
6190
6291# Test 2
6392# Correct result => 2
6493test = [3 , 4 , - 1 , 1 ]
65- print (find_first_missing (test ))
94+ print (find_first_missing_1 (list (test )))
95+ print (find_first_missing_2 (list (test )))
6696
6797# Test 3
6898# Correct result => 3
6999test = [1 , 2 , 0 ]
70- print (find_first_missing (test ))
100+ print (find_first_missing_1 (list (test )))
101+ print (find_first_missing_2 (list (test )))
71102
72103# Test 4
73104# Correct result => 4
74105test = [1 , 2 , 3 ]
75- print (find_first_missing (test ))
106+ print (find_first_missing_1 (list (test )))
107+ print (find_first_missing_2 (list (test )))
76108
77109# Test 5
78110# Correct result => 5
79111test = [- 4 , - 1 , - 3 , - 1 ]
80- print (find_first_missing (test ))
112+ print (find_first_missing_1 (list (test )))
113+ print (find_first_missing_2 (list (test )))
81114
82115# Test 6
83116# Correct result => 6
84117test = [2 , 1 , 2 , - 1 , 0 , 20 ]
85- print (find_first_missing (test ))
118+ print (find_first_missing_1 (list (test )))
119+ print (find_first_missing_2 (list (test )))
86120
87121# Test 7
88122# Correct result => 7
89123test = [1 , 2 , 5 , 5 , 1 , 2 ]
90- print (find_first_missing (test ))
124+ print (find_first_missing_1 (list (test )))
125+ print (find_first_missing_2 (list (test )))
126+
127+ # Test 8
128+ # Correct result => 4
129+ test = [1 , 2 , 3 , 5 , 1 , 2 , 3 , 3 ]
130+ print (find_first_missing_1 (list (test )))
131+ print (find_first_missing_2 (list (test )))
0 commit comments