Skip to main content
2 of 2
deleted 34 characters in body
ovs
  • 61.2k
  • 3
  • 49
  • 164

Haskell, 102 bytes

Port of my BQN answer.

import Data.List n!([u,v]:e)=zipWith(-)(n!e)$0:tail n!nub[sort$take 2$(r++[v])\\[u]|r<-e] n!_=1:(0<$n) 

Try it online!

I feel like there should be a shorter way to replace \$u\$ with \$v\$ in a list (currently take 2$(r++[v])\\[u]).

ovs
  • 61.2k
  • 3
  • 49
  • 164