Practical Exam: Pair

advertisement
Pair
CS1020

You are given a number N. N distinct “strengths” follow. Then:
•

A match is composed of:
•

Two teams of two, with each team having the same
combined strength.
Two matches are different, if:
•

Count how many “matches” are there.
The ordering of teams is different
Two teams are different, if:
•
The ordering of team members is different
Sample Input:
4
1 2 3 4

4 persons each with strengths 1,2,3,4 respectively
What did you do
for the PE?
“Brute-Force” O(n4) solution

4-nested for loops

Keep a total counter

Pair-up the loop counters if all are different. If they sum up
equally (the sum of strengths) for two different teams,
increment the counter

Report the count
“Brute-Force” O(n4) solution
counter  0
for i : 0 to N-1
for j : 0 to N-1
for k : 0 to N-1
for l : 0 to N-1
if total strengths of two teams are
equal, increment the counter
end for
end for
end for
end for
print counter
How to get the O(n2) solution?
Hint:

A match is composed of:
•
Two teams of two, with each team having the same
combined strength.
Smart O(n2) solution

Read input and store them normally (as before)

Keep a sum array. What should the sum array contain?

Count the number of matches. How?
The sum array
N = 4

sum[index] = n means:
 There are n pairs that adds up
to index

We can pick two teams that sums
up to the same strength for a
match

If we have n pairs whose strengths
adds up to index:
 How many matches do we get
from them?
S[] = {1, 2, 3, 4}
index sum[index]
0
0
1
0
2
0
3
1
4
1
5
2
6
1
7
1
8
0
The sum array
N = 4
S[] = {1, 2, 3, 4}

Only 5 is the feasible sum of
strength for the team pairings.

There are two teams whose
strengths add up to 5

One pairing is found

What if there are 3? 4? 5? teams
that sum up to [index]?
index sum[index]
0
0
1
0
2
0
3
1
4
1
5
2
6
1
7
1
8
0
Choosing two teams
N = 6
S[] = {1, 2, 3, 4, 5, 6}
index
0
1
2
3
4
5
6
7
8
9
10 11
Sum[index]
0
0
0
1
1
2
2
3
2
2
1

We have 3 teams whose strength sums up to 7:

We can match them in 3 different ways:
{1, 6}
{2, 5}
{1, 6}
{3, 4}
{2, 5}
{3, 4}
{1, 6}
1
{2, 5}
𝑛!
𝑛
=
2
𝑛 − 2 ! 2!
{3, 4}
Choosing two teams
N = 6
S[] = {1, 2, 3, 4, 5, 6}
index
0
1
2
3
4
5
6
7
8
9
10 11
Sum[index]
0
0
0
1
1
2
2
3
2
2
1

We have 3 teams whose strength sums up to 7:

We can match them in 3 different ways:
{1, 6}
{2, 5}
{1, 6}
{3, 4}
{2, 5}
{3, 4}
{1, 6}
1
{2, 5}
𝑛(𝑛 − 1)
𝑛
=
2
2
{3, 4}
Calculating the matches


For each pairing of teams:
{1, 6}
{2, 5}
{2, 5}
{1, 6}
{1, 6}
{5, 2}
{5, 2}
{1, 6}
{6, 1}
{2, 5}
{2, 5}
{6, 1}
{6, 1}
{5, 2}
{5, 2}
{6, 1}
Total = number of pairings x 8
Putting them all together

Keep a sum array. sum[index] = n means that there
are n teams whose strength sums up to index. What
should be the initial capacity of sum[]?

(Hint: what is the maximum strength?)

Keep a counter (initially 0)

For each entry K in sum array:
𝑘
 Number of pairings is
[call this number N]
2
 Increment counter by N x 8

Report the total count
Initialize sum array
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
sum[nums[i] + nums[j]]++;
}
}
Counting the matches
for (int i : sum) {
count = count + i * (i - 1) / 2;
}
Download