Check if an array contains all elements of a given range | Function to check the array for elements in given range ; Range is the no . of elements that are to be checked ; Traversing the array ; If an element is in range ; Checking whether elements in range 0 - range are negative ; Element from range is missing from array ; All range elements are present ; Defining Array and size ; A is lower limit and B is the upper limit of range ; True denotes all elements were present ; False denotes any element was not present
< ? php function check_elements ( $ arr , $ n , $ A , $ B ) { $ range = $ B - $ A ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( abs ( $ arr [ $ i ] ) >= $ z = abs ( $ arr [ $ i ] ) - $ A ; if ( $ arr [ $ z ] > 0 ) { $ arr [ $ z ] = $ arr [ $ z ] * -1 ; } } } $ A && abs ( $ arr [ $ i ] ) <= $ B ) { $ count = 0 ; for ( $ i = 0 ; $ i <= $ range && $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) return -1 ; else $ count ++ ; } if ( $ count != ( $ range + 1 ) ) return -1 ; return true ; } $ arr = array ( 1 , 4 , 5 , 2 , 7 , 8 , 3 ) ; $ n = sizeof ( $ arr ) ; $ A = 2 ; $ B = 5 ; if ( ( check_elements ( $ arr , $ n , $ A , $ B ) ) == true ) echo " Yes " ; else echo " No " ; ? >