Range Queries for count of Armstrong numbers in subarray using MO 's algorithm | Java implementation to count the number of Armstrong numbers in subarray using MOaTMs algorithm ; Variable to represent block size . ; Class to represent a query range ; Count of Armstrong numbers ; To store the count of Armstrong numbers ; sort all queries so that all queries of the same block are arranged together and within a block , queries are sorted in increasing order of R values . ; Different blocks , sort by block . ; Same block , sort by R value ; sort all queries in order of their index value so that results of queries can be printed in same order as of input ) ; ; Function that return true if num is armstrong else return false ; Function to Add elements of current range ; If a [ currL ] is a Armstrong number then increment count_Armstrong ; Function to remove elements of previous range ; If a [ currL ] is a Armstrong number then decrement count_Armstrong ; Function to generate the result of queries ; Initialize count_Armstrong to 0 ; Find block size ; Initialize current L , current R and current result ; L and R values of current range ; Add Elements of current range ; Remove element of previous range ; Function to display the results of queries in their initial order ; Driver Code
import java . io . * ; import java . util . * ; static int block ; class Query { int L , R , index ; int armstrong ; 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 count_Armstrong ; Arrays . 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 ; } ) ; x . index - y . index ) ; for ( int i = 0 ; i < m ; i ++ ) { System . out . println ( q [ i ] . armstrong ) ; } } static boolean isArmstrong ( int x ) { int n = String . valueOf ( x ) . length ( ) ; int sum1 = 0 ; int temp = x ; while ( temp > 0 ) { int digit = temp % 10 ; sum1 += 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 ) ) ; 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 ) { Arrays . sort ( q , ( Query x , Query y ) -> public 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 ) ; } }