# ★★☆☆☆ 두개 이하로 다른 비트

# 구현코드

package pgm_77885;

public class Solution {
    public long[] solution(long[] numbers) {
        long[] answer = new long[numbers.length];
        int idx = 0;
        for(long number : numbers){
            long value = solve(number);
            answer[idx++] = value;
        }
        return answer;
    }

    public long solve(long number){
        long depth = 0;
        long target = number;
        while (true){
            if((target&1) == 0){
                break;
            }
            target /= 2;
            depth++;
        }

        double addValue = Math.pow(2, depth);
        double minusValue = depth == 0 ? 0 : Math.pow(2, depth-1);
        return number + (long) addValue - (long)minusValue;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30