Queries to calculate sum of array elements consisting of odd number of divisors | java program for the above approach ; Function to get the middle index from the given ranges ; Recursive function to find the sum of values in the given range of the array ; If segment of this node is a part of given range , then return the sum of the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps the given range ; Function to find the sum of elements in the range from index qs ( query start ) to qe ( query end ) ; Invalid ranges ; Recursive function to construct the Segment Tree for array [ ss . . se ] . si is index of current node in tree st ; If there is one element in the array ; Recur for left and right subtrees and store the sum of values in this node ; Function to construct segment tree from the given array ; Allocate memory for the segment tree Height of segment tree ; Maximum size of segment tree ; Allocate memory ; Fill the allocated memory st ; Return the constructed segment tree ; Function to find the sum of elements having odd number of divisors in index range [ L , R ] for Q queries ; Traverse the array , arr [ ] ; Replace elements that are not perfect squares with 0 ; Build segment tree from the given array ; Iterate through all the queries ; Print sum of values in array from index l to r ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static int getSumUtil ( int st [ ] , int ss , int se , int qs , int qe , int si ) { if ( qs <= ss && qe >= se ) return st [ si ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return getSumUtil ( st , ss , mid , qs , qe , 2 * si + 1 ) + getSumUtil ( st , mid + 1 , se , qs , qe , 2 * si + 2 ) ; } static int getSum ( int st [ ] , int n , int qs , int qe ) { if ( qs < 0 qe > n - 1 qs > qe ) { System . out . println ( " Invalid ▁ Input " ) ; return - 1 ; } return getSumUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; } static int constructSTUtil ( int arr [ ] , int ss , int se , int st [ ] , int si ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return arr [ ss ] ; } int mid = getMid ( ss , se ) ; st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) + constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 ) ; return st [ si ] ; } static int [ ] constructST ( int arr [ ] , int n ) { int x = ( int ) ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) ) ; int max_size = 2 * ( int ) Math . pow ( 2 , x ) - 1 ; int st [ ] = new int [ max_size ] ; constructSTUtil ( arr , 0 , n - 1 , st , 0 ) ; return st ; } static void OddDivisorsSum ( int n , int q , int arr [ ] , int Query [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { int sq = ( int ) Math . sqrt ( arr [ i ] ) ; if ( sq * sq != arr [ i ] ) arr [ i ] = 0 ; } int st [ ] = constructST ( arr , n ) ; for ( int i = 0 ; i < q ; i ++ ) { int l = Query [ i ] [ 0 ] ; int r = Query [ i ] [ 1 ] ; System . out . print ( getSum ( st , n , l , r ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 6 , 9 } ; int N = arr . length ; int Q = 3 ; int Query [ ] [ ] = { { 0 , 2 } , { 1 , 3 } , { 1 , 4 } } ; OddDivisorsSum ( N , Q , arr , Query ) ; } }