When condition get true means we find the sum equal to target

even after finding the sum equals to target i am not increasing the i value and not decreasing the j value by 1
still i am getting the right answer and passsed all test cases
but as u taught us that u have to increase and decrease the value of i and j by 1;

code:

using namespace std;
#include
#include
int main()
{
int n,target;
cin>>n;
int a[n];

for(int i=0;i<n;i++)
cin>>a[i];
cin>>target;
sort(a,a+n);

int i=0,j=n-1;
while(i<j)
{
	if((a[i]+a[j])==target)
	{
		cout<<a[i]<<" and "<<a[j]<<endl;
		//i++;
		//j--;						//doubt ??
	}
	if((a[i]+a[j])<target)
	i++;
	else
	j--;
}

}

@n.nishchaya2000
Because of the if else statements.
if((a[i]+a[j])<target)
i++;
else
j–;
this if else automatically decreases the value of j.

so even if first condition get true it will check rest of two also if and else as well means

you have 1 if condition and 1 if else condition if(a[i] + a[j] == target) the else of second statement would work.

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.