Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

aashish-chaubey commented Jul 11, 2021

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

Atharvk7 commented Oct 30, 2021

Thanks bro

@rajdeep-biswas
Copy link

rajdeep-biswas commented Nov 23, 2021

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

vishalm30 commented Jun 17, 2022

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

ramakrishna1607 commented Jun 24, 2022

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

@Priyanshu581
Copy link

Priyanshu581 commented Nov 9, 2022

can you give full program

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