10364. Do you know your ABCs?

 

Farmer John's cows hold daily online meetings on the video conferencing platform “mooZ”. To have some fun, they have invented a simple number game that can be played during their meetings.

Elsie has three positive integers a, b and  c (abc). Since these numbers are considered secret, Elsie does not reveal them directly to her sister Bessie. Instead, she gives Bessie seven (not necessarily distinct) integers in the range from 1 to 109, claiming that they represent a, b, c, a + b, b + c, c + a and a + b + c and  in some order.

Given these seven numbers, help Bessie determine the values of a, b and  c. It is guaranteed that the answer exists and is unique.

 

Input. Seven integers.

 

Output. Print ab and c.

 

Sample input

Sample output

2 2 11 4 9 7 9

2 2 7

 

 

SOLUTION

greedy

 

Algorithm analysis

Sort all seven input numbers. The two smallest ones correspond to the values of a and b. The largest number in the list represents the sum a + b + c. Subtracting a and b from it gives the value of c.

 

Example

Sort the input numbers.

We have: a = 2, b = 2. Since a + b + c = 11, then c = 11 – ab = 11 – 2 – 2 = 7.

 

Algorithm implementation

Read the seven input numbers into an array.

 

for (i = 0; i < 7; i++)

  scanf("%d", &nums[i]);

 

Sort the array.

 

sort(nums, nums + 7);

 

The two smallest numbers correspond to the values of a and b.

 

a = nums[0], b = nums[1];

 

The largest number in the array equals a + b + c. Subtracting a and b from it gives c.

 

c = nums[6] - a - b;

 

Print the answer.

 

printf("%d %d %d\n", a, b, c);