---
# System prepended metadata

title: 2028. Find Missing Observations
tags: [Leetcode, Medium, Math]

---

# 2028. Find Missing Observations
###### tags: `Leetcode` `Medium` `Math`
Link: https://leetcode.com/problems/find-missing-observations/description/
## 思路
首先算出n个observation的和
如果invalid直接回传空的array或者list
然后算出平均数d和余数r 前r个roll我们设成d+1 后面的都设成d 就能得到一个答案
注意python 如果直接写```d=sum/n``` 得到的```d```是小数 可以用```divmod(a,b)```直接得到结果
## Code
```java=
class Solution {
    public int[] missingRolls(int[] rolls, int mean, int n) {
        int sum = mean*(rolls.length+n);
        for(int roll:rolls) sum -= roll;
        if(sum > 6*n || sum < n) return new int[]{};
        int d = sum/n;
        int r = sum%n;
        int[] ans = new int[n];
        for(int i=0; i<n; i++){
            if(i<r) ans[i] = d+1;
            else ans[i] = d;
        }
        return ans;
    }
}
```
```python=
class Solution:
    def missingRolls(self, rolls: List[int], mean: int, n: int) -> List[int]:
        sum = mean*(len(rolls)+n)
        for roll in rolls: sum -= roll
        if sum>6*n or sum<n: return []
        # d = sum//n
        # r = sum%n
        d,r = divmod(sum, n)
        ans = []
        for i in range(n):
            if i<r: ans.append(d+1)
            else: ans.append(d)
        return ans
```