// Given two non-empty arrays in non-decreasing order, // find the minimum absolute difference between pairs of elements. // // b.Length ┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐ // ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ // ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ // ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ // ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ // follow the ├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤ // "canyon" of 0 ┼─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┤ // minima near ↗ 0 a.Length // the diagonal function Dist(x: int, y: int): nat { if x < y then y - x else x - y } method CanyonSearch(a: array, b: array) returns (d: int) requires a.Length != 0 && b.Length != 0 requires forall i, j :: 0 <= i < j < a.Length ==> a[i] <= a[j] requires forall i, j :: 0 <= i < j < b.Length ==> b[i] <= b[j] ensures exists i, j :: 0 <= i < a.Length && 0 <= j < b.Length && d == Dist(a[i], b[j]) ensures forall i, j :: 0 <= i < a.Length && 0 <= j < b.Length ==> d <= Dist(a[i], b[j]) { d := Dist(a[0], b[0]); var m, n := 0, 0; while m < a.Length && n < b.Length invariant 0 <= m <= a.Length && 0 <= n <= b.Length invariant exists i, j :: 0 <= i < a.Length && 0 <= j < b.Length && d == Dist(a[i], b[j]) // TODO { if Dist(a[m], b[n]) < d { d := Dist(a[m], b[n]); } // TODO } }