Longest substring of only 4’s from the first N characters of the infinite string
Last Updated :
19 Dec, 2022
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,
- If N falls in group 2, the answer will be at least 3. That is if, length = 4 then the 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 the first 5 “4’s” from group 3, the answer is 5.
Now,
Group 1 has 1 * 2^1 characters
Group 2 has 2 * 2^2 characters
Generally, group K has K * 2^K characters. So the problem reduces to finding to which group, the given N belongs to. This can be easily found by using the prefix sum array pre[] where the ith element contains the sum of a number of characters up to the ith group.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 30
int countMaxLength( int N)
{
int res;
int pre[MAXN], p = 1;
pre[0] = 0;
for ( int i = 1; i < MAXN; i++) {
p *= 2;
pre[i] = pre[i - 1] + i * p;
}
int ind;
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;
}
int main()
{
int N = 25;
cout << countMaxLength(N);
return 0;
}
|
Java
class GFG
{
static int MAXN = 30 ;
static int countMaxLength( int N)
{
int res;
int pre[] = new int [MAXN];
int p = 1 ;
pre[ 0 ] = 0 ;
for ( int i = 1 ; i < MAXN; i++)
{
p *= 2 ;
pre[i] = pre[i - 1 ] + i * p;
}
int ind = 0 ;
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;
}
public static void main(String[] args)
{
int N = 25 ;
System.out.println(countMaxLength(N));
}
}
|
Python3
MAXN = 30
def countMaxLength(N):
pre = [ 0 for i in range (MAXN)]
p = 1
pre[ 0 ] = 0
for i in range ( 1 , MAXN, 1 ):
p * = 2
pre[i] = pre[i - 1 ] + i * p
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
if __name__ = = '__main__' :
N = 25
print (countMaxLength(N))
|
C#
using System;
class GFG
{
static int MAXN = 30;
static int countMaxLength( int N)
{
int res;
int [] pre = new int [MAXN];
int p = 1;
pre[0] = 0;
for ( int i = 1; i < MAXN; i++)
{
p *= 2;
pre[i] = pre[i - 1] + i * p;
}
int ind = 0;
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;
}
public static void Main()
{
int N = 25;
Console.WriteLine(countMaxLength(N));
}
}
|
PHP
<?php
$MAXN = 30;
function countMaxLength( $N )
{
$res = 0;
$pre = array ();
$p = 1;
$pre [0] = 0;
for ( $i = 1; $i < $GLOBALS [ 'MAXN' ]; $i ++)
{
$p *= 2;
$pre [ $i ] = $pre [ $i - 1] + $i * $p ;
}
$ind = 0;
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 ;
}
$N = 25;
echo countMaxLength( $N );
?>
|
Javascript
<script>
var MAXN = 30;
function countMaxLength(N)
{
var res;
var pre = Array(MAXN), p = 1;
pre[0] = 0;
for ( var i = 1; i < MAXN; i++)
{
p *= 2;
pre[i] = pre[i - 1] + i * p;
}
var ind;
for ( var i = 1; i < MAXN; i++)
{
if (pre[i] >= N)
{
ind = i;
break ;
}
}
var x = N - pre[ind - 1];
var y = 2 * ind - 1;
if (x >= y)
res = Math.min(x, y);
else
res = Math.max(x, 2 * (ind - 2) + 1);
return res;
}
var N = 25;
document.write(countMaxLength(N));
</script>
|
Time Complexity: O(MAXN)
Auxiliary Space: O(MAXN), where MAXN is a predefined value here. MAXN = 30
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...