# Longest substring of only 4’s from the first N characters of the infinite string

Given an integer N, the task is to find the length of the longest substring containing only 4’s from the first N characters of the infinite string str.

The string str is generated by concatenating the numbers formed by only 4’s and 5’s in increasing order. For example 4, 5, 44, 45, 54, 55 and so on. Therefore the string str looks like “4544455455444445454455…”.

Examples:

```Input : N = 4
Output : 2
First 4 characters of str are "4544".
Therefore the required length is 2.

Input : N = 10
Output : 3
First 10 characters of str are "4544455455".
Therefore the required length is 3.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The problem can be solved easily by observing the pattern. The task is to count the maximum consecutive 4’s appearing in the string. So, there is no need to generate the whole string.

We can observe a pattern if we divide the string into different groups as the first group will have 2 characters, the second group will have 4 characters, the third group will have 8 characters and so on….

For Example:

Group 1 -> 45
Group 2 -> 44455455
Group 3 -> 444445454455544545554555
.
.
.
and, so on…

Now, the task reduces to finding the group in which N lies, and how many characters it covers in that group from start.

Here,

• If N falls in group 2, the answer will be at least 3. That is if, length = 4 then answer will be 2 as with length 4, the string will cover only up to the 2nd 4 in the group and if length = 5 answer will be 3.
• Similarly, if length covers at least first 5 “4’s” from the group 3, the answer is 5.
• Now,
Group 1 have 1 * 2^1 characters
Group 2 have 2 * 2^2 characters
Generally, group K have K * 2^K characters. So the problem reduces to finding that to which group, the given N belongs to. This can be easily found by using prefix sum array pre[] where the ith element contains the sum of number of characters up to the ith group.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define MAXN 30 ` ` `  `// Function to return the length of longest ` `// contiguous string containing only 4’s from ` `// the first N characters of the string ` `int` `countMaxLength(``int` `N) ` `{ ` `    ``// Initialize result ` `    ``int` `res; ` ` `  `    ``// Initialize prefix sum array of ` `    ``// characters and product variable ` `    ``int` `pre[MAXN], p = 1; ` ` `  `    ``// Preprocessing of prefix sum array ` `    ``pre = 0; ` `    ``for` `(``int` `i = 1; i < MAXN; i++) { ` `        ``p *= 2; ` `        ``pre[i] = pre[i - 1] + i * p; ` `    ``} ` ` `  `    ``// Initialize variable to store the ` `    ``// string length where N belongs to ` `    ``int` `ind; ` ` `  `    ``// Finding the string length where ` `    ``// N belongs to ` `    ``for` `(``int` `i = 1; i < MAXN; i++) { ` `        ``if` `(pre[i] >= N) { ` `            ``ind = i; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``int` `x = N - pre[ind - 1]; ` `    ``int` `y = 2 * ind - 1; ` ` `  `    ``if` `(x >= y) ` `        ``res = min(x, y); ` `    ``else` `        ``res = max(x, 2 * (ind - 2) + 1); ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `N = 25; ` `    ``cout << countMaxLength(N); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` `     `  `static` `int` `MAXN = ``30``; ` ` `  `// Function to return the length of longest ` `// contiguous string containing only 4's from ` `// the first N characters of the string ` `static` `int` `countMaxLength(``int` `N) ` `{ ` `    ``// Initialize result ` `    ``int` `res; ` ` `  `    ``// Initialize prefix sum array of ` `    ``// characters and product variable ` `    ``int` `pre[] = ``new` `int``[MAXN]; ` `    ``int` `p = ``1``; ` ` `  `    ``// Preprocessing of prefix sum array ` `    ``pre[``0``] = ``0``; ` `    ``for` `(``int` `i = ``1``; i < MAXN; i++) ` `    ``{ ` `        ``p *= ``2``; ` `        ``pre[i] = pre[i - ``1``] + i * p; ` `    ``} ` ` `  `    ``// Initialize variable to store the ` `    ``// string length where N belongs to ` `    ``int` `ind = ``0``; ` ` `  `    ``// Finding the string length where ` `    ``// N belongs to ` `    ``for` `(``int` `i = ``1``; i < MAXN; i++) ` `    ``{ ` `        ``if` `(pre[i] >= N) ` `        ``{ ` `            ``ind = i; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``int` `x = N - pre[ind - ``1``]; ` `    ``int` `y = ``2` `* ind - ``1``; ` ` `  `    ``if` `(x >= y) ` `        ``res = Math.min(x, y); ` `    ``else` `        ``res = Math.max(x, ``2` `* (ind - ``2``) + ``1``); ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `N = ``25``; ` `    ``System.out.println(countMaxLength(N)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech. `

## Python3

 `# Python 3 implementation of the approach ` `MAXN ``=` `30` ` `  `# Function to return the length of longest ` `# contiguous string containing only 4’s from ` `# the first N characters of the string ` `def` `countMaxLength(N): ` `     `  `    ``# Initialize result ` `    ``# Initialize prefix sum array of ` `    ``# characters and product variable ` `    ``pre ``=` `[``0` `for` `i ``in` `range``(MAXN)] ` `    ``p ``=` `1` ` `  `    ``# Preprocessing of prefix sum array ` `    ``pre[``0``] ``=` `0` `    ``for` `i ``in` `range``(``1``, MAXN, ``1``): ` `        ``p ``*``=` `2` `        ``pre[i] ``=` `pre[i ``-` `1``] ``+` `i ``*` `p ` ` `  `    ``# Initialize variable to store the ` `    ``# string length where N belongs to ` ` `  `    ``# Finding the string length where ` `    ``# N belongs to ` `    ``for` `i ``in` `range``(``1``, MAXN, ``1``): ` `        ``if` `(pre[i] >``=` `N): ` `            ``ind ``=` `i ` `            ``break` ` `  `    ``x ``=` `N ``-` `pre[ind ``-` `1``] ` `    ``y ``=` `2` `*` `ind ``-` `1` ` `  `    ``if` `(x >``=` `y): ` `        ``res ``=` `min``(x, y) ` `    ``else``: ` `        ``res ``=` `max``(x, ``2` `*` `(ind ``-` `2``) ``+` `1``) ` `    ``return` `res ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``N ``=` `25` `    ``print``(countMaxLength(N)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `static` `int` `MAXN = 30; ` ` `  `// Function to return the length of longest ` `// contiguous string containing only 4's from ` `// the first N characters of the string ` `static` `int` `countMaxLength(``int` `N) ` `{ ` `    ``// Initialize result ` `    ``int` `res; ` ` `  `    ``// Initialize prefix sum array of ` `    ``// characters and product variable ` `    ``int``[] pre = ``new` `int``[MAXN]; ` `    ``int` `p = 1; ` ` `  `    ``// Preprocessing of prefix sum array ` `    ``pre = 0; ` `    ``for` `(``int` `i = 1; i < MAXN; i++) ` `    ``{ ` `        ``p *= 2; ` `        ``pre[i] = pre[i - 1] + i * p; ` `    ``} ` ` `  `    ``// Initialize variable to store the ` `    ``// string length where N belongs to ` `    ``int` `ind = 0; ` ` `  `    ``// Finding the string length where ` `    ``// N belongs to ` `    ``for` `(``int` `i = 1; i < MAXN; i++) ` `    ``{ ` `        ``if` `(pre[i] >= N) ` `        ``{ ` `            ``ind = i; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``int` `x = N - pre[ind - 1]; ` `    ``int` `y = 2 * ind - 1; ` ` `  `    ``if` `(x >= y) ` `        ``res = Math.Min(x, y); ` `    ``else` `        ``res = Math.Max(x, 2 * (ind - 2) + 1); ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `N = 25; ` `    ``Console.WriteLine(countMaxLength(N)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech. `

## PHP

 `= ``\$N``)  ` `        ``{  ` `            ``\$ind` `= ``\$i``;  ` `            ``break``;  ` `        ``}  ` `    ``}  ` ` `  `    ``\$x` `= ``\$N` `- ``\$pre``[``\$ind` `- 1];  ` `    ``\$y` `= 2 * ``\$ind` `- 1;  ` ` `  `    ``if` `(``\$x` `>= ``\$y``)  ` `        ``\$res` `= min(``\$x``, ``\$y``);  ` `    ``else` `        ``\$res` `= max(``\$x``, 2 * (``\$ind` `- 2) + 1);  ` ` `  `    ``return` `\$res``;  ` `}  ` ` `  `// Driver Code  ` `\$N` `= 25;  ` `echo` `countMaxLength(``\$N``);  ` ` `  `// This code is contributed by Ryuga ` `?> `

Output:

```5
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.