logic for SNCK04

can anyone state the logic for this problem


m getting tle for the same…
//