Skip to content

1018. Binary Prefix Divisible By 5

  • Time:
  • Space:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  vector<bool> prefixesDivBy5(vector<int>& A) {
    vector<bool> ans;
    int num = 0;

    for (int a : A) {
      num = (num * 2 + a) % 5;
      ans.push_back(num % 5 == 0);
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
  public List<Boolean> prefixesDivBy5(int[] A) {
    List<Boolean> ans = new ArrayList<>();
    int num = 0;

    for (int a : A) {
      num = (num * 2 + a) % 5;
      ans.add(num % 5 == 0);
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
  def prefixesDivBy5(self, A: List[int]) -> List[bool]:
    ans = []
    num = 0

    for a in A:
      num = (num * 2 + a) % 5
      ans.append(num % 5 == 0)

    return ans