Minimum difference between adjacent elements of array which contain elements from each row of a matrix - GeeksforGeeks
Given a matrix of N rows and M columns, the task is to find the minimum absolute difference between any of the two adjacent elements of an array of size N, which is created by picking one element from each row of the matrix. Note the element picked from row 1 will become arr[0], element picked from row 2 will become arr[1] and so on.
Read full article from Minimum difference between adjacent elements of array which contain elements from each row of a matrix - GeeksforGeeks
Given a matrix of N rows and M columns, the task is to find the minimum absolute difference between any of the two adjacent elements of an array of size N, which is created by picking one element from each row of the matrix. Note the element picked from row 1 will become arr[0], element picked from row 2 will become arr[1] and so on.
The idea is to sort all rows individually and then do binary search to find the closest element in next row for each element.
To do this in an efficient manner, sort each row of the matrix. Starting from row 1 to row N – 1 of matrix, for each element m[i][j] of current row in the matrix, find the smallest element in the next row which is greater than or equal to the current element, say p and the largest element which is smaller than the current element, say q. This can be done using Binary Search. Finally,find the minimum of the difference of current element from p and q and update the variable.
To do this in an efficient manner, sort each row of the matrix. Starting from row 1 to row N – 1 of matrix, for each element m[i][j] of current row in the matrix, find the smallest element in the next row which is greater than or equal to the current element, say p and the largest element which is smaller than the current element, say q. This can be done using Binary Search. Finally,find the minimum of the difference of current element from p and q and update the variable.
int bsearch(int low, int high, int n, int arr[]){ int mid = (low + high)/2; if(low <= high) { if(arr[mid] < n) return bsearch(mid +1, high, n, arr); return bsearch(low, mid - 1, n, arr); } return low;}// Return the minimum absolute difference adjacent// elements of arrayint mindiff(int arr[R][C], int n, int m){ // Sort each row of the matrix. for (int i = 0; i < n; i++) sort(arr[i], arr[i] + m); int ans = INT_MAX; // For each matrix element for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m; j++) { // Search smallest element in the next row which // is greater than or equal to the current element int p = bsearch(0, m-1, arr[i][j], arr[i + 1]); ans = min(ans, abs(arr[i + 1][p] - arr[i][j])); // largest element which is smaller than the current // element in the next row must be just before // smallest element which is greater than or equal // to the current element because rows are sorted. if (p-1 >= 0) ans = min(ans, abs(arr[i + 1][p - 1] - arr[i][j])); } } return ans;}Read full article from Minimum difference between adjacent elements of array which contain elements from each row of a matrix - GeeksforGeeks