Sub-string Divisibility by 3 Queries
Last Updated :
21 Jul, 2022
Given a large number, n (having number digits up to 10^6) and various queries of the form :
Query(l, r) : find if the sub-string between the indices l and r (Both inclusive) are divisible by 3.
Examples:
Input: n = 12468236544
Queries:
l=0 r=1
l=1 r=2
l=3 r=6
l=0 r=10
Output:
Divisible by 3
Divisible by 3
Not divisible by 3
Divisible by 3
Explanation:
In the first query, 12 is divisible by 3
In the second query, 24 is divisible by 3 and so on.
We know that any number is divisible by 3 if the sum of its digits is divisible by 3. Hence the idea is to pre-process an auxiliary array that would store the sum of digits.
Mathematically,
sum[0] = 0
and
for i from 0 to number of digits of number:
sum[i+1] = sum[i]+ toInt(n[i])
where toInt(n[i]) represents the integer value
of i'th digit of n
Once our auxiliary array is processed, we can answer each query in O(1) time, because the substring from indices l to r would be divisible by 3 only if, (sum[r+1]-sum[l])%3 == 0
Below is a the implementation program for the same.
C++
#include <iostream>
using namespace std;
int sum[1000005];
int toInt( char x)
{
return int (x) - '0' ;
}
void prepareSum(string s)
{
sum[0] = 0;
for ( int i=0; i<s.length(); i++)
sum[i+1] = sum[i] + toInt(s[i]);
}
void query( int l, int r)
{
if ((sum[r+1]-sum[l])%3 == 0)
cout << "Divisible by 3\n" ;
else
cout << "Not divisible by 3\n" ;
}
int main()
{
string n = "12468236544" ;
prepareSum(n);
query(0, 1);
query(1, 2);
query(3, 6);
query(0, 10);
return 0;
}
|
Java
class GFG
{
static int sum[] = new int [ 1000005 ];
static int toInt( char x)
{
return x - '0' ;
}
static void prepareSum(String s)
{
sum[ 0 ] = 0 ;
for ( int i = 0 ; i < s.length(); i++)
{
sum[i + 1 ] = sum[i] + toInt(s.charAt(i));
}
}
static void query( int l, int r)
{
if ((sum[r + 1 ] - sum[l]) % 3 == 0 )
{
System.out.println( "Divisible by 3" );
}
else
{
System.out.println( "Not divisible by 3" );
}
}
public static void main(String[] args)
{
String n = "12468236544" ;
prepareSum(n);
query( 0 , 1 );
query( 1 , 2 );
query( 3 , 6 );
query( 0 , 10 );
}
}
|
Python3
sum = [ 0 for i in range ( 1000005 )]
def toInt(x):
return int (x)
def prepareSum(s):
sum [ 0 ] = 0
for i in range ( 0 , len (s)):
sum [i + 1 ] = sum [i] + toInt(s[i])
def query(l, r):
if (( sum [r + 1 ] - sum [l]) % 3 = = 0 ):
print ( "Divisible by 3" )
else :
print ( "Not divisible by 3" )
if __name__ = = '__main__' :
n = "12468236544"
prepareSum(n)
query( 0 , 1 )
query( 1 , 2 )
query( 3 , 6 )
query( 0 , 10 )
|
C#
using System;
class GFG
{
static int []sum = new int [1000005];
static int toInt( char x)
{
return x - '0' ;
}
static void prepareSum(String s)
{
sum[0] = 0;
for ( int i = 0; i < s.Length; i++)
{
sum[i + 1] = sum[i] + toInt(s[i]);
}
}
static void query( int l, int r)
{
if ((sum[r + 1] - sum[l]) % 3 == 0)
{
Console.WriteLine( "Divisible by 3" );
}
else
{
Console.WriteLine( "Not divisible by 3" );
}
}
public static void Main()
{
String n = "12468236544" ;
prepareSum(n);
query(0, 1);
query(1, 2);
query(3, 6);
query(0, 10);
}
}
|
Javascript
<script>
let sum = [];
function toInt(x)
{
return x - '0';
}
function prepareSum(s)
{
sum[0] = 0;
for (let i = 0; i < s.length; i++)
{
sum[i + 1] = sum[i] + toInt(s[i]);
}
}
function query(l, r)
{
if ((sum[r + 1] - sum[l]) % 3 == 0)
{
document.write( "Divisible by 3" + "<br />" );
}
else
{
document.write( "Not divisible by 3" + "<br />" );
}
}
let n = "12468236544" ;
prepareSum(n);
query(0, 1);
query(1, 2);
query(3, 6);
query(0, 10);
</script>
|
PHP
<?php
$sum = [];
function toInt( $x )
{
return $x - '0' ;
}
function prepareSum( $s )
{
$sum [0] = 0;
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
$sum [ $i + 1] = $sum [ $i ] + toInt( $s [ $i ]);
}
}
function query( $l , $r )
{
if (( $sum [ $r + 1] - $sum [ $l ]) % 3 == 0)
{
echo ( "Divisible by 3" );
}
else
{
echo ( "Not divisible by 3" );
}
}
$n = "12468236544" ;
prepareSum( $n );
query(0, 1);
query(1, 2);
query(3, 6);
query(0, 10);
?>
|
Output:
Divisible by 3
Divisible by 3
Not divisible by 3
Divisible by 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...