Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There are N sets of integers from 1 to K both inclusive. Find out number of pairs of sets whose union contains all the K elements.

What I have tried:

i have used set_union but its giving TLE , what should i do to optimise it ?
Posted
Updated 6-Jun-17 22:25pm
Comments
Patrice T 6-Jun-17 18:43pm    
"what should i do to optimise it ?"
What have you done ?
OriginalGriff 7-Jun-17 1:43am    
Without seeing the relevant code fragments, we can't even begin to guess how to improve your execution speed or time - that like you trying to improve the flavour of my Rogan Josh over the internet when I can't describe the current taste to you!
Use the "Improve question" widget to edit your question and provide better information.

1 solution

You don't need to fully perform the union of each pair: abandon the union computing process on first missing item, e.g.

Suppose
k=5, s1 = {1,2,5}, s2 = {1,4}


since 3 is missing in both s1 and s2 then you don't have to fully perform the union of the two sets to discover they are bad candidates.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900