Skip to main content

Command Palette

Search for a command to run...

Day 11 of My 30-Day Coding Challenge on HackerRank

Updated
3 min read

Day 11 was all about binary numbers and understanding how decimal numbers are converted into binary format.

This time, I solved the challenge without using built-in functions, which helped me understand the actual conversion logic much better.

Problem Overview

Today’s challenge focused on:

  • Decimal to binary conversion

  • Using loops and arithmetic operations

  • Counting consecutive 1s

  • Improving logical thinking

We were given an integer n and asked to:

  1. Convert it into binary

  2. Find the maximum number of consecutive 1s

  3. Print the result


My Solution

#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    n = int(input().strip())

    binary = ""

    while n > 0:
        remainder = n % 2
        binary = str(remainder) + binary
        n = n // 2

    count = 0
    max_count = 0

    for digit in binary:
        if digit == '1':
            count += 1
            if count > max_count:
                max_count = count
        else:
            count = 0

    print(max_count)

What I Did Today

I learned how to:

  • Convert decimal to binary manually

  • Use % 2 to get binary digits

  • Use // 2 to reduce the number step by step

  • Track consecutive values efficiently

Here’s the logic I followed:

1. Read integer n
2. Repeat until n becomes 0:
   - Get remainder using n % 2
   - Add remainder to binary string
   - Divide n by 2
3. Traverse binary digits
4. If digit is 1:
   - Increase count
   - Update maximum count
5. If digit is 0:
   - Reset count
6. Print maximum consecutive 1s

What I Learned

Today’s key takeaways:

  • Binary conversion uses repeated division by 2

  • % 2 gives the current binary digit

  • Consecutive counting is a common coding pattern

  • Manual logic helps understand concepts deeply

Example (n = 13)

Binary Conversion:

13 % 2 = 1
6 % 2 = 0
3 % 2 = 1
1 % 2 = 1

Reading from bottom to top:

1101

Consecutive Ones:

11 → count = 2
1  → count = 1

Maximum:

2

Challenges Faced

  • Building binary digits in correct order

  • Resetting count when 0 appears

  • Fixing indentation mistakes in conditions

  • Debugging failed test cases carefully


Why This Problem Matters

This problem helps in understanding:

  • Binary system used inside computers

  • Bit manipulation basics

  • Pattern tracking logic

  • Debugging skills

These are valuable concepts for coding interviews and real-world programming.


Plan for Day 12

  • Learn arrays or classes deeper

  • Solve more logic-based challenges

  • Improve debugging confidence


💬 Final Thoughts

Day 11 taught me that solving problems without shortcuts often gives deeper understanding.

It also reminded me how small mistakes like indentation can change program logic completely.

Let’s keep learning and growing