Given integers n, d and three arrays a[], b[], c[] of size n each, print count of all distinct triplets {i,j,k} such that all three conditions satisfy -
abs(a[i]-b[j])<=d,
abs(b[j]-c[k])<=d &
abs(c[k]-a[i])<=d.
n<=100000
(You can assume the arrays are sorted)
Follow up: print all triplets
please share the optimal way to solve this problem.