Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

g152xx Group Title

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. E.g.: The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. Write a function: class Solution { public int binary_gap(int N); } that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

  • one year ago
  • one year ago

  • This Question is Closed
  1. Luckycutie1 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    this seems pretty hard of a problem

    • one year ago
  2. asnaseer Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    just thinking aloud, I would approach it along the lines of: maxGap = 0 inGap = false while N > 0 if N % 2 == 1 if inGap AND gapLength > maxGap maxGap = gapLength inGap = true gapLength = 0 else: gapLength++ N = N >> 1 return maxGap

    • one year ago
  3. luan.cestari Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    I made this program in python (just create a file with ".py" in the end put the code in it and use "python nameOfFile.py number", where 'number' can be any integer): """ Get the Max Gap """ import sys import re if __name__ == '__main__': number = int(sys.argv[1]) binNumber = bin(number) strBinNumber = binNumber.__str__() strBinNumber = re.split("0b", strBinNumber)[1] maxGap = 0 counter = 0 firstFound = False for c in list(strBinNumber): if c == '1': firstFound = True if maxGap < counter: maxGap = counter counter = 0 else: if firstFound: counter+=1 print(strBinNumber) print(maxGap)

    • one year ago
  4. g152xx Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you very much all of you, I can't give 2 medals but @asnasser I'll prepare one for you on the next one. Thanks much.

    • one year ago
  5. asnaseer Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    yw :)

    • one year ago
  6. atifmehar Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I made this program in Java public int maxBinaryGap(int number) { int maxGap = 0; boolean inGap = false; int gapLength = 0; while (number > 0) { if (number % 2 == 1) { if (inGap && gapLength > maxGap) { maxGap = gapLength; } inGap = true; gapLength = 0; } else { gapLength++; } number = number >> 1; } return maxGap; }

    • 11 months ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.