Question
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
My code:
class Solution:
def repeatedNumber(self, A):
n=len(A)
asum=0
rsum = (n*(n+1))//2
x=0
dict={}
for i in A:
asum+=A[i]
if A[i] in dict:
x=A[i]
else:
dict[i]=1
diff=rsum-asum
return x,x+diff