Sort by

recency

|

2837 Discussions

|

  • + 0 comments

    For C#

    int n = Convert.ToInt32(Console.ReadLine().Trim());

    string binary = "";

    int max = 0;

    int sum = 0;

    int remainder = 0;

    for (int i = n; i > 0; i = i / 2)

    { remainder = i % 2; sum += remainder;

    if (remainder  == 1 &&sum > max)
        max = sum;
    
    else if (remainder == 0)
        sum = 0;
    
    binary += remainder.ToString();
    

    } binary = new string(binary.Reverse().ToArray());

    Console.WriteLine(max);

  • + 0 comments
    if __name__ == '__main__':
        n = int(input().strip())
        k=bin(n)
        k=k.replace("0b", "")
       
        count=0
        max=0
        for i in k:
            if i=='1':
                count=count +1
               
            else:
                count=0
            if count>max:
                max=count     
        print(max)
       
        
    
  • + 0 comments

    I have completed this solution in Python 3 with a bigginer freindly logic

    n = int(input().strip())
    
    binary = bin(n)[2:]   # remove '0b'
    max_ones = 0
    current = 0
    
    for bit in binary:
        if bit == '1':
            current += 1
            max_ones = max(max_ones, current)
        else:
            current = 0
    
    print(max_ones)
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            int n = Integer.parseInt(bufferedReader.readLine().trim());
            bufferedReader.close();
    
            System.out.println(Arrays.stream(Integer.toBinaryString(n).split("0+"))
                    .map(String::length)
                    .max(Comparator.comparingInt(l -> l)).orElse(0));
        }
    }
    
  • + 0 comments

    A simple one line solution for Python 3

    #!/bin/python3
    if __name__ == '__main__':
        n = int(input().strip())
    # Solution
        print(max([*map(len, bin(n)[2:].split("0"))]))