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.
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,
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 <bits/stdc++.h> 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] = 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] = 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
<?php // PHP 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 function countMaxLength($N) { // Initialize result $res = 0; // Initialize prefix sum array of // characters and product variable $pre = array(); $p = 1; // Preprocessing of prefix sum array $pre[0] = 0; for ($i = 1; $i < $GLOBALS['MAXN']; $i++) { $p *= 2; $pre[$i] = $pre[$i - 1] + $i * $p; } // Initialize variable to store the // string length where N belongs to $ind = 0; // Finding the string length where // N belongs to for ($i = 1; $i < $GLOBALS['MAXN']; $i++) { 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 $N = 25; echo countMaxLength($N); // This code is contributed by Ryuga ?> |
5
Recommended Posts:
- Find the longest substring with k unique characters in a given string
- Length of the longest substring without repeating characters
- Print Longest substring without repeating characters
- Length of the longest substring with consecutive characters
- Find length of longest subsequence of one string which is substring of another string
- Searching characters and substring in a String in Java
- Longest substring of 0s in a string formed by k concatenations
- Minimum steps to delete a string by deleting substring comprising of same characters
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Largest substring with same Characters
- Longest substring with count of 1s more than 0s
- Longest Non-palindromic substring
- Longest substring of vowels
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.



