Counting Rock Samples | TCS Codevita 2020 | Function to find the rock samples in the ranges ; Iterate over the ranges ; Driver Code ; Function call
function findRockSample ( ranges , n , r , arr ) { let a = [ ] ; for ( let i = 0 ; i < r ; i ++ ) { let c = 0 ; let l = ranges [ i ] [ 0 ] ; let h = ranges [ i ] [ 1 ] ; for ( let j = 0 ; j < arr . length ; j ++ ) { if ( l <= arr [ j ] && arr [ j ] <= h ) c += 1 ; } a . push ( c ) ; } for ( let i = 0 ; i < a . length ; i ++ ) { document . write ( a [ i ] + " " ) ; } } let n = 5 ; let r = 2 ; let arr = [ 400 , 567 , 890 , 765 , 987 ] ; let ranges = [ [ 300 , 380 ] , [ 800 , 1000 ] ] ; findRockSample ( ranges , n , r , arr ) ;