-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRepeat and Missing Number Array.py
More file actions
52 lines (34 loc) · 959 Bytes
/
Repeat and Missing Number Array.py
File metadata and controls
52 lines (34 loc) · 959 Bytes
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
"""
You are given a read only array of n integers from 1 to n.
Each integer appears exactly once except A which appears twice and B which is missing.
Return A and B.
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Note that in your output A should precede B.
Example:
Input:[3 1 2 5 3]
Output:[3, 4]
A = 3, B = 4
"""
class Solution:
# @param A : tuple of integers
# @return a list of integers
def repeatedNumber(self, A):
A = list(A)
p = 0
m = 0
for i in range(len(A)):
if (A[abs(A[i]) - 1]) > 0:
A[abs(A[i]) - 1] = -A[abs(A[i]) - 1]
else:
p = abs(A[i])
for i in range(len(A)):
if A[i] > 0:
m = i + 1
return [p, m]
"""
Testing code
"""
arr = [3, 1, 2, 5, 3]
s = Solution()
print(s.repeatedNumber(arr))
# repeating = 3 , missing = 4