|
| 1 | +PROBLEM STATEMENT |
| 2 | + |
| 3 | +We have a sequence of N positive integers: a[0] through a[N-1]. |
| 4 | +You do not know these integers. |
| 5 | +All you know is the number of trailing zeros in their binary representations. |
| 6 | +You are given a vector <int> d with N elements. |
| 7 | +For each i, d[i] is the number of trailing zeros in the binary representation of a[i]. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +For example, suppose that a[0]=40. |
| 12 | +In binary, 40 is 101000 which ends in three zeros. |
| 13 | +Therefore, d[0] will be 3. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +You like geometric sequences. |
| 18 | +(See the Notes section for a definition of a geometric sequence.) |
| 19 | +You would like to count all non-empty contiguous subsequences of the sequence a[0], a[1], ..., a[N-1] that can be geometric sequences (given the information you have in d). |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +More precisely: |
| 24 | +For each pair (i,j) such that 0 <= i <= j <= N-1, we ask the following question: "Given the values d[i] through d[j], is it possible that the values a[i] through a[j] form a geometric sequence?" |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +For example, suppose that d = {0,1,2,3,2}. |
| 29 | +For i=0 and j=3 the answer is positive: it is possible that the values a[0] through a[3] are {1,2,4,8} which is a geometric sequence. |
| 30 | +For i=1 and j=4 the answer is negative: there is no geometric sequence with these numbers of trailing zeros in binary. |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +Compute and return the number of contiguous subsequences of a[0], a[1], ..., a[N-1] that can be geometric sequences. |
| 35 | + |
| 36 | + |
| 37 | +DEFINITION |
| 38 | +Class:PotentialGeometricSequence |
| 39 | +Method:numberOfSubsequences |
| 40 | +Parameters:vector <int> |
| 41 | +Returns:int |
| 42 | +Method signature:int numberOfSubsequences(vector <int> d) |
| 43 | + |
| 44 | + |
| 45 | +NOTES |
| 46 | +-A geometric sequence is any sequence g[0], g[1], ..., g[k-1] such that there is a real number q (the quotient) with the property that for each valid i, g[i+1] = g[i]*q. For example, {1,2,4,8} is a geometric sequence with q=2, {7,7,7} is a geometric sequence with q=1, and {18,6,2} is a geometric sequence with q=1/3. |
| 47 | + |
| 48 | + |
| 49 | +CONSTRAINTS |
| 50 | +-N will be between 1 and 50, inclusive. |
| 51 | +-d will contain exactly N elements. |
| 52 | +-Each element of d will be between 0 and 100, inclusive. |
| 53 | + |
| 54 | + |
| 55 | +EXAMPLES |
| 56 | + |
| 57 | +0) |
| 58 | +{0,1,2} |
| 59 | + |
| 60 | +Returns: 6 |
| 61 | + |
| 62 | +One possibility is that a[0]=3, a[1]=6, and a[2]=12. In this case, all contiguous subsequences of this sequence are geometric. |
| 63 | + |
| 64 | +1) |
| 65 | +{1,2,4} |
| 66 | + |
| 67 | +Returns: 5 |
| 68 | + |
| 69 | +All one-element and two-element subsequences are geometric. The entire sequence cannot be geometric. |
| 70 | + |
| 71 | +2) |
| 72 | +{3,2,1,0} |
| 73 | + |
| 74 | +Returns: 10 |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +3) |
| 79 | +{1,2,4,8,16} |
| 80 | + |
| 81 | +Returns: 9 |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +4) |
| 86 | +{1,3,5,5,5,5,64,4,23,2,3,4,5,4,3} |
| 87 | + |
| 88 | +Returns: 37 |
| 89 | + |
| 90 | + |
0 commit comments