-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissing.py
More file actions
59 lines (39 loc) · 1.39 KB
/
missing.py
File metadata and controls
59 lines (39 loc) · 1.39 KB
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
from typing import List
class Solution(object):
def findMissingAndRepeatedValues(self, grid):
"""
:type grid: List[List[int]]
:rtype: List[int]
"""
n = len(grid)
N = n * n
actualSum = 0
actualSquareSum = 0
# Compute the actual sum and sum of squares from the grid
for i in range(n):
for j in range(n):
num = grid[i][j]
actualSum += num
actualSquareSum += num * num
# Compute the expected sum and sum of squares for numbers from 1 to N
expectedSum = (N * (N + 1)) // 2
expectedSquareSum = (N * (N + 1) * (2 * N + 1)) // 6
# Calculate the differences
sumDifference = actualSum - expectedSum # a - b
squareSumDifference = actualSquareSum - expectedSquareSum # a² - b²
# Use derived equations to find repeated (a) and missing (b)
sum_ab = squareSumDifference // sumDifference # a + b
repeated = (sum_ab + sumDifference) // 2
missing = (sum_ab - sumDifference) // 2
return [repeated, missing]
# Run the function with a sample grid
if __name__ == "__main__":
# Example: 1 is repeated, 3 is missing
grid = [
[1, 2],
[1, 4]
]
sol = Solution()
result = sol.findMissingAndRepeatedValues(grid)
print("Repeated:", result[0])
print("Missing:", result[1])