Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
C++ Program to find out the minimum difference value in n integer pairs
Suppose, we are given two arrays a and b that have a total of n and m values in them respectively. We have to make n or m number of pairs (whichever is minimum) using values from the two arrays. A pair must contain a value from array a and another one from array b. We have to make the pairs in such a way so that the difference of the value in the pairs is minimum and the same. We print the value as output.
So, if the input is like n = 4, m = 4, a = {2, 3, 4, 7}, b = {3, 4, 6, 5}, then the output will be 1.
The pairs that can be made are −
(3, 4), (4, 5), (7, 6), (2, 3).
The difference in the value in all pairs is 1.
Steps
To solve this, we will follow these steps −
sort the array a Define an array s1 initialized with 0 Define an array s2 initialized with 0 for initialize i := 1, when iExample
Let us see the following implementation to get better understanding −
#includeusing namespace std; const int INF = 1e9; void solve(int n, int m, vector a, vector b){ sort(a.begin(), a.end()); vector s1 = {0}; vector s2 = {0}; for (int i = 1; i a = {2, 3, 4, 7}, b = {3, 4, 6, 5}; solve(n, m, a, b); return 0; } Input
4, 4, {2, 3, 4, 7}, {3, 4, 6, 5}Output
1
