Please help me why it is not giving o/p for the given input?

this is how it runs
uptil 40 all the elements just simply get added making it 6
now comes 40 and sum > key so left goes ahead and sum becomes 45
and then it comes again in the loop, not it adds 40 again
sum becomes 95 so now 2 also gets subtracted
then again it becomes 135 because right is still at the place of 40
and now 3 gets subtracted and the loop ends

the correct approach,
take right and left both at 0
take a currsum = arr[0] and increment right
now run a while loop till the right reaches the end
inside check if sum < key add arr[right] and right++
if sum > key sub arr[left] and left ++
if sum == key print array from left to right

hey @kailash_01 if your doubt is solved, please mark it as resolved