Skip to main content
2 of 11
saved 2 bytes
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES6),  106  104 bytes

Saved 2 bytes thanks to @Shaggy

a=>a.filter(n=>n,[...a].map((v,i)=>a[a[p>v&(n=~~a[i+1])<p?i-++k:i+(k=0,n>v&p<n)]+=x=a[i],p=v,i]-=x,p=0))

Try it online!

Arnauld
  • 197.7k
  • 20
  • 179
  • 650