using
System;
using
System.Collections.Generic;
using
System.Linq;
class
Query
{
public
int
L, R, index, armstrong;
public
Query(
int
L,
int
R,
int
index,
int
armstrong)
{
this
.L = L;
this
.R = R;
this
.index = index;
this
.armstrong = armstrong;
}
}
class
GFG
{
static
int
block;
static
int
count_Armstrong;
static
bool
isArmstrong(
int
x)
{
int
n = x.ToString().Length;
int
sum1 = 0;
int
temp = x;
while
(temp > 0)
{
int
digit = temp % 10;
sum1 += (
int
)Math.Pow(digit, n);
temp /= 10;
}
if
(sum1 == x)
return
true
;
return
false
;
}
static
void
add(
int
currL,
int
[] a)
{
if
(isArmstrong(a[currL]))
count_Armstrong++;
}
static
void
remove(
int
currR,
int
[] a)
{
if
(isArmstrong(a[currR]))
count_Armstrong--;
}
static
void
queryResults(
int
[] a,
int
n, Query[] q,
int
m)
{
count_Armstrong = 0;
block = (
int
)(Math.Sqrt(n));
Array.Sort(q, (Query x, Query y) =>
{
if
(x.L / block != y.L / block)
return
x.L / block - y.L / block;
return
x.R - y.R;
});
int
currL = 0, currR = 0;
for
(
int
i = 0; i < m; i++)
{
int
L = q[i].L, R = q[i].R;
while
(currR <= R)
{
add(currR, a);
currR++;
}
while
(currL > L)
{
add(currL - 1, a);
currL--;
}
while
(currR > R + 1)
{
remove(currR - 1, a);
currR--;
}
while
(currL < L)
{
remove(currL, a);
currL++;
}
q[i].armstrong = count_Armstrong;
}
}
static
void
printResults(Query[] q,
int
m)
{
Array.Sort(q, (Query x, Query y) =>
x.index - y.index);
for
(
int
i = 0; i < m; i++)
{
Console.WriteLine(q[i].armstrong);
}
}
static
void
Main(
string
[] args)
{
int
[] arr = { 18, 153, 8, 9, 14, 5 };
int
n = arr.Length;
Query[] q =
new
Query[2];
q[0] =
new
Query(0, 5, 0, 0);
q[1] =
new
Query(3, 5, 1, 0);
int
m = q.Length;
queryResults(arr, n, q, m);
printResults(q, m);
}
}