The problem can be found at the following link: Question Link
- Initialize
mod
to (10^9 + 7) to handle large numbers and prevent overflow. - Initialize
out
to 1, which will store the number of valid sequences of ones. - Initialize
x
to 0 andy
to 1, representing the number of valid sequences ending in a single one or two consecutive ones respectively. - Iterate from 3 to
n
:- Update
out
to(out * 2 + x + y) % mod
to include new sequences formed by appending "0" or "1" to previous valid sequences. - Temporarily store the current value of
x
inz
. - Update
x
to the current value ofy
. - Update
y
to(x + z) % mod
, maintaining the count of sequences ending in two consecutive ones.
- Update
- Return the final value of
out
.
- Time Complexity:
(O(n))
, since we iterate from 3 ton
. - Auxiliary Space Complexity:
(O(1))
, as we use a constant amount of extra space regardless of the input size.
class Solution {
public:
int numberOfConsecutiveOnes(int n) {
int mod = 1e9 + 7;
long out = 1;
int x = 0, y = 1;
for (int i = 3; i <= n; ++i) {
out = (out * 2 + x + y) % mod;
int z = x;
x = y;
y = (x + z) % mod;
}
return out;
}
};
I always encourage contributors to participate in the discussion forum for this repository.
If you have a better solution or any queries / discussions related to the Problem of the Day
solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.