Queries to count points lying on or inside an isosceles Triangle with given length of equal sides | Javascript implementation of above approach ; Function to find answer of each query ; Stores the count of points with sum less than or equal to their indices ; Traverse the array ; If both x and y - coordinate < 0 ; Stores the sum of co - ordinates ; Increment count of sum by 1 ; Prefix array ; Perform queries ; Drivers Code
let MAX = ( 1e6 + 5 ) function query ( arr , Q ) { let pre = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) pre [ i ] = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] [ 0 ] < 0 arr [ i ] [ 1 ] < 0 ) continue ; let sum = Math . ceil ( ( arr [ i ] [ 0 ] + arr [ i ] [ 1 ] ) ) ; pre [ sum ] ++ ; } for ( let i = 1 ; i < MAX ; i ++ ) pre [ i ] += pre [ i - 1 ] ; for ( let i = 0 ; i < Q . length ; i ++ ) { document . write ( pre [ Q [ i ] ] + " " ) ; } document . write ( " " ) ; } let arr = [ [ 2.1 , 3.0 ] , [ 3.7 , 1.2 ] , [ 1.5 , 6.5 ] , [ 1.2 , 0.0 ] ] ; let Q = [ 2 , 8 , 5 ] ; let N = arr . length ; let M = Q . length ; query ( arr , Q ) ;