GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Arrays/find_first_missing_positive.py at master · vedsgit/coding-problems

Expand file tree

/

find_first_missing_positive.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
90 lines (69 loc) · 1.9 KB
/

find_first_missing_positive.py

Copy path

File metadata and controls

90 lines (69 loc) · 1.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'''
Find first missing positive integer (>0)
Given an array of integers, find the first missing positive integer in linear time and constant space.
In other words, find the lowest positive integer that does not exist in the array.
The array can contain duplicates and negative numbers as well.
Note: you can modify the input array in-place.
Input: [3, 4, -1, 1]
Output: 2
Input: [1, 2, 0]
Output: 3
=========================================
Play with indicies and mark them, a marked index means that the number equals to that index exist in the array.
Time Complexity: O(N)
Space Complexity: O(1)
'''
############
# Solution #
############
def find_first_missing(a):
n = len(a)
# eliminate all zeros and all negative numbers
for i in range(n):
if a[i] < 1:
a[i] = n + 1 # those elements aren't used later
# find all numbers in the range and mark all numbers at those positions as negative numbers
for i in range(len(a)):
idx = abs(a[i]) - 1
if idx >= n:
continue
val = a[idx]
if val > 0:
# mark element as found for the first time
a[idx] = -val
# find the first non-negative position
for i in range(n):
if a[i] > 0:
return i + 1
return n + 1
###########
# Testing #
###########
# Test 1
# Correct result => 1
test = [-1, 2, 3]
print(find_first_missing(test))
# Test 2
# Correct result => 2
test = [3, 4, -1, 1]
print(find_first_missing(test))
# Test 3
# Correct result => 3
test = [1, 2, 0]
print(find_first_missing(test))
# Test 4
# Correct result => 4
test = [1, 2, 3]
print(find_first_missing(test))
# Test 5
# Correct result => 5
test = [-4, -1, -3, -1]
print(find_first_missing(test))
# Test 6
# Correct result => 6
test = [2, 1, 2, -1, 0, 20]
print(find_first_missing(test))
# Test 7
# Correct result => 7
test = [1, 2, 5, 5, 1, 2]
print(find_first_missing(test))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.