> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2024/06-2024-june-2/03-trail-of-ones.md).

# 03. Trail of ones

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/trail-of-ones3242/1)

## My Approach

* 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 and `y` 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` in `z`.
  * Update `x` to the current value of `y`.
  * Update `y` to `(x + z) % mod`, maintaining the count of sequences ending in two consecutive ones.
* Return the final value of `out`.

## Time and Auxiliary Space Complexity

* **Time Complexity**: `(O(n))`, since we iterate from 3 to `n`.
* **Auxiliary Space Complexity**: `(O(1))`, as we use a constant amount of extra space regardless of the input size.

## Code (C++)

```cpp
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;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). 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](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/06-2024-june-2/03-trail-of-ones.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
