Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5930067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:30:24+00:00 2026-05-22T14:30:24+00:00

Given a binary number calculate the maximum block. For ex: Binary representation = 11111

  • 0

Given a binary number calculate the maximum block.
For ex:
Binary representation = 11111
Maximum block length = 5

Binary representation = 10111011
Maximum block length = 3

max block means the number of consecutive 1’s or 0’s. So 00010000 would have a max block of 4

Above are the only 2 examples my professor gave.
“Compute the maximum block length of the binary
representation.” This is what he said. I’m assuming this includes 0s as well. I really don’t know how to go about it.

This is what I have come up with:

Let B = the binary number received.
put B into A[], each digit representing an element.
assume A[0] = 1
for A.length – 1
count 1s as long as not hit zero
max = total count of 1s.
count 0s as long as not hit 1
update max if necessary
repeat.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:30:24+00:00Added an answer on May 22, 2026 at 2:30 pm

    So I figured it out. Here’s the complete code. This gets the user input, converts the decimal number to binary, counts the maximum block of 1’s and counts total 1’s. There is still small bug, input should be accepted if 0 is entered and the program should work properly, but it does not.

    import java.util.Scanner;
    
    public class Test 
    {
        private static int decimalNumber = 0;
        private static String binaryNumber = "";
        // Get user input and return it
        // Check to make sure the input received is >= 0
        // If not > 0 then ask for input again
        private static void getInput()
        {
            Scanner sc = new Scanner(System.in); // Scanner to get user input
            System.out.println("Please type in a number >= 0"); // Tells user to type a number > 0
            decimalNumber = sc.nextInt(); // Stores the input in decimalNumber
            if(decimalNumber < 0)   // Loop to make sure input received is > 0
            {
                System.out.println("Incorrect input received"); // Tells the user input received was incorrect.
            }
        }
    
        private static void toBinary() 
        {
            while (decimalNumber != 0) 
            {
                if (binaryNumber.length() % 5 == 0) 
                {
                    binaryNumber = "" + binaryNumber;
                }
                binaryNumber = (decimalNumber % 2) + binaryNumber;
                decimalNumber /= 2;
            }
            System.out.println("Binary representation = " + binaryNumber);
        }
    
        public static void countOnes()
        {
            int ones = 0;
            for(int i=0; i < binaryNumber.length(); i++)
            {
                if(binaryNumber.charAt(i) == '1')
                {
                    ones++;
                }
            }
            System.out.println("No. of 1’s in the binary representation = " + ones);
        }
    
        public static void maximumBlock()
        {
            int block = 0;
            int maxBlock = 0;
            for(int i=0; i < binaryNumber.length(); i++)
            {
                if((binaryNumber.charAt(i) == '1') && (i < binaryNumber.length()))
                {
                    block++;
                    if(maxBlock < block)
                    {
                        maxBlock = block;
                    }
                }
                else
                {
                    block = 0;
                }
            }
            System.out.println("Maximum block length = " + maxBlock);
        }
    
        public static void main(String[] args)
        {
            getInput();
            toBinary();
            countOnes();
            maximumBlock();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a binary number that repeats, for example 0.(0011) or 0.0(101), how would one
Given either the binary or string representation of an IPv6 address and its prefix
Given a binary number, what is the fastest way of removing the lowest order
I have to flip all bits in a binary representation of an integer. Given:
I have number with binary representation 0000abcd . How convert it to 0a0b0c0d with
Is there any way to get the binary representation of a floating point number
Given a gray code for a number, find the binary code for the number.
Suppose in a given binary tree if each node contains number of child elements
I saw this problem: Given a binary search tree and a number, find a
I'm given three binary random variables: X, Y, and Z. I'm also given the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.