Alice's Sweets
Practice
4.4 (8 votes)
Binary search
Sorting
Algorithms
Problem
69% Success 1705 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

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

1N105
1A[i],B[i],C[i]108

Sample Input
3
4 2 5
3 2 1
5 5 5
Sample Output
4
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.

Code Editor

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Submissions
Please login to view your submissions
Similar Problems
Points:30
13 votes
Tags:
AlgorithmsBinary SearchMediumSearchingpartitions
Points:30
11 votes
Tags:
AlgorithmsBinary SearchGreedy AlgorithmsMediumReadySearching
Points:20
32 votes
Tags:
ApprovedBasic ProgrammingEasyOpen
Editorial

Login to unlock the editorial