Sum of all duck numbers lying in range [L,R] for Q queries
Last Updated :
06 Sep, 2022
Given Q queries in the form of a 2D array arr[][] in which every row consists of two numbers L and R which denotes a range [L, R], the task is to find the sum of all duck numbers lying in the given range [L, R].
A duck number is a number that has at least one 0 present in it.
Examples:
Input: Q = 2, arr[] = {{1, 13}, {99, 121}}
Output: {10, 1275}
Explanation: In first query {1, 13}, only number with 0 in it is 10.
So the sum is 10.
In Second query {99, 121}, the numbers with 0 in them are
100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 120.
So their sum is 1275
Input: Q = 5, arr[] = {{1, 10}, {99, 121}, {56, 70}, {1000, 1111}, {108, 250}}
Output: {10, 1275, 130, 117105, 4762}
Approach: To solve the problem follow the below idea:
Use prefix array to store the sum of the numbers from 1 to a particular index having 0 in them.This way each query can be answered in O(1) .
Follow the steps to solve this problem:
- Initialize the pre[] array to store the prefix sum.
- Iterate from 1 to a certain big value (say 105, we can also fix this to be the maximum among the queries):
- If i has 0, then pre[i] = pre[i – 1] + i ;
- Otherwise, pre[i] = pre[i – 1];
- The answer to the query for range L to R is (pre[R] – pre[L – 1]).
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int pre[100001];
bool CheckZero( int x)
{
string s = to_string(x);
int i = 0, n = s.size();
while (i < n && s[i] == '0' )
i++;
while (i < n) {
if (s[i] == '0' )
return true ;
i++;
}
return false ;
}
void precompute()
{
for ( int i = 1; i < 100001; i++) {
if (CheckZero(i))
pre[i] = pre[i - 1] + i;
else
pre[i] = pre[i - 1];
}
}
void printresult( int L, int R)
{
cout << pre[R] - pre[L - 1] << endl;
}
void printsumduck( int arr[][2], int Q)
{
precompute();
for ( int i = 0; i < Q; i++) {
printresult(arr[i][0], arr[i][1]);
}
}
int main()
{
int Q = 5;
int arr[][2] = { { 1, 10 },
{ 99, 121 },
{ 56, 70 },
{ 1000, 1111 },
{ 108, 250 } };
printsumduck(arr, Q);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int [] pre= new int [ 100001 ];
static boolean CheckZero( int x)
{
String s = String.valueOf(x);
int i = 0 , n = s.length();
while (i < n && s.charAt(i)== '0' )
i++;
while (i < n) {
if (s.charAt(i) == '0' )
return true ;
i++;
}
return false ;
}
static void precompute()
{
for ( int i = 1 ; i < 100001 ; i++) {
if (CheckZero(i))
pre[i] = pre[i - 1 ] + i;
else
pre[i] = pre[i - 1 ];
}
}
static void printresult( int L, int R)
{
System.out.println(pre[R] - pre[L - 1 ]);
}
static void printsumduck( int [][]arr, int Q)
{
precompute();
for ( int i = 0 ; i < Q; i++) {
printresult(arr[i][ 0 ], arr[i][ 1 ]);
}
}
public static void main (String[] args) {
int Q = 5 ;
int [][] arr = { { 1 , 10 },
{ 99 , 121 },
{ 56 , 70 },
{ 1000 , 1111 },
{ 108 , 250 } };
printsumduck(arr, Q);
}
}
|
Python3
pre = [ 0 ] * ( 100001 )
def CheckZero(x):
s = str (x)
i = 0
n = len (s)
while (i < n and s[i] = = '0' ):
i + = 1
while (i < n):
if (s[i] = = '0' ):
return True
i + = 1
return False
def precompute():
for i in range ( 1 , 100001 ):
if (CheckZero(i)):
pre[i] = pre[i - 1 ] + i
else :
pre[i] = pre[i - 1 ]
def printresult(L, R):
print (pre[R] - pre[L - 1 ])
def printsumduck(arr, Q):
precompute()
for i in range (Q):
printresult(arr[i][ 0 ], arr[i][ 1 ])
Q = 5
arr = [[ 1 , 10 ],
[ 99 , 121 ],
[ 56 , 70 ],
[ 1000 , 1111 ],
[ 108 , 250 ]]
printsumduck(arr, Q)
|
C#
using System;
class GFG {
static int [] pre= new int [100001];
static bool CheckZero( int x)
{
string s = x.ToString();
int i = 0, n = s.Length;
while (i < n && s[i]== '0' )
i++;
while (i < n) {
if (s[i] == '0' )
return true ;
i++;
}
return false ;
}
static void precompute()
{
for ( int i = 1; i < 100001; i++) {
if (CheckZero(i))
pre[i] = pre[i - 1] + i;
else
pre[i] = pre[i - 1];
}
}
static void printresult( int L, int R)
{
Console.WriteLine(pre[R] - pre[L - 1]);
}
static void printsumduck( int [,]arr, int Q)
{
precompute();
for ( int i = 0; i < Q; i++) {
printresult(arr[i, 0], arr[i, 1]);
}
}
public static void Main()
{
int Q = 5;
int [,] arr = { { 1, 10 },
{ 99, 121 },
{ 56, 70 },
{ 1000, 1111 },
{ 108, 250 } };
printsumduck(arr, Q);
}
}
|
Javascript
<script>
let pre = new Array(100001).fill(0);
const CheckZero = (x) => {
let s = x.toString();
let i = 0, n = s.length;
while (i < n && s[i] == '0' )
i++;
while (i < n) {
if (s[i] == '0' )
return true ;
i++;
}
return false ;
}
const precompute = () => {
for (let i = 1; i < 100001; i++) {
if (CheckZero(i))
pre[i] = pre[i - 1] + i;
else
pre[i] = pre[i - 1];
}
}
const printresult = (L, R) => {
document.write(`${pre[R] - pre[L - 1]}<br/>`);
}
const printsumduck = (arr, Q) => {
precompute();
for (let i = 0; i < Q; i++) {
printresult(arr[i][0], arr[i][1]);
}
}
let Q = 5;
let arr = [[1, 10],
[99, 121],
[56, 70],
[1000, 1111],
[108, 250]];
printsumduck(arr, Q);
</script>
|
Output
10
1275
130
117105
4762
Time Complexity: O(max(Q, N)) where N is the maximum value till which precomputation is being done.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...