Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
Last Updated :
31 Jan, 2023
Given a number as a string, write a function to find the number of substrings (or contiguous subsequences) of the given string which recursively add up to 9.
Example:
Digits of 729 recursively add to 9,
7 + 2 + 9 = 18
Recur for 18
1 + 8 = 9
Examples:
Input: 4189
Output: 3
There are three substrings which recursively add to 9.
The substrings are 18, 9 and 189.
Input: 999
Output: 6
There are 6 substrings which recursively add to 9.
9, 99, 999, 9, 99, 9
All digits of a number recursively add up to 9, if only if the number is multiple of 9. We basically need to check for s%9 for all substrings s. One trick used in below program is to do modular arithmetic to avoid overflow for big strings.
Following is a simple implementation based on this approach. The implementation assumes that there are no leading 0’s in input number.
C++
#include <iostream>
#include <cstring>
using namespace std;
int count9s(string number)
{
int count = 0;
int n = number.size();
for ( int i = 0; i < n; i++)
{
int sum = number[i] - '0' ;
if (number[i] == '9' ) count++;
for ( int j = i+1; j < n; j++)
{
sum = (sum + number[j] - '0' )%9;
if (sum == 0)
count++;
}
}
return count;
}
int main()
{
cout << count9s( "4189" ) << endl;
cout << count9s( "1809" );
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int count9s(String number)
{
int count = 0 ;
int n = number.length();
for ( int i = 0 ; i < n; i++)
{
int sum = number.charAt(i) - '0' ;
if (number.charAt(i) == '9' )
count++;
for ( int j = i + 1 ;
j < n; j++)
{
sum = (sum +
number.charAt(j) -
'0' ) % 9 ;
if (sum == 0 )
count++;
}
}
return count;
}
public static void main (String[] args)
{
System.out.println(count9s( "4189" ));
System.out.println(count9s( "1809" ));
}
}
|
Python 3
def count9s(number):
count = 0
n = len (number)
for i in range (n):
sum = ord (number[i]) - ord ( '0' )
if (number[i] = = '9' ):
count + = 1
for j in range (i + 1 , n):
sum = ( sum + ord (number[j]) -
ord ( '0' )) % 9
if ( sum = = 0 ):
count + = 1
return count
if __name__ = = "__main__" :
print (count9s( "4189" ))
print (count9s( "1809" ))
|
C#
using System;
class GFG
{
static int count9s(String number)
{
int count = 0;
int n = number.Length;
for ( int i = 0; i < n; i++)
{
int sum = number[i] - '0' ;
if (number[i] == '9' )
count++;
for ( int j = i + 1;
j < n; j++)
{
sum = (sum + number[j] -
'0' ) % 9;
if (sum == 0)
count++;
}
}
return count;
}
public static void Main ()
{
Console.WriteLine(count9s( "4189" ));
Console.WriteLine(count9s( "1809" ));
}
}
|
PHP
<?php
function count9s( $number )
{
$count = 0;
$n = strlen ( $number );
for ( $i = 0; $i < $n ; $i ++)
{
$sum = $number [ $i ] - '0' ;
if ( $number [ $i ] == '9' ) $count ++;
for ( $j = $i + 1; $j < $n ; $j ++)
{
$sum = ( $sum + $number [ $j ] - '0' ) % 9;
if ( $sum == 0)
$count ++;
}
}
return $count ;
}
echo count9s( "4189" ), "\n" ;
echo count9s( "1809" );
?>
|
Javascript
<script>
function count9s(number){
let count = 0;
let n = (number.length);
for (let i = 0; i < n; i++)
{
let sum = number[i] - '0' ;
if (number[i] == '9' ){ count++;}
for (let j = i+1; j < n; j++)
{
sum = (sum + number[j] - '0' )%9;
if (sum == 0){
count++;
}
}
}
return count;
}
document.write( count9s( "4189" ) );
document.write( "</br>" );
document.write( count9s( "1809" ));
</script>
|
Time complexity of the above program is O(n2). Please let me know if there is a better solution.
Auxiliary Space :O(1), since no extra space is used.
Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
Share your thoughts in the comments
Please Login to comment...