The Weekly Challenge 367

Binary Optimization & Schedule Conflict Detection!

Original Challenge Link

Task 1: Max Odd Binary

"Make It Odd, Make It Big!"

This week's first task involves rearranging bits in a binary string to form the maximum odd binary number. Given a binary string containing at least one '1', we need to rearrange the bits to produce the largest possible odd binary number.

The Strategy: To create the maximum odd binary number, we need to place as many '1's as possible in the most significant positions while ensuring the number remains odd. Since an odd binary number must have '1' as its least significant bit, we put one '1' at the end and all other '1's at the beginning. The remaining zeros fill the middle positions.
Perl Implementation
sub max_odd_binary {
    my ($bin) = @_;
    die "Input must contain at least one '1'\n" unless $bin =~ /1/;
    my $ones = () = $bin =~ /1/g;
    my $zeros = length($bin) - $ones;
    # Result: ($ones-1) '1's, then zeros, then one '1' at the end
    return ('1' x ($ones - 1)) . ('0' x $zeros) . '1';
}
Python Implementation
def max_odd_binary(bin_str: str) -> str:
    """
    Return maximum odd binary number by rearranging bits.
    
    Args:
        bin_str: Binary string containing at least one '1'.
        
    Returns:
        Maximum odd binary number as a string.
        
    Raises:
        ValueError: If input doesn't contain at least one '1'.
    """
    if '1' not in bin_str:
        raise ValueError("Input must contain at least one '1'")
    ones = bin_str.count('1')
    zeros = len(bin_str) - ones
    # Result: (ones-1) '1's, then zeros, then one '1' at the end
    return '1' * (ones - 1) + '0' * zeros + '1'

Task 2: Conflict Events

"Schedule Showdown: Do They Overlap?"

The second task involves detecting conflicts between two events. Given two events each with start and end times, we need to determine if they have any overlap (non-empty intersection).

The Strategy: To check for conflicts, we convert time strings to minutes since midnight for easier comparison. Two events conflict if the start time of one event is before the end time of the other, and vice versa. We use the condition: max(start1, start2) < min(end1, end2) to detect overlaps.
Perl Implementation
sub time_to_minutes {
    my ($time) = @_;
    my ($h, $m) = split /:/, $time;
    return $h * 60 + $m;
}

sub has_conflict {
    my ($start1, $end1, $start2, $end2) = @_;
    my $s1 = time_to_minutes($start1);
    my $e1 = time_to_minutes($end1);
    my $s2 = time_to_minutes($start2);
    my $e2 = time_to_minutes($end2);
    # Overlap if max(s1,s2) < min(e1,e2)
    return ($s1 < $e2 && $s2 < $e1) ? 1 : 0;
}
Python Implementation
def time_to_minutes(time_str: str) -> int:
    """Convert HH:MM to minutes since midnight."""
    h, m = map(int, time_str.split(':'))
    return h * 60 + m

def has_conflict(start1: str, end1: str, start2: str, end2: str) -> bool:
    """
    Return True if two events overlap.
    
    Args:
        start1: Start time of first event (HH:MM).
        end1: End time of first event (HH:MM).
        start2: Start time of second event (HH:MM).
        end2: End time of second event (HH:MM).
        
    Returns:
        True if events have non-empty intersection, False otherwise.
    """
    s1 = time_to_minutes(start1)
    e1 = time_to_minutes(end1)
    s2 = time_to_minutes(start2)
    e2 = time_to_minutes(end2)
    # Overlap if intervals intersect
    return s1 < e2 and s2 < e1