Problem Statement
Warning: This problem statement contains superscripts and/or subscripts. It may not display properly outside of the applet.
In this problem we will consider binary polynomials. A binary polynomial can be written in the following form:
P(x) = a[0] * x0 + a[1] * x1 + ... + a[n] * xn
The values a[i] are integer constants (called coefficients). For a binary polynomial we must have a[n] = 1 and each other a[i] must be either 0 or 1. The number n is called the degree of the polynomial.
Several examples:
- P(x) = 1 * x0 + 0 * x1 + 1 * x2 is a binary polynomial of degree 2.
- P(x) = 0 * x0 + 1 * x1 + 0 * x2 + 1 * x3 is a binary polynomial of degree 3.
- P(x) = 1 * x0 is a binary polynomial of degree 0.
- P(x) = 0 * x0 + 3 * x1 - 1 * x2 is not a binary polynomial, because each coefficient must be a 0 or a 1.
- P(x) = 0 * x0 is not a valid binary polynomial, because the last of the values a[i] must be 1.
We call an integer x (where x is 0 or 1) a root of the binary polynomial P if P(x) = 0. You are given a binary polynomial P as the array a of its coefficients. Return the number of roots of that binary polynomial.
Definition
- Class:
- BinaryPolynomialDivTwo
- Method:
- countRoots
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int countRoots(int[] a)
- (be sure your method is public)
Constraints
- The degree of P will be between 0 and 49, inclusive.
- a will contain exactly n+1 elements, where n is the degree of P.
- Each element of a will be either 0 (zero) or 1 (one).
- a[n] will be equal to 1 (one).
Examples
{1, 0, 1}
Returns: 1
The example from the problem statement. The only root of this binary polynomial is 1.
{0, 1, 0, 1}
Returns: 2
This is the maximum possible answer; both 0 and 1 are roots of this binary polynomial.
{1}
Returns: 0
This binary polynomial has no roots - it always evaluates to 1.
{1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1}
Returns: 0
{1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1}
Returns: 1
{1, 1, 0, 0, 1, 0, 1, 1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