aboutsummaryrefslogtreecommitdiff
path: root/challenge-114/ziameraj16/java/HigherIntegerSetBits.java
blob: 4d8f03fa63214ae2bc52912279523f59319621ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;

public class HigherIntegerSetBits {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number");
        int number = scanner.nextInt();
        System.out.println(findNextNumberWithSameOnes(number));

    }

    public static int findNextNumberWithSameOnes(int n) {
        long value = Integer.toBinaryString(n).chars().filter(ch -> ch == '1').count();
        while(true) {
            if (value == Integer.toBinaryString(++n).chars().filter(ch -> ch == '1').count()) {
                return n;
            }
        }
    }
}