Target sum Problem

I am unable to code as my complexity will becoming greater than n^2 .please help me

Hello @Gade_Deepthi,

You have a video in which the approach has been explained to solve the problem.
Please, refer to that video.

Approach:

  1. Sort the input array.
  2. use to variable i=0 and j= length_of_array
  3. Loop until i>j:
    3.1. a[i]+a[j]>target:
    decrement j (because we need a smaller element)
    3.2. a[i]+a[j]<target:
    increment i (because we need a greater element)
    3.3. a[i]+a[j]==target:
    print the pair and both increment and decrement i and j resp.

Hope, this would help.
Give a like if you are satisfied.

1 Like

loop until j>i; not i>j

Hey @Gade_Deepthi,

Please mark it as resolved if you don’t have any more doubts regarding this.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.