Find the sum of all array elements that are equidistant from two consecutive powers of 2 | Function to print the sum of array elements that are equidistant from two consecutive powers of 2 ; Stores the resultant sum of the array elements ; Traverse the array arr [ ] ; Stores the power of 2 of the number arr [ i ] ; Stores the number which is power of 2 and lesser than or equal to arr [ i ] ; Stores the number which is power of 2 and greater than or equal to arr [ i ] ; If arr [ i ] - LesserValue is the same as LargerValue - arr [ i ] ; Increment res by arr [ i ] ; Return the resultant sum res ; Driver Code
function FindSum ( arr , N ) { let res = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let power = Math . floor ( Math . log2 ( arr [ i ] ) ) ; let LesserValue = Math . pow ( 2 , power ) ; let LargerValue = Math . pow ( 2 , power + 1 ) ; if ( ( arr [ i ] - LesserValue ) == ( LargerValue - arr [ i ] ) ) { res += arr [ i ] ; } } return res ; } let arr = [ 10 , 24 , 17 , 3 , 8 ] ; let N = arr . length ; document . write ( FindSum ( arr , N ) ) ;