Count numbers from a given range that can be expressed as sum of digits raised to the power of count of digits
Given an array arr[] consisting of queries of the form {L, R}, the task for each query is to count the numbers in the range [L, R] that can be expressed as the sum of its digits raised to the power of count of digits.
Examples:
Input: arr[][] = {{8, 11}}
Output: 2
Explanation:
From the given range [1, 9], the numbers that can be expressed as the sum of its digit raised to the power of count of digits are:
- 8: Sum of digits = 8, Count of digit = 1. Therefore, 81 is equal to the given number.
- 9: Sum of digits = 9, Count of digit = 1. Therefore, 91 is equal to the given number.
Therefore, the count of such numbers from the given range is 2.
Input: arr[][] = {{10, 100}, {1, 400}}
Output: 0 11
Naive Approach: The simplest approach is to iterate over the range arr[i][0] to arr[i][1] for each query and print the count of such numbers.
Time Complexity: O(Q*(R – L)*log10R), where R and L denote the limits of the longest range.
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by precomputing and storing all the numbers whether they can be expressed as the sum of its digit raised to the power of count of digits or not. Finally, print the count for each query efficiently.
Follow the steps below to solve the problem:
- Initialize an auxiliary array, say ans[], to store at ans[i], whether i can be expressed as the sum of its digit raised to the power of count of digits.
- Iterate over the range [1, 106] and update the array ans[] accordingly.
- Convert the array ans[] to a prefix sum array.
- Traverse the given array of queries arr[] and for each query {arr[i][0], arr[i][1]}, print the value of (ans[arr[i][1]] – ans[arr[i][1] – 1]) as the resultant count of numbers that can be expressed as the sum of its digit raised to the power of count of digits.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define R 100005
int arr[R];
bool canExpress( int N)
{
int temp = N;
int n = 0;
while (N != 0) {
N /= 10;
n++;
}
N = temp;
int sum = 0;
while (N != 0) {
sum += pow (N % 10, n);
N /= 10;
}
return (sum == temp);
}
void precompute()
{
for ( int i = 1; i < R; i++) {
if (canExpress(i)) {
arr[i] = 1;
}
}
for ( int i = 1; i < R; i++) {
arr[i] += arr[i - 1];
}
}
void countNumbers( int queries[][2], int N)
{
precompute();
for ( int i = 0; i < N; i++) {
int L1 = queries[i][0];
int R1 = queries[i][1];
cout << (arr[R1] - arr[L1 - 1])
<< ' ' ;
}
}
int main()
{
int queries[][2] = {
{ 1, 400 },
{ 1, 9 }
};
int N = sizeof (queries)
/ sizeof (queries[0]);
countNumbers(queries, N);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static int R = 100005 ;
static int [] arr = new int [R];
public static boolean canExpress( int N)
{
int temp = N;
int n = 0 ;
while (N != 0 )
{
N /= 10 ;
n++;
}
N = temp;
int sum = 0 ;
while (N != 0 )
{
sum += (( int )Math.pow(N % 10 , n));
N /= 10 ;
}
if (sum == temp)
return true ;
return false ;
}
public static void precompute()
{
for ( int i = 1 ; i < R; i++)
{
if (canExpress(i))
{
arr[i] = 1 ;
}
}
for ( int i = 1 ; i < R; i++)
{
arr[i] += arr[i - 1 ];
}
}
public static void countNumbers( int [][] queries, int N)
{
precompute();
for ( int i = 0 ; i < N; i++)
{
int L1 = queries[i][ 0 ];
int R1 = queries[i][ 1 ];
System.out.print((arr[R1] - arr[L1 - 1 ]) + " " );
}
}
public static void main(String args[])
{
int [][] queries = { { 1 , 400 }, { 1 , 9 } };
int N = queries.length;
countNumbers(queries, N);
}
}
|
Python3
R = 100005
arr = [ 0 for i in range (R)]
def canExpress(N):
temp = N
n = 0
while (N ! = 0 ):
N / / = 10
n + = 1
N = temp
sum = 0
while (N ! = 0 ):
sum + = pow (N % 10 , n)
N / / = 10
return ( sum = = temp)
def precompute():
for i in range ( 1 , R, 1 ):
if (canExpress(i)):
arr[i] = 1
for i in range ( 1 ,R, 1 ):
arr[i] + = arr[i - 1 ]
def countNumbers(queries, N):
precompute()
for i in range (N):
L1 = queries[i][ 0 ]
R1 = queries[i][ 1 ]
print ((arr[R1] - arr[L1 - 1 ]),end = " " )
if __name__ = = '__main__' :
queries = [[ 1 , 400 ],[ 1 , 9 ]]
N = len (queries)
countNumbers(queries, N)
|
C#
using System;
class GFG{
static int R = 100005;
static int [] arr = new int [R];
public static bool canExpress( int N)
{
int temp = N;
int n = 0;
while (N != 0)
{
N /= 10;
n++;
}
N = temp;
int sum = 0;
while (N != 0)
{
sum += (( int )Math.Pow(N % 10, n));
N /= 10;
}
if (sum == temp)
return true ;
return false ;
}
public static void precompute()
{
for ( int i = 1; i < R; i++)
{
if (canExpress(i))
{
arr[i] = 1;
}
}
for ( int i = 1; i < R; i++)
{
arr[i] += arr[i - 1];
}
}
public static void countNumbers( int [,] queries, int N)
{
precompute();
for ( int i = 0; i < N; i++)
{
int L1 = queries[i, 0];
int R1 = queries[i, 1];
Console.Write((arr[R1] - arr[L1 - 1]) + " " );
}
}
static public void Main()
{
int [,] queries = { { 1, 400 }, { 1, 9 } };
int N = queries.GetLength(0);
countNumbers(queries, N);
}
}
|
Javascript
<script>
var R = 100005;
var arr = Array(R).fill(0);
function canExpress(N) {
var temp = N;
var n = 0;
while (N != 0) {
N = parseInt(N/10);
n++;
}
N = temp;
var sum = 0;
while (N != 0) {
sum += Math.pow(N % 10, n);
N = parseInt(N/10);
}
return (sum == temp);
}
function precompute() {
for ( var i = 1; i < R; i++) {
if (canExpress(i)) {
arr[i] = 1;
}
}
for ( var i = 1; i < R; i++) {
arr[i] += arr[i - 1];
}
}
function countNumbers(queries , N) {
precompute();
for ( var i = 0; i < N; i++) {
var L1 = queries[i][0];
var R1 = queries[i][1];
document.write((arr[R1] - arr[L1 - 1]) + " " );
}
}
var queries = [ [ 1, 400 ], [ 1, 9 ] ];
var N = queries.length;
countNumbers(queries, N);
</script>
|
Time Complexity: O(Q + 106)
Auxiliary Space: O(106)
Last Updated :
15 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...