Skip to content

Instantly share code, notes, and snippets.

@aashish-chaubey
Created July 11, 2021 14:36
Show Gist options
  • Select an option

  • Save aashish-chaubey/51aa70a61932739e03311e239e3eaf1b to your computer and use it in GitHub Desktop.

Select an option

Save aashish-chaubey/51aa70a61932739e03311e239e3eaf1b to your computer and use it in GitHub Desktop.
Task of Pairing
def taskOfPairing(freq):
count = 0
marker = False
for i in freq:
if i != 0:
count += i // 2
if i % 2 != 0 and marker:
count += 1
marker = False
elif i % 2 != 0:
marker = True
else:
marker = False
return count
@anshulg8
Copy link

anshulg8 commented Nov 12, 2024

This is my solution which passes all test cases.

def taskOfPairing(freq):
    # Initialize the number of pairs
    no_of_pairs = 0
    
    # Track any remaining dumbells that can't be paired at each step
    remaining = 0

    # Iterate over the frequency list
    for count in freq:
        if count != 0:
            if remaining != 0:
                # Add pairs that can be formed by combining the current count with the remaining
                no_of_pairs += (count + remaining) // 2
            else:
                # Add pairs that can be formed from the current count alone
                no_of_pairs += count // 2
            
            # Calculate if there's any leftover that can't be paired
            if (count + remaining) % 2 != 0:
                remaining = (count + remaining) % 2
            else:
                remaining = 0
        else:
            # Reset remaining if the count is zero
            remaining = 0

    return no_of_pairs
  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment