Need code for approach 1

I am facing difficulty in creating the code ,can i get the code for this very problem

hello @siddhant_samal
check this ->

logic is same as video explanation

figure out the problem , no need of code now

thankyou sir for your help, actually I have done an error now I have fixed that