Skip to content

Instantly share code, notes, and snippets.

@aashish-chaubey
Created July 11, 2021 14:36
Show Gist options
  • Save aashish-chaubey/51aa70a61932739e03311e239e3eaf1b to your computer and use it in GitHub Desktop.
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
@aashish-chaubey
Copy link
Author

A company sells dumbells in pairs. These are weights for exercising. They receive a shipment of dumbells weighing anywhere from 1 unit upto a certain maximum. A pair can only be sold if their weights are sufficiently close: no greater than 1 unit difference. Given an inventory of various weights, determine the maximum number of pairs a company can sell.

For example, if there are 2 dumbells of weight 1, 4 of weight 2, 3 of weight 3, and 1 of weight 4, they can be paired as [1, 1], [2, 2], [2, 2], [3, 3], [3, 4] for a total of 5 pairs.

Function description

complete the function taskOfPairing. The function must return an integer representing the maximum number of similar pairs that can be made from the given supply of weights.

taskOfPairing has the following parameter(s):
freq [0...n-1]: a frequency array of integers where ith element represents the number of dumbells having a weight of i+1.

Constrains:

  • 1 ≤ n ≤ 105
  • 0 ≤ freq[i] ≤ 109

@Atharvk7
Copy link

Thanks bro

@rajdeep-biswas
Copy link

Buddy, 12/15 testcases don't pass with your solution.

@aashish-chaubey
Copy link
Author

aashish-chaubey commented Nov 23, 2021

Buddy, 12/15 testcases don't pass with your solution.

@therajdeepbiswas Can you please paste some of the test cases here? Let me check and fix it!

@vishalm30
Copy link

Buddy, 12/15 testcases don't pass with your solution.

Change last marker to False
Then all your test cases will run

@ramakrishna1607
Copy link

thanks a lot, bro, but change your code, there is a bug on top where you need to change marker=False

@Priyanshu581
Copy link

can you give full program

@GowthamPonraj
Copy link

i wrote the same code ... i only passed 3/15 cases ..

@mynameisjinhohong
Copy link

mynameisjinhohong commented Oct 26, 2024

def taskOfPairing(freq):
    dap = 0
    test = freq
    for i in range(len(test)):
        if i < len(test)-1:
            if test[i]%2 == 0:
                dap += test[i]//2
                print(i,test[i]//2,dap)
            else:
                dap += test[i]//2
                print(i,test[i]//2,dap)
                test[i+1] += 1
        else:
            dap += test[i]//2
    return dap

This is my code. i think mine and your is same logic. but i only pass 3/15 cases too. so, i copy and paste your code in hacker rank. but your code pass only 3/15 cases too. Is your code is correct?
ps.
oh I found my mistake. i change my code. and it pass all testcase. but i still wonder why when i paste your code it doesnt work...

@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