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…”.
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….
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.
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:
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.
- Find the longest substring with k unique characters in a given string
- Longest substring with atmost K characters from the given set of characters
- Longest substring such that no three consecutive characters are same
- Length of longest substring having all characters as K
- Length of the longest substring with consecutive characters
- Print Longest substring without repeating characters
- Length of the longest substring without repeating characters
- Find length of longest substring with at most K normal characters
- Longest substring with K unique characters using Binary Search
- Find length of longest subsequence of one string which is substring of another string
- Searching characters and substring in a String in Java
- Find the String having each substring with exactly K distinct characters
- Longest palindromic string possible after removal of a substring
- 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
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.