time limit error
https://ide.codingblocks.com/s/72783
Target sum pairs_
@p45s99tik hey pratik there are two things in your code
first
your loop is not right
correct with this loop
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i] + arr[j] == target){
cout<<arr[i]<<" and "<<arr[j]<<endl;
}
}
and second
when you’re already taking value of n then why are you taking size
pass n instead of size in the function arguments
and declare your array as a[1000] declaring like this a[n] may give you grabage value when you compiling your code
did changes suggested by you , got correct answer
what is wrong with loop in previous approach , i wanted to solve it using two pointer technique
@p45s99tik
you can refer to this line of code if you want to apply two pointer approach this will give you true or false for target element
bool isPairSum(A[], N, X)
{
// represents first pointer
int i = 0;
// represents second pointer
int j = N - 1;
while (i < j) {
// If we find a pair
if (A[i] + A[j] == X)
return true;
// If sum of elements at current
// pointers is less, we move towards
// higher values by doing i++
else if (A[i] + A[j] < X)
i++;
// If sum of elements at current
// pointers is more, we move towards
// lower values by doing i++
else
j--;
}
return false;
}