this code can work with exceptional case where num equal to 2? to resolve this we need a set to remember sum’s
Did you mean that the code gives a wrong answer for num=2? I just tested the code and it worked fine (i.e. it gave the correct result) for num=2.