Alice's Sweets

4.4

8 votes
Binary Search, , Sorting, Algorithms
Problem

Alice has three different types of sweets. There are N sweets of each type, and each sweet has a tastiness value. The tastiness values for sweets are given in three arrays A, B, and C. The tastiness value of two sweets of a particular type can differ.

Alice wants to choose three sweets, exactly one of each type. Let the chosen sweets’ tastiness values be a, b, and c. You need to choose the sweets such that the value of the equation abs(ab)+abs(bc)+abs(ca) is minimized. Here, abs(x) is the absolute value function.

Input Format

  • The first line contains a single integer N denoting the number of sweets of each type.
  • The second line contains N space-separated integers denoting the array A.
  • The third line contains N space-separated integers denoting the array B.
  • The fourth line contains N space-separated integers denoting the array C.

Output Format

Print the minimum value of the equation mentioned in the problem statement if we can choose exactly one sweet of each type.

Constraints

1N1051A[i],B[i],C[i]108

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

There are 27 possible choices of sweets we can take. The minimum equation value is when we take the triplet with indices (3, 1, 1) or (3, 1, 2), or (1, 1, 1). In the first triplet, the equation value will be abs(A[3] - B[1]) + abs(B[1] - C[1]) + abs(C[1] - A[3]) = abs(5 - 3) + abs(3 - 5) + abs(5 - 5) = 4.

Editor Image

?