Consider array elements from positions of set bits in the binary representation of n
for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) != 0 ) : NEW_LINE INDENT sum = sum + arr [ j ] NEW_LINE DEDENT DEDENT